-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bakery.c
184 lines (122 loc) · 3.57 KB
/
Bakery.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <errno.h>
#include <semaphore.h>
#include <fcntl.h>
//initialize to 0 function
void initArray(int *array, int size)
{
for (int i = 0; i < size; i++)
{
array[i] = 0;
}
}
//find max function
int findMax(int *array, int size)
{
int i;
int maxToken = 0;
for (i = 0; i < size; i++)
{
if(array[i] > maxToken)
{
maxToken = array[i];
}
}
return maxToken;
}
//printf array function
void printArray(int *array, int size)
{
for (int i = 0; i < size; i ++)
{
printf("%d\t",array[i]);
}
}
int main()
{
int cnt , chStatus;
pid_t deadPid;
key_t shmkey[3];
int shmid[3];
printf("Please enter number of child processes to be created: ");
scanf("%d",&cnt);
int *SA = malloc(cnt * sizeof(*SA)); //shared
int *token = malloc(cnt * sizeof(*token)); //shared
int *choosing = malloc(cnt * sizeof(*choosing)); //shared
pid_t *pid = malloc(cnt * sizeof(*pid));
if(!SA || !token || !choosing || !pid)
{
printf("Failure allocating memory.");
exit(EXIT_FAILURE);
}
for(int i = 0; i < 3; i++)
{
shmkey[i] = ftok ("/dev/null", i); //hashes the pathname and integer to a unique key. returns -1 on failure.
shmid[i] = shmget(shmkey[i], sizeof (int), 0644 | IPC_CREAT); // allocates memory segment. On sucess returns memory key.
}
// shared memory error check
for(int i = 0; i < 3; i++)
{
if (shmid[i] == -1)
{
perror ("shmget\n");
exit (1);
}
}
// memory attachment
SA = (int *) shmat(shmid[0], NULL, 0);
initArray(SA,cnt);
token = (int *) shmat(shmid[1], NULL, 0);
initArray(token,cnt);
choosing = (int *) shmat(shmid[2], NULL, 0);
initArray(choosing,cnt);
for(int i = 0; i < cnt; i++)
{
pid[i] = fork();
if(pid[i] == 0) //if child process
{
sleep(cnt - i);
//get token
choosing[i] = 1;
token[i] = findMax(token , cnt) + 1;
choosing[i] = 0;
for(int j = 0; j < cnt - 1; j++)
{
while(choosing[j]) {/* wait */}
while(token[j] != 0 && (token[j] < token[i] || (token[i] == token[j] && j < i))) {/* still do nothing */}
//critical section
for (int k = 0; k < cnt; k++)
{
SA[k] = SA[k] + i;
}
token[i] = 0;
//end of critical section
printf("\nHello, i was process %d with pid: %d and SA values now are:\n",i,getpid());
printArray(SA,cnt);
printf("\n");
exit(i);
} //end of j loop
}
if (pid[i] < 0)
{
perror("Something went wrong with fork..");
return -1;
}
}// end of i loop
// wait children first to terminate
for(int i = 0; i < cnt; i++)
{
deadPid = waitpid(pid[i],&chStatus,0);
if(WIFEXITED(chStatus))
{
printf("child %d terminated with exit status %d\n", deadPid, WEXITSTATUS(chStatus) );
}
else printf("Child %d terminated abnormally\n", deadPid);
}
free(pid);
return (0);
}