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