/*
<<高级Linux编程.pdf>> page46
wait系统调用: 宏WIFEXITED和WEXITSTATUS的定义一般在/usr/include/sys/wait.h
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h> // 包含对宏WIFEXITED和WEXITSTATUS的定义
int spawn(char *program, char **arg_list)
{
pid_t child_pid;
child_pid = fork();
if(child_pid != 0) // father process
{
printf("here is the father process\n");
return child_pid;
}
else // child process
{
execvp(program, arg_list);
printf("here is the child process\n");
fprintf(stderr, "Error: %s\n", strerror(errno));
abort();
}
}
int main(int argc, char *argv[])
{
int child_status;
char* arg_list[] = {"ls", "-l", "/home/zcm", NULL};
spawn("ls", arg_list);
wait(&child_status);
if(WIFEXITED(child_status))
printf("The child process exited normally, with exit code %d\n", WEXITSTATUS(child_status));
else
printf("The child process exited abnormally\n");
printf("done with main program\n");
return 0;
}
wait系统调用
最新推荐文章于 2024-03-19 13:45:13 发布