-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_perf.c
More file actions
49 lines (43 loc) · 962 Bytes
/
fork_perf.c
File metadata and controls
49 lines (43 loc) · 962 Bytes
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
/*
* This program is for measuring the difference in speed between fork() and vfork() calls.
* On openBSD, the times appear to be the same... vfork implemented with plain fork?
*/
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NFORK 50000
int main(int argc, char **argv) {
int i, status;
time_t start;
void *stuff;
stuff = malloc(10000000);
start = time(NULL);
for (i = 0; i < NFORK; i++) {
switch (fork()) {
case -1:
puts("fork");
exit(EXIT_FAILURE);
case 0:
_exit(0);
default:
wait(&status);
}
}
printf("Took %lld seconds to fork %d processes\n", time(NULL) - start, NFORK);
start = time(NULL);
for (i = 0; i < NFORK; i++) {
switch (vfork()) {
case -1:
puts("fork");
exit(EXIT_FAILURE);
case 0:
_exit(0);
default:
wait(&status);
}
}
printf("Took %lld seconds to vfork %d processes\n", time(NULL) - start, NFORK);
exit(EXIT_SUCCESS);
}