-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait2.c
100 lines (89 loc) · 2.38 KB
/
wait2.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
/* wait2.c */
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/types.h>
#
#define MAX_COUNT 200
#define BUF_SIZE 100
void ChildProcess(char [], char []); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid1, pid2, pid;
int status;
int i;
char buf[BUF_SIZE];
printf("*** Parent is about to fork process 1 ***\n");
if ((pid1 = fork()) < 0) {
printf("Failed to fork process 1\n");
exit(1);
}
else if (pid1 == 0)
ChildProcess("First", " ");
printf("*** Parent is about to fork process 2 ***\n");
if ((pid2 = fork()) < 0) {
printf("Failed to fork process 2\n");
exit(1);
}
else if (pid2 == 0)
ChildProcess("Second", " ");
ParentProcess();
sprintf(buf, "*** Parent enters waiting status .....\n");
write(1, buf, strlen(buf));
pid = wait(&status);
sprintf(buf, "*** Parent detects process %d was done ***\n", pid);
write(1, buf, strlen(buf));
pid = wait(&status);
printf("*** Parent detects process %d is done ***\n", pid);
printf("*** Parent exits ***\n");
exit(0);
}
#define QUAD(x) (x*x*x*x)
void ParentProcess(void)
{
int a, b, c, d;
int abcd, a4b4c4d4;
int count = 0;
char buf[BUF_SIZE];
sprintf(buf, "Parent is about to compute the Armstrong numbers\n");
write(1, buf, strlen(buf));
for (a = 0; a <= 9; a++)
for (b = 0; b <= 9; b++)
for (c = 0; c <= 9; c++)
for (d = 0; d <= 9; d++) {
abcd = a*1000 + b*100 + c*10 + d;
a4b4c4d4 = QUAD(a) + QUAD(b) + QUAD(c) + QUAD(d);
if (abcd == a4b4c4d4) {
sprintf(buf, "From parent: "
"the %d Armstrong number is %d\n",
++count, abcd);
write(1, buf, strlen(buf));
}
}
sprintf(buf, "From parent: there are %d Armstrong numbers\n", count);
write(1, buf, strlen(buf));
}
void ChildProcess(char *number, char *space)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
pid = getpid();
sprintf(buf, "%s%s child process starts (pid = %d)\n",
space, number, pid);
write(1, buf, strlen(buf));
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "%s%s child's output, value = %d\n",
space, number, i);
write(1, buf, strlen(buf));
}
sprintf(buf, "%s%s child (pid = %d) is about to exit\n",
space, number, pid);
write(1, buf, strlen(buf));
exit(0);
}