I need to get pid for every processes. In process I get sum of every arrays. In the I get max sum.
I wrote programm but I have doubt. Can you check it?
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
#include <algorithm>
int main(){
int arr1[99];
for(int i=0; i<99; i++) arr1[i] = 0+rand()%10;
int arr2[99];
for(int i=0; i<199; i++) arr2[i] = 0+rand()%20;
int arr3[99];
for(int i=0; i<99; i++) arr3[i] = 0+rand()%30;
int arr4[99];
for(int i=0; i<99; i++) arr4[i] = 0+rand()%40;
int arr5[99];
for(int i=0; i<99; i++) arr5[i] = 0+rand()%50;
//process 1
pid_t pid1;
pid1=fork();
if (pid1<0){
std::cout << "Cannot create child process. Exit!!!" << std::endl;
exit(1);
}
if (pid1==0){
std::cout << "This is child process. My pid 1 = " << getpid() << std::endl;
int sum1 = 0;
for (int i = 0; i < 100; i++) sum1 += arr1[i];
std::cout << "sum 1 = " << sum1 << std::endl;
exit(0);
}
std::cout<< "This is parent process. Pid child process 1 " << pid1 << std::endl;
int sum1 = 0;
for (int i = 0; i < 100; i++) sum1 += arr1[i];
std::cout << "sum 1 = " << sum1 << std::endl;
wait(0);
//process 2
pid_t pid2;
pid2=fork();
if (pid2<0){
std::cout << "Cannot create child process. Exit!!!" << std::endl;
exit(1);
}
if (pid2==0){
std::cout << "This is child process. My pid 2 = " << getpid() << std::endl;
int sum2 = 0;
for (int i = 0; i < 100; i++) sum2 += arr2[i];
std::cout << "sum 2 = " << sum2 << std::endl;
exit(0);
}
std::cout<< "\nThis is parent process. Pid child process 2 = " << pid2 << std::endl;
int sum2 = 0;
for (int i = 0; i < 100; i++) sum2 += arr2[i];
std::cout << "sum 2 = " << sum2 << std::endl;
wait(0);
//process 3
pid_t pid3;
pid3=fork();
if (pid3<0){
std::cout << "Cannot create child process. Exit!!!" << std::endl;
exit(1);
}
if (pid3==0){
std::cout << "This is child process. My pid 3 = " << getpid() << std::endl;
int sum3 = 0;
for (int i = 0; i < 100; i++) sum3 += arr3[i];
std::cout << "sum 3 = " << sum3 << std::endl;
exit(0);
}
std::cout<< "\nThis is parent process. Pid child process 3 " << pid3 << std::endl;
int sum3 = 0;
for (int i = 0; i < 100; i++) sum3 += arr3[i];
std::cout << "sum 3 = " << sum3 << std::endl;
wait(0);
//process 4
pid_t pid4;
pid4=fork();
if (pid4<0){
std::cout << "Cannot create child process. Exit!!!" << std::endl;
exit(1);
}
if (pid4==0){
std::cout << "This is child process. My pid 4 = " << getpid() << std::endl;
int sum4 = 0;
for (int i = 0; i < 100; i++) sum4 += arr4[i];
std::cout << "sum 4 = " << sum4 << std::endl;
exit(0);
}
std::cout<< "\nThis is parent process. Pid child process 4 " << pid4 << std::endl;
int sum4 = 0;
for (int i = 0; i < 100; i++) sum4 += arr4[i];
std::cout << "sum 4 = " << sum4 << std::endl;
wait(0);
//process 5
pid_t pid5;
pid5=fork();
if (pid5<0){
std::cout << "Cannot create child process. Exit!!!" << std::endl;
exit(1);
}
if (pid5==0){
std::cout << "This is child process. My pid 5 = " << getpid() << std::endl;
int sum5 = 0;
for (int i = 0; i < 100; i++) sum5 += arr5[i];
std::cout << "sum 5 = " << sum5 << std::endl;
exit(0);
}
std::cout<< "\nThis is parent process. Pid child process 5 " << pid5 << std::endl;
int sum5 = 0;
for (int i = 0; i < 100; i++) sum5 += arr5[i];
std::cout << "sum 5 = " << sum5 << std::endl;
wait(0);
std::cout << "\nMax value = " << std::max({sum1, sum2, sum3, sum4, sum5}) << std::endl;
return 0;
};
Result:
This is parent process. Pid child process 1 5051
sum 1 = 464
This is child process. My pid 1 = 5051
sum 1 = 464
This is child process. My pid 2 = 5052
sum 2 = 978
This is parent process. Pid child process 2 = 5052
sum 2 = 978
This is parent process. Pid child process 3 5053
This is child process. My pid 3 = 5053
sum 3 = 1275
sum 3 = 1275
This is parent process. Pid child process 4 5054
sum 4 = 1762
This is child process. My pid 4 = 5054
sum 4 = 1762
This is parent process. Pid child process 5 5055
sum 5 = 2767
This is child process. My pid 5 = 5055
sum 5 = 2767
Max value = 2767