source: trunk/minix/test/test4.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.7 KB
Line 
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
12pid_t pid0, pid1, pid2, pid3;
13int s, i, fd, nextb, errct = 0;
14char *tempfile = "test4.temp";
15char buf[1024];
16extern int errno;
17
18_PROTOTYPE(int main, (void));
19_PROTOTYPE(void subr, (void));
20_PROTOTYPE(void nofork, (void));
21_PROTOTYPE(void quit, (void));
22
23int main()
24{
25 int k;
26
27 printf("Test 4 ");
28 fflush(stdout); /* have to flush for child's benefit */
29
30 system("rm -rf DIR_04; mkdir DIR_04");
31 chdir("DIR_04");
32
33 creat(tempfile, 0777);
34 for (k = 0; k < 20; k++) {
35 subr();
36 }
37 unlink(tempfile);
38 quit();
39 return(-1); /* impossible */
40}
41
42void subr()
43{
44 if ( (pid0 = fork()) != 0) {
45 /* Parent 0 */
46 if (pid0 < 0) nofork();
47 if ( (pid1 = fork()) != 0) {
48 /* Parent 1 */
49 if (pid1 < 0) nofork();
50 if ( (pid2 = fork()) != 0) {
51 /* Parent 2 */
52 if (pid2 < 0) nofork();
53 if ( (pid3 = fork()) != 0) {
54 /* Parent 3 */
55 if (pid3 < 0) nofork();
56 for (i = 0; i < 10000; i++);
57 kill(pid2, 9);
58 kill(pid1, 9);
59 kill(pid0, 9);
60 wait(&s);
61 wait(&s);
62 wait(&s);
63 wait(&s);
64 } else {
65 fd = open(tempfile, O_RDONLY);
66 lseek(fd, 20480L * nextb, 0);
67 for (i = 0; i < 10; i++) read(fd, buf, 1024);
68 nextb++;
69 close(fd);
70 exit(0);
71 }
72 } else {
73 while (1) getpid();
74 }
75 } else {
76 while (1) getpid();
77 }
78 } else {
79 while (1) getpid();
80 }
81}
82
83void nofork()
84{
85 int e = errno;
86 printf("Fork failed: %s (%d)\n",strerror(e),e);
87 exit(1);
88}
89
90void quit()
91{
92
93 chdir("..");
94 system("rm -rf DIR*");
95
96 if (errct == 0) {
97 printf("ok\n");
98 exit(0);
99 } else {
100 printf("%d errors\n", errct);
101 exit(1);
102 }
103}
Note: See TracBrowser for help on using the repository browser.