-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.c
207 lines (171 loc) · 3.63 KB
/
fork.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static bool loop = true;
static void *
dummy(void *arg)
{
return arg;
}
static size_t
threading(void)
{
size_t counter;
pthread_t thread;
int ret;
for (counter = 0; loop; counter++) {
if ((ret = pthread_create(&thread, NULL, dummy, NULL)) != 0)
err(EXIT_FAILURE, "pthread_create: %d", ret);
if ((ret = pthread_join(thread, NULL)) != 0)
err(EXIT_FAILURE, "pthread_join: %d", ret);
}
return counter;
}
static size_t
forking(bool exec_enoent, char *argv[])
{
size_t counter;
int status;
for (counter = 0; loop; counter++) {
switch (fork()) {
case -1:
err(EXIT_FAILURE, "fork");
case 0:
if (argv[0] != NULL) {
execv(argv[0], argv);
if (!exec_enoent || errno != ENOENT)
err(EXIT_FAILURE, "execv");
}
_exit(EXIT_SUCCESS);
default:
again:
if (wait(&status) == -1) {
if (errno == EINTR)
goto again;
err(EXIT_FAILURE, "wait");
}
break;
}
}
return counter;
}
static size_t
sys_getpid(void)
{
size_t counter;
for (counter = 0; loop; counter++)
getpid();
return counter;
}
static void
signal_handler(int sig)
{
if (sig == SIGALRM)
loop = false;
}
static void
usage(void)
{
fputs("fork [-E] [-j jobs] [-s sec] thread\n", stderr);
fputs("fork [-E] [-j jobs] [-s sec] fork [cmd [args [...]]]\n", stderr);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
size_t counter = 0;
const char *errstr;
unsigned int seconds = 5;
int ch;
bool exec_enoent = false;
int ncpu = 1;
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
if (argc == 1)
_exit(0);
if (setenv("POSIXLY_CORRECT", "1", 1) == -1)
err(EXIT_FAILURE, "setenv");
/* parameter handling */
while ((ch = getopt(argc, argv, "Ej:s:")) != -1) {
switch (ch) {
case 'E':
exec_enoent = true;
break;
case 'j':
ncpu = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "strtonum: %s", errstr);
break;
case 's':
seconds = strtonum(optarg, 1, UINT_MAX, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "strtonum: %s", errstr);
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
char *test = argv[0];
argc--;
argv++;
/* signal handling */
signal(SIGALRM, signal_handler);
int fd[ncpu][2];
for (int i = 0; i < ncpu; i++) {
if (pipe(fd[i]) == -1)
err(EXIT_FAILURE, "pipe");
switch (fork()) {
case -1:
err(EXIT_FAILURE, "fork");
case 0: /* child */
/* close the reading side */
if (close(fd[i][0]) == -1)
err(EXIT_FAILURE, "close");
/* set timer */
if (alarm(seconds) == (unsigned int)-1)
err(EXIT_FAILURE, "alarm");
/* doing */
if (strcmp(test, "thread") == 0)
counter = threading();
else if (strcmp(test, "fork") == 0)
counter = forking(exec_enoent, argv);
else if (strcmp(test, "getpid") == 0)
counter = sys_getpid();
else
usage();
/* write results to master */
if (write(fd[i][1], &counter, sizeof counter) !=
sizeof counter)
err(EXIT_FAILURE, "write");
exit(EXIT_SUCCESS);
break;
default: /* parent */
/* close the writing side */
if (close(fd[i][1]) == -1)
err(EXIT_FAILURE, "close");
}
}
/* collect the results */
for (int i = 0; i < ncpu; i++) {
size_t c;
if (read(fd[i][0], &c, sizeof c) != sizeof c)
err(EXIT_FAILURE, "read");
counter += c;
}
if (strcmp(test, "fork") == 0 && argv[0] != NULL)
test = argv[0];
printf("%6zu %s\n", counter / seconds, test);
return EXIT_SUCCESS;
}