标签:
#include "apue.h"
#include <sys/wait.h>
void pr_exit(int status)
{
if (WIFEXITED(status))
printf("normal termination, exit status = %d\n",
WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination, signal number = %d%s\n",
WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status) ? " (core file generated)" : "");
#else
"");
#endif
else if (WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
exit(7);
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
abort(); /* generates SIGABRT */
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
status /= 0; /* divide by 0 generates SIGFPE */
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
exit(0);
}
all: shell1 shell2 fork1 vfork1 wait1 shell1: shell1.c g++ -g -Wall shell1.c ../lib/libapue.a -I ../include -o shell1 shell2: shell2.c g++ -g -Wall shell2.c ../lib/libapue.a -I ../include -o shell2 fork1: fork1.c g++ -g -Wall fork1.c ../lib/libapue.a -I ../include -o fork1 vfork1: vfork1.c g++ -g -Wall vfork1.c ../lib/libapue.a -I ../include -o vfork1 wait1: wait1.c g++ -g -Wall wait1.c ../lib/libapue.a -I ../include -o wait1 clean: rm shell1 shell2 fork1 vfork1 wait1
调用fork两次以避免僵死进程
#include "apue.h"
#include <sys/wait.h>
int main(void)
{
printf("file %s, line %d parent, getpid() = %d\n", __FILE__, __LINE__, getpid());
fflush(stdout);
pid_t pid;
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
else if (pid == 0)
{ /* first child */
printf("file %s, line %d child 1, getpid() = %d\n", __FILE__, __LINE__, getpid());
fflush(stdout);
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
else if (pid > 0)
{
printf("first child, going to sleep(2)\n");
sleep(2);
printf("first child, after sleep(2), begin exiting\n");
exit(0); /* parent from second fork == first child */
}
/*
* We‘re the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here‘s where we‘d continue executing, knowing that when
* we‘re done, init will reap our status.
*/
printf("file %s, line %d, child 2, getpid() = %d\n", __FILE__, __LINE__, getpid());
printf("second child, going to sleep(5)\n");
sleep(5);
printf("second child, after sleep(5)\n");
printf("second child, parent pid = %d, parent id is 1, which means my parent process is init\n", getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /* wait for first child */
err_sys("waitpid error");
else
printf("after sleep(2), first child exit caught by parent (waitpid)\n");
printf("file %s, line %d, getpid() = %d\n", __FILE__, __LINE__, getpid());
/*
* We‘re the parent (the original process); we continue executing,
* knowing that we‘re not the parent of the second child.
*/
exit(0);
}
all: shell1 shell2 fork1 vfork1 wait1 fork2 shell1: shell1.c g++ -g -Wall shell1.c ../lib/libapue.a -I ../include -o shell1 shell2: shell2.c g++ -g -Wall shell2.c ../lib/libapue.a -I ../include -o shell2 fork1: fork1.c g++ -g -Wall fork1.c ../lib/libapue.a -I ../include -o fork1 fork2: fork2.c g++ -g -Wall fork2.c ../lib/libapue.a -I ../include -o fork2 vfork1: vfork1.c g++ -g -Wall vfork1.c ../lib/libapue.a -I ../include -o vfork1 wait1: wait1.c g++ -g -Wall wait1.c ../lib/libapue.a -I ../include -o wait1 clean: rm shell1 shell2 fork1 vfork1 wait1 fork2
UNIX环境高级编程8.5exit 8.6wait waitpid
标签:
原文地址:http://www.cnblogs.com/sunyongjie1984/p/4262982.html