-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_waitpid.c
54 lines (50 loc) · 1.25 KB
/
Test_waitpid.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
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int ret, i, status;
char *child_argv[16] = {0};
pid_t pid;
if (argc < 2)
{
fprintf(stderr, "Usage:%s <exe_path> <args...>", argv[0]);
return -1;
}
for (i = 1; i < argc; ++i)
{
child_argv[i-1] = (char *)calloc(1, strlen(argv[i])+1);
memcpy(child_argv[i-1], argv[i], strlen(argv[i]));
}
while(1)
{
pid = fork();
if (pid == -1)
{
fprintf(stderr, "fork() error.errno:%d error:%s", errno, strerror(errno));
break;
}
if (pid == 0)
{
ret = execv(child_argv[0], (char **)child_argv);
if (ret < 0)
{
fprintf(stderr, "execv ret:%d errno:%d error:%s", ret, errno, strerror(errno));
continue;
}
exit(0);
}
if (pid > 0)
{
pid = wait(&status);
fprintf(stdout, "Child process id: %d, status:%d\n", pid, status);
//todo:´Ë´¦°´Õչ淶дÎļþ
}
}/* endless loop here */
return 0;
}