1*433d6423SLionel Sambuc /* test 4 */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <sys/types.h>
4*433d6423SLionel Sambuc #include <sys/wait.h>
5*433d6423SLionel Sambuc #include <fcntl.h>
6*433d6423SLionel Sambuc #include <signal.h>
7*433d6423SLionel Sambuc #include <stdlib.h>
8*433d6423SLionel Sambuc #include <unistd.h>
9*433d6423SLionel Sambuc #include <stdio.h>
10*433d6423SLionel Sambuc #include <string.h>
11*433d6423SLionel Sambuc #include <errno.h>
12*433d6423SLionel Sambuc
13*433d6423SLionel Sambuc pid_t pid0, pid1, pid2, pid3;
14*433d6423SLionel Sambuc int s, i, fd, nextb;
15*433d6423SLionel Sambuc char *tempfile = "test4.temp";
16*433d6423SLionel Sambuc char buf[1024];
17*433d6423SLionel Sambuc
18*433d6423SLionel Sambuc int max_error = 2;
19*433d6423SLionel Sambuc #include "common.h"
20*433d6423SLionel Sambuc
21*433d6423SLionel Sambuc
22*433d6423SLionel Sambuc
23*433d6423SLionel Sambuc int main(void);
24*433d6423SLionel Sambuc void subr(void);
25*433d6423SLionel Sambuc void nofork(void);
26*433d6423SLionel Sambuc void quit(void);
27*433d6423SLionel Sambuc
main()28*433d6423SLionel Sambuc int main()
29*433d6423SLionel Sambuc {
30*433d6423SLionel Sambuc int k;
31*433d6423SLionel Sambuc
32*433d6423SLionel Sambuc start(4);
33*433d6423SLionel Sambuc
34*433d6423SLionel Sambuc creat(tempfile, 0777);
35*433d6423SLionel Sambuc for (k = 0; k < 20; k++) {
36*433d6423SLionel Sambuc subr();
37*433d6423SLionel Sambuc }
38*433d6423SLionel Sambuc unlink(tempfile);
39*433d6423SLionel Sambuc quit();
40*433d6423SLionel Sambuc return(-1); /* impossible */
41*433d6423SLionel Sambuc }
42*433d6423SLionel Sambuc
subr()43*433d6423SLionel Sambuc void subr()
44*433d6423SLionel Sambuc {
45*433d6423SLionel Sambuc if ( (pid0 = fork()) != 0) {
46*433d6423SLionel Sambuc /* Parent 0 */
47*433d6423SLionel Sambuc if (pid0 < 0) nofork();
48*433d6423SLionel Sambuc if ( (pid1 = fork()) != 0) {
49*433d6423SLionel Sambuc /* Parent 1 */
50*433d6423SLionel Sambuc if (pid1 < 0) nofork();
51*433d6423SLionel Sambuc if ( (pid2 = fork()) != 0) {
52*433d6423SLionel Sambuc /* Parent 2 */
53*433d6423SLionel Sambuc if (pid2 < 0) nofork();
54*433d6423SLionel Sambuc if ( (pid3 = fork()) != 0) {
55*433d6423SLionel Sambuc /* Parent 3 */
56*433d6423SLionel Sambuc if (pid3 < 0) nofork();
57*433d6423SLionel Sambuc for (i = 0; i < 10000; i++);
58*433d6423SLionel Sambuc kill(pid2, SIGKILL);
59*433d6423SLionel Sambuc kill(pid1, SIGKILL);
60*433d6423SLionel Sambuc kill(pid0, SIGKILL);
61*433d6423SLionel Sambuc wait(&s);
62*433d6423SLionel Sambuc wait(&s);
63*433d6423SLionel Sambuc wait(&s);
64*433d6423SLionel Sambuc wait(&s);
65*433d6423SLionel Sambuc } else {
66*433d6423SLionel Sambuc fd = open(tempfile, O_RDONLY);
67*433d6423SLionel Sambuc lseek(fd, 20480L * nextb, 0);
68*433d6423SLionel Sambuc for (i = 0; i < 10; i++) read(fd, buf, 1024);
69*433d6423SLionel Sambuc nextb++;
70*433d6423SLionel Sambuc close(fd);
71*433d6423SLionel Sambuc exit(0);
72*433d6423SLionel Sambuc }
73*433d6423SLionel Sambuc } else {
74*433d6423SLionel Sambuc while (1) getpid();
75*433d6423SLionel Sambuc }
76*433d6423SLionel Sambuc } else {
77*433d6423SLionel Sambuc while (1) getpid();
78*433d6423SLionel Sambuc }
79*433d6423SLionel Sambuc } else {
80*433d6423SLionel Sambuc while (1) getpid();
81*433d6423SLionel Sambuc }
82*433d6423SLionel Sambuc }
83*433d6423SLionel Sambuc
nofork()84*433d6423SLionel Sambuc void nofork()
85*433d6423SLionel Sambuc {
86*433d6423SLionel Sambuc int e = errno;
87*433d6423SLionel Sambuc printf("Fork failed: %s (%d)\n",strerror(e),e);
88*433d6423SLionel Sambuc exit(1);
89*433d6423SLionel Sambuc }
90*433d6423SLionel Sambuc
91