make minix lwip make explicit use of 'int'
[minix3.git] / test / test52.c
blob1ac0e2f6aae998ae6c5452e859931cb6fbc7ee42
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include <math.h>
9 #define ROUNDS 20
10 #define SWAPS 40
11 #define MAX_ERROR 5
13 #include "common.c"
15 int pipefdc[2];
16 int pipefdp[2];
17 int subtest = 0;
18 int child_is_dead = 0;
20 void dead_child(int n);
21 void do_child(void);
22 void do_parent(void);
23 void do_calcs(void);
24 void err(int n);
25 void quit(void);
27 void err(int n)
29 e(n);
32 void do_calcs(void)
34 float a, b, c, d, e;
35 float foo, bar;
36 int i;
37 subtest = 3;
39 a = 1.1;
40 b = 2.2;
41 c = 3.3;
42 d = 4.4;
43 e = 5.5;
45 foo = a * b; /* 2.42 */
46 bar = c * d; /* 14.52 */
48 i = 0;
49 while(i < ROUNDS) {
50 foo += c; /* 5.72 */
51 foo *= d; /* 25.168 */
52 foo /= e; /* 4.5760 */
53 bar -= a; /* 13.42 */
54 bar *= b; /* 29.524 */
55 bar /= e; /* 5.3680 */
57 /* Undo */
58 foo *= e;
59 foo /= d;
60 foo -= c;
62 bar *= e;
63 bar /= b;
64 bar += a;
66 i++;
69 if (fabs(foo - (a * b)) > 0.0001) err(1);
70 if (fabs(bar - (c * d)) > 0.0001) err(2);
73 void dead_child(int n)
75 int status;
76 subtest = 4;
78 (void) n; /* Avoid warning about unused parameter */
80 if (wait(&status) == -1) err(1);
82 if (!WIFEXITED(status)) {
83 err(2);
84 quit();
85 } else {
86 errct += WEXITSTATUS(status);
87 child_is_dead = 1;
91 void do_child(void)
93 char buf[2];
94 int s;
96 s = 0;
97 close(pipefdp[0]);
98 close(pipefdc[1]);
100 while(s < SWAPS) {
101 do_calcs();
103 /* Wake up parent */
104 write(pipefdp[1], buf, 1);
106 /* Wait for parent to wake me up */
107 read(pipefdc[0], buf, 1);
109 s++;
111 exit(0);
114 void do_parent(void)
116 int s;
117 char buf[2];
118 struct sigaction sa;
119 subtest = 2;
121 sa.sa_handler = dead_child;
122 sa.sa_flags = 0;
123 if (sigaction(SIGCHLD, &sa, NULL) == -1) err(1);
125 s = 0;
126 close(pipefdp[1]);
127 close(pipefdc[0]);
129 while(s < SWAPS) {
130 /* Wait for child to wake me up */
131 read(pipefdp[0], buf, 1);
133 do_calcs();
135 /* Wake up child */
136 write(pipefdc[1], buf, 1);
137 s++;
140 while(child_is_dead == 0) { fflush(stdout); } /* Busy wait */
142 quit();
145 int main(void)
147 pid_t r;
148 subtest = 1;
150 start(52);
152 if (pipe(pipefdc) == -1) err(1);
153 if (pipe(pipefdp) == -1) err(2);
155 r = fork();
156 if(r < 0) {
157 err(3);
158 } else if(r == 0) {
159 /* Child */
160 do_child();
161 } else {
162 /* Parent */
163 do_parent();
166 return(0); /* Never reached */