-
Notifications
You must be signed in to change notification settings - Fork 8
/
fork.c
49 lines (43 loc) · 825 Bytes
/
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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
void run(const char *program, const char* arg, char *const* env)
{
#ifdef DEBUG
printf("program: %s, param: %s\n", program, arg);
#endif
execle(program, arg, env);
perror("exec");
_exit(10);
}
int main(int argc, char *const*argv, char *const* env)
{
argc--; argv++;
if (!argc)
{
fprintf(stderr, "Number of parameters must be at least 2!\n");
return 1;
}
if (((argc >> 1) << 1) != argc)
{
fprintf(stderr, "Number of parameters is odd!\n");
return 2;
}
for (; argc > 2; argc -= 2)
{
switch (vfork())
{
case -1:
perror("vfork error");
break;
case 0:
break;
default:
run(argv[0], argv[1], env);
}
argv += 2;
}
puts("xxx");
run(argv[0], argv[1], env);
return 10;
}