我正在探索 c 中的流程。我想更新以下程序,使子程序以退出状态(例如 10)终止。从父进程获取此退出状态并在父进程中打印它。我尝试了以下方法,但从家长那里得到的状态是错误的。我哪里错了?请帮助我。提前致谢。
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pid;
int ppid;
int status;
pid=vfork();
pid_t return_pid = waitpid(pid, &status, WNOHANG);
//printf("%d\n", pid); --> value = 0
if (pid < 0) {
printf("\n Error ");
exit(1);
} else if (pid==0) {
printf("\n Hello I am the child process\n");
printf("\n My pid is: %d\n", getpid());
printf("\n My parent pid is: %d\n", getppid());
printf("\n My return_pid is: %d\n", return_pid);
printf("_______________________________________\n");
int es = WEXITSTATUS(status);
printf ("\n Child exit status: %d\n", es);
status = es;
exit(0);
} else {
printf("The child has terminated!\n");
printf("_______________________________________\n");
printf("\n Hello I am the parent process\n");
printf("\n My actual pid is %d\n" , getpid());
printf("\n My parent pid is: %d\n", getppid());
printf("\n My return_pid is: %d\n", return_pid);
printf("_______________________________________\n");
printf ("\n Child's Exit staus from parent: %d\n", status);
exit(1);
}
}
最佳答案
此代码使用 fork()
而不是 vfork()
,其功能与您所追求的接近。您想等待 child 死亡 - 不要使用“如果 child 尚未死亡,则不要等待 child 死亡”的选项。而且最好只在父进程中等待。这段代码有时也会用中断信号杀死子进程——主要是为了让你可以看到退出状态是如何编码的。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
pid_t pid = fork();
if (pid < 0)
{
perror("fork()");
exit(1);
}
else if (pid == 0)
{
printf("Hello I am the child process\n");
printf("My pid is: %d\n", (int)getpid());
printf("My parent pid is: %d\n", (int)getppid());
int status = getpid() % 256;
printf("I am about to exit with status 0x%.2X (%d)\n", status, status);
printf("_______________________________________\n");
exit(status);
}
else
{
int status;
if (pid % 10 == 0)
kill(pid, SIGINT);
pid_t return_pid = waitpid(pid, &status, 0);
printf("The child has terminated!\n");
printf("Hello I am the parent process\n");
printf("My actual pid is %d\n", (int)getpid());
printf("My parent pid is: %d\n", (int)getppid());
printf("My return_pid is: %d\n", (int)return_pid);
printf("Child's raw exit status: 0x%.4X (%d)\n", status, status);
if (WIFEXITED(status))
printf("Child's exit status: 0x%.2X (%d)\n",
WEXITSTATUS(status), WEXITSTATUS(status));
if (WIFSIGNALED(status))
printf("Child exited because of signal %d\n", WTERMSIG(status));
printf("_______________________________________\n");
}
return 0;
}
示例输出:
正常运行:
Hello I am the child process
My pid is: 40377
My parent pid is: 40376
I am about to exit with status 0xB9 (185)
_______________________________________
The child has terminated!
Hello I am the parent process
My actual pid is 40376
My parent pid is: 847
My return_pid is: 40377
Child's raw exit status: 0xB900 (47360)
Child's exit status: 0xB9 (185)
_______________________________________
信号运行:
The child has terminated!
Hello I am the parent process
My actual pid is 40379
My parent pid is: 847
My return_pid is: 40380
Child's raw exit status: 0x0002 (2)
Child exited because of signal 2
_______________________________________
关于c - 从父进程获取子进程的退出状态并将其打印在父进程中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165763/