getpid() checking of code

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  
#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
Last edited on
We did this already, and the code looks no different to last time.
https://www.cplusplus.com/forum/general/277406/

You didn't even manage to fix the array overrun problems.
> int arr1[99];
...
> for (int i = 0; i < 100; i++) sum1 += arr1[i];
Topic archived. No new replies allowed.