-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.c
369 lines (245 loc) · 8.46 KB
/
parallel.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include<stdlib.h>
#include<stdio.h>
#include <stdbool.h>
#include<unistd.h>
#include<sys/wait.h>
#include<math.h>
FILE *f,*f1,*f2, *f3;
void p1( int c1){ //fils p1
f = fopen("f.txt","r");
f1 = fopen("f1.txt","w+");
pid_t id;
int p, status, premier1;
//fscanf(f, "%d", &p); //to avoid 2, we only want to put multiples
while (fscanf(f, "%d", &p) != EOF ) // trouver le nombre P immédiatement superiur a c1----------- first iteration: p = 3
{
if(p %c1 != 0 && p>c1)
{
break; //we found P, exit the loop
}
}
//p=3
// p will have this value of P after the fork
if ((id = fork()) < 0) { //create child p
perror("p creation failed !\n");
fclose(f);
fclose(f1);
exit(-1);
}else if(id ==0) //p2
{
f = fopen("f.txt","r");
f2 = fopen("f2.txt","w+");
int c, p2;
// 2 3 4 5 6 7 8 9 10 11 12 13 14
while (fscanf(f, "%d", &c) != EOF) //écrire les multiples de P dans F2
{
if ((c%p == 0 && c>p)) //p
{
fprintf(f2, "%d ", c);
//fscanf(f,"%d",&c);
}
}
rewind(f); // return the pointer to the beginning of f.
// nombre immédiatement sup a P=3
// 2 3 4 5 6 7 8 9 10 11 12 13 14
while (fscanf(f, "%d", &p2) != EOF ) // trouver le nombre p immédiatement superiur a le P precedent
{
if((p2 % p != 0) && (p2 > p )&& (p2 %c1 != 0))
{
break; // we exists p, exit the loop.
}else{
continue;
}
}
//p2 = 5
printf("\n nombre P immédiatement sup a %d = %d (trouvé par p1)\n", c1, p);
printf(" nombre P2 immédiatement sup a p = %d (trouvé par p2)\n", p2);
fclose(f); fclose(f2);
exit(p2); //send the number p to the the process P1--------- first iteration: p = 5
} else { //p1 //en parallel ye7seb multiples de C1 -----first iteration: c1 =2
int c;
rewind(f); //pointer to the begining
while (fscanf(f, "%d", &c) != EOF) //multiples of c1 in f1
{
if ((c%c1 == 0 && c > c1))
{
fprintf(f1, "%d ", c);
fscanf(f,"%d",&c);
}
}
wait(&status); //wait for the child process P2 to send the number.--------first iteration: p2 will send 5
if (WIFEXITED(status))
{
// premier1 will have the value received from P2-----first iteration: 5
premier1 = WEXITSTATUS(status); printf("\n P1 a recu %d de P2\n", WEXITSTATUS(status));
}else{
printf(" \n\np didn't exit normally: %d\n\n", WEXITSTATUS(status));
}
}
fclose(f); fclose(f1);
//premier1 = 5
exit(premier1); // now P1 will send the number received from p2 (5 in first iteration) to the main Process (to update the variable "Premier" and iterate)
}
void fill(int n){
int r;
f=fopen("f.txt","w+");
if(f==NULL){
printf("error\n");
exit(1);}
for(int i=2;i<=n;i++){
fprintf(f," %d \n",i);
}
fclose(f);
}
void printFile()
{
int primeNumber;
f = fopen("f.txt", "r");
if (f == NULL) // In case we could not open the file.
{
printf("error\n");
exit(1);
}
while (fscanf(f, "%d", &primeNumber) != EOF)
{
printf("%d ", primeNumber);
}
printf("\n");
fclose(f);
}
void printF2()
{
int primeNumber;
f2 = fopen("f2.txt", "r");
if (f2 == NULL) // In case we could not open the file.
{
printf("error\n");
exit(1);
}
while (fscanf(f2, "%d", &primeNumber) != EOF)
{
printf("%d ", primeNumber);
}
printf("\n");
fclose(f2);
}
void printF1()
{
int primeNumber;
f1 = fopen("f1.txt", "r");
if (f1 == NULL) // In case we could not open the file.
{
printf("error\n");
exit(1);
}
while (fscanf(f1, "%d", &primeNumber) != EOF)
{
printf("%d ", primeNumber);
}
printf("\n");
fclose(f1);
}
void main(){
int id, n, status, premier = 2;
printf(" insert n \n");
scanf("%d",&n);
int sqr = sqrt(n);
printf("\nracine de n: %d\n\n", sqr);
fill(n);
while(premier <= sqr)
{
printf("________________________________________");
printf("\n PREMIER est maintenant = %d\n", premier);
id = fork(); // create p1
if(id <0)
{
printf("error fork failed\n");
exit(-1);
}else if(id == 0){
p1(premier); // premier = 2 in the beginning, and then 5, then 7 ...
}else
{
wait(&status); //wait for p1 to send the next prime number------- first iteration: rah yejih 5, then we update premier =5 to iterate again
if (WIFEXITED(status))
{
sleep(3);
//update premier
premier = WEXITSTATUS(status); printf("\n Processus Parent a recu: %d de P1", premier );
}else{
printf("\n P1 didn't exit normally: %d\n", WEXITSTATUS(status));
}
printf("\n processus Parent continue...");
// printFile();
printf("\n\n");
// printF3();
printf("f1 contient: ");
printF1();
printf("\nF2 contient: ");
printF2();
//update f
f = fopen("f.txt", "r");
f3 = fopen("f3.txt", "w"); //temporaire
int exists = 0, f_number, f1_number, f2_number;
// remove all the numbers of F1 and F2 from F, put the result in a remporary file F3 and then copy it back to F at the end
while(fscanf(f, "%d", &f_number) != EOF && exists == 0) //update f
{
f1 = fopen("f1.txt", "r");
f2 = fopen("f2.txt", "r");
if (f1 == NULL || f2 == NULL) // In case we could not open the file.
{
printf("error creating f1, f2\n");
exit(1);
}else
{
while (fscanf(f1, "%d", &f1_number) != EOF && exists == 0 ) //check if f_number exists in f1
{
if (f_number == f1_number)
{
exists = 1;
}
}
while (fscanf(f2, "%d", &f2_number) != EOF && exists == 0) //check if f_number exists in f2
{
if (f_number == f2_number)
{
exists = 1;
}
}
// If the value does not exist in both files f1 & f2 then we will add the value into the temp file F3.
if (exists == 0)
{
fprintf(f3, " %d\n", f_number);
}
exists = 0; // Set it to 0 for the next iteration.
fclose(f1);
fclose(f2);
}
}
fclose(f);
fclose(f3);
f3 = fopen("f3.txt", "r");
f = fopen("f.txt", "w+");
if (f == NULL || f3 == NULL) // In case we could not open the file.
{
printf("error\n");
exit(1);
}else
{
// update F: copy f3 into F
while (fscanf(f3, "%d", &f_number) != EOF)
{
fprintf(f, " %d\n", f_number);
}
fclose(f);
//printFile();
fclose(f3);
}
}
}
printf("\n _______________");
printf("\n Boucle Terminé!\n");
printf(" _______________\n");
printf("\n les nombres premiers sont: \n");
printFile();
exit(0);
}