下面是一個實例,來演示不通的exit值
[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void pt_exit(int status)
{
if(WIFEXITED(status))
printf("nomal termination, exit status = %d\n",
WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("abnomal termination, signal number = %d%s\n",
WTERMSIG(status), "");
else if(WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
int main(void)
{
pid_t pid;
int status;
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
exit(7);
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
abort();
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
status /= 0;
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void pt_exit(int status)
{
if(WIFEXITED(status))
printf("nomal termination, exit status = %d\n",
WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("abnomal termination, signal number = %d%s\n",
WTERMSIG(status), "");
else if(WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
int main(void)
{
pid_t pid;
int status;
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
exit(7);
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
abort();
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
if((pid = fork()) < 0)
printf("error: fork error.\n");
else if(pid == 0)
status /= 0;www.2cto.com
if(wait(&status) != pid)
printf("error: wait error.\n");
pt_exit(status);
exit(0);
}