[英]is possible to get the exit status from the child of a child process in the parent
[英]Obtain this exit status of a child process and from the parent process and print it in the parent.
我正在探索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()
來完成您所追求的目標。 您想等待孩子死掉-不要使用“如果孩子還沒死就不要等待孩子死”的選項。 而且最好只在父母那里等待。 該代碼有時還會用中斷信號殺死孩子,主要是讓您了解如何對退出狀態進行編碼。
#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
_______________________________________
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.