-
Notifications
You must be signed in to change notification settings - Fork 1
/
fork-me.c
74 lines (65 loc) · 1.35 KB
/
fork-me.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Synopsis:
* Simple measurement of sequential fork/execs.
* Usage:
* time fork-me /usr/bin/true
* Exit Status:
* 0 ok
* 1 error
* See:
* https://github.com/jmscott/work
* Note:
* Need to require number of forks/execs from command line.
* Currently value is static 100000.
*/
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
int i = 0;
pid_t pid;
int status;
char *path;
static char blurb[] = "fork-me: starting 100000 fork/execs ...\n";
static char done[] = "fork-me: done\n";
if (argc != 2) {
static char badargc[] = "ERROR: wrong number of arguments\n";
write(2, badargc, sizeof badargc - 1);
_exit(1);
}
write(1, blurb, sizeof blurb - 1);
path = argv[1];
while (i++ < 100000) {
pid = fork();
if (pid == 0) {
execl(path, path, NULL);
perror(path);
_exit(1);
}
if (pid < 0) {
perror("fork() failed");
_exit(1);
}
if (wait(&status) < 0) {
perror("wait() failed");
_exit(1);
}
if (WIFEXITED(status)) {
if (status != 0) {
static char notzero[] =
"ERROR: child exit status not 0\n";
write(2, notzero, sizeof notzero - 1);
_exit(1);
}
} else {
static char badkid[] =
"ERROR: child process did not exit normally\n";
write(2, badkid, sizeof badkid - 1);
_exit(1);
}
}
write(1, done, sizeof done - 1);
return 0;
}