File tree 1 file changed +78
-0
lines changed
1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <unistd.h>
2
+ #include <sys/types.h>
3
+ #include <stdio.h>
4
+ #include <signal.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+ #include <sys/wait.h>
8
+ #include <time.h>
9
+
10
+ #define MAX_CHILD 5
11
+
12
+ int main (){
13
+ int fd [MAX_CHILD ][2 ];
14
+ int v = 100 ,i ,which ;
15
+ int total = 0 ;
16
+ int Chile_process = 0 ;
17
+ char buffer [256 ];
18
+ int mypipe [MAX_CHILD ];
19
+ pid_t pid [MAX_CHILD ];
20
+ char parent_buffer [256 ];
21
+
22
+ for (i = 0 ;i < MAX_CHILD ;i ++ ){
23
+
24
+ mypipe [i ]= pipe (fd [i ]);
25
+ pid [i ]= fork ();
26
+
27
+ //which=i;
28
+
29
+ if (pid [i ]== 0 ){
30
+ Chile_process = 1 ;
31
+ break ;
32
+ }
33
+
34
+ }
35
+
36
+ while (Chile_process == 1 )
37
+ {
38
+ close (fd [i ][0 ]);
39
+ srand (time (NULL )+ getpid ());
40
+ int myrandnum = rand ()%10 + 1 ;
41
+ printf ("in child and i is %d and myrandnum is %d\n" ,i ,myrandnum );
42
+
43
+ sprintf (buffer ,"%d" , myrandnum );
44
+ printf ("%s\n" ,buffer );
45
+ write (fd [i ][1 ],buffer ,255 );
46
+ printf ("pass write\n" );
47
+ sleep (myrandnum );
48
+ }
49
+
50
+ while (Chile_process == 0 ){
51
+ //close(fd[i][1]);
52
+ for (int k = 0 ;k < MAX_CHILD ;k ++ ){
53
+ close (fd [i ][1 ]);
54
+ if (read (fd [k ][0 ],buffer ,255 )> 0 )
55
+ {
56
+ total += atoi (buffer );
57
+ printf ("the total is %d \n" , total );
58
+
59
+ sleep (1 );
60
+ }
61
+
62
+ }
63
+
64
+ if (total > 30 )
65
+ {
66
+ printf ("now total is %d\n" , total );
67
+ kill (0 , SIGKILL );
68
+ printf ("process is killed\n" );
69
+ //sleep(1);
70
+ }
71
+
72
+ }
73
+
74
+
75
+
76
+ return 0 ;
77
+ }
78
+
You can’t perform that action at this time.
0 commit comments