-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainOfProcess.c
134 lines (112 loc) · 4.61 KB
/
chainOfProcess.c
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
131
132
133
134
/*
* Name: Yuvashree
* Login: SP_19_CPS536_09
* Purpose:The goal is to become familiar with the basic ideas of processes in UNIX.
* Bug report: “No bugs, and all the features have been implemented” */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char* argv[]){
//initializations
int i, n, myid;
pid_t childpid, ppid;
myid = 1;
int eo ;
int odd = 0;
int even = 0;
int rand_num, val = 0;
//usage check
if(argc !=2){
fprintf(stderr,"Usage: %s <nprocs>\n",argv[0]);
exit(1);
}
else{
n = atoi(argv[1]);
}
//n range check
if (!(n>=1 && n<=10)){
fprintf(stderr,"nprocs must be in [1 ... 10]\n");
exit(1);
}
//if input is 1 then random number for current process is created.
if(n == 1){
srand(getpid());
rand_num = rand();
val = rand_num % 50000;
fprintf(stderr,"myid = %d pid = %d ppid = %d val = %d\n",myid, getpid(),getppid(),val);
if(val%2 == 0){
even = 1;
}
else{
odd = 1 ;
}
fprintf(stderr,"FINAL RESULTS: #even = %d #odd = %d \n",even,odd);
}
//loop for creating n process
for(i=1; i<n; i++){
childpid = fork();
//checks for fork failure
if(childpid <= -1){
printf("fork failed\n");
exit(1);
}
//generating random number for each process
srand(getpid());
rand_num = rand();
val = rand_num % 50000;
//child does
if(childpid == 0){
myid+=1;
fprintf(stderr,"myid = %d pid = %d ppid = %d val = %d\n",myid, getpid(),getppid(),val);
//assigning eo = 1 for odd random number and 10 for even random number
//this is only of last process in chain and exiting it
if(i == (n-1)){
eo=1;
if(val%2 == 0){
eo = 10;
}
exit(eo);
}
}
//parent does
else{
int oddOrEven;
if(i == 1){
myid=1;
fprintf(stderr,"myid = %d pid = %d ppid = %d val = %d\n",myid, getpid(),getppid(),val);
}
wait(&oddOrEven);
int newOddOrEven = oddOrEven/256;
//if exited successfully
if(WIFEXITED(oddOrEven)){
//first digit is for even
//second digit is for odd
even =newOddOrEven/10;
odd =newOddOrEven%10;
fprintf(stderr,"myid is %d received #even = %d #odd = %d \n",myid,even,odd);
}
//even and odd becomes 0 for for unsuccessfull termination
else{
even = 0;
odd = 0;
fprintf(stderr,"myid is %d, abnormal exit of child, therefore received #even = %d #odd = %d \n",myid,even,odd);
}
//condition to find odd or even and updating the variable
if(val%2 == 0){
newOddOrEven+=10;
}
else if(val%2 !=0){
newOddOrEven+=1;
}
//first process prints the final result
if(myid == 1){
even =newOddOrEven/10;
odd =newOddOrEven%10;
fprintf(stderr,"\nFINAL RESULTS: #even = %d #odd = %d \n",even,odd);
}
exit(newOddOrEven);
}
}
}