comparison src/usr/forktest.c @ 0:83c23a36980d

Init
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 26 May 2017 23:11:05 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:83c23a36980d
1 // Test that fork fails gracefully.
2 // Tiny executable so that the limit can be filling the proc table.
3
4 #include "types.h"
5 #include "stat.h"
6 #include "user.h"
7
8 #define N 1000
9
10 void
11 printf(int fd, char *s, ...)
12 {
13 write(fd, s, strlen(s));
14 }
15
16 void
17 forktest(void)
18 {
19 int n, pid;
20
21 printf(1, "fork test\n");
22
23 for(n=0; n<N; n++){
24 pid = fork();
25 if(pid < 0)
26 {
27 printf(1, "fork failed!\n");
28 break;
29 }
30 if(pid == 0)
31 exit();
32 }
33
34 if(n == N){
35 printf(1, "fork claimed to work N times!\n", N);
36 exit();
37 }
38
39 for(; n > 0; n--){
40 if(wait() < 0){
41 printf(1, "wait stopped early\n");
42 exit();
43 }
44 }
45
46 if(wait() != -1){
47 printf(1, "wait got too many\n");
48 exit();
49 }
50
51 printf(1, "fork test OK\n");
52 }
53
54 int
55 main(void)
56 {
57 forktest();
58 exit();
59 }