-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathspawn.c
40 lines (37 loc) · 1010 Bytes
/
spawn.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int
main(int argc, char *argv[])
{
pid_t pid;
if (argc != 3) {
fprintf(stderr, "Usage: %s <command> <arg>\n", argv[0]);
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork(2) failed\n");
exit(1);
}
if (pid == 0) { /* 子プロセス */
execl(argv[1], argv[1], argv[2], NULL);
/* execl()は成功したら戻らないので、戻ったらすべて失敗 */
perror(argv[1]);
exit(99);
}
else { /* 親プロセス */
int status;
waitpid(pid, &status, 0);
printf("child (PID=%d) finished; ", pid);
if (WIFEXITED(status))
printf("exit, status=%d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("signal, sig=%d\n", WTERMSIG(status));
else
printf("abnormal exit\n");
exit(0);
}
}