-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.c
103 lines (91 loc) · 1.64 KB
/
test4.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
/* test 4 */
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
pid_t pid0, pid1, pid2, pid3;
int s, i, fd, nextb, errct = 0;
char *tempfile = "test4.temp";
char buf[1024];
int main(void);
void subr(void);
void nofork(void);
void quit(void);
int main()
{
int k;
printf("Test 4 ");
fflush(stdout); /* have to flush for child's benefit */
system("rm -rf DIR_04; mkdir DIR_04");
chdir("DIR_04");
creat(tempfile, 0777);
for (k = 0; k < 20; k++) {
subr();
}
unlink(tempfile);
quit();
return(-1); /* impossible */
}
void subr()
{
if ( (pid0 = fork()) != 0) {
/* Parent 0 */
if (pid0 < 0) nofork();
if ( (pid1 = fork()) != 0) {
/* Parent 1 */
if (pid1 < 0) nofork();
if ( (pid2 = fork()) != 0) {
/* Parent 2 */
if (pid2 < 0) nofork();
if ( (pid3 = fork()) != 0) {
/* Parent 3 */
if (pid3 < 0) nofork();
for (i = 0; i < 10000; i++);
kill(pid2, 9);
kill(pid1, 9);
kill(pid0, 9);
wait(&s);
wait(&s);
wait(&s);
wait(&s);
} else {
fd = open(tempfile, O_RDONLY);
lseek(fd, 20480L * nextb, 0);
for (i = 0; i < 10; i++) read(fd, buf, 1024);
nextb++;
close(fd);
exit(0);
}
} else {
while (1) getpid();
}
} else {
while (1) getpid();
}
} else {
while (1) getpid();
}
}
void nofork()
{
int e = errno;
printf("Fork failed: %s (%d)\n",strerror(e),e);
exit(1);
}
void quit()
{
chdir("..");
system("rm -rf DIR*");
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}