Linux C gets the implementation code for the process exit value
- 2020-04-02 00:43:52
- OfStack
As shown in the following code:
Reference: Man 2 "wait"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
pid_t pid;
int stat;
int exit_code;
pid = fork();
if(pid == 0)
{
sleep(3);
exit(5);
}
else if( pid < 0 )
{
fprintf(stderr, "fork failed: %s", strerror(errno));
return -1;
}
wait(&stat); //Wait for a child process to finish
if(WIFEXITED(stat)) //WIFEXITED() returns true if the child process ends normally by return, exit, _exit
{
exit_code = WEXITSTATUS(stat);
printf("child's exit_code: %dn", exit_code);
}
return 0;
}
Reference: Man 2 "wait"