首页 > 代码库 > UNIX环境高级编程8.5exit 8.6wait waitpid
UNIX环境高级编程8.5exit 8.6wait waitpid
#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>intmain(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 wait1shell1: shell1.c g++ -g -Wall shell1.c ../lib/libapue.a -I ../include -o shell1shell2: shell2.c g++ -g -Wall shell2.c ../lib/libapue.a -I ../include -o shell2fork1: fork1.c g++ -g -Wall fork1.c ../lib/libapue.a -I ../include -o fork1vfork1: vfork1.c g++ -g -Wall vfork1.c ../lib/libapue.a -I ../include -o vfork1wait1: wait1.c g++ -g -Wall wait1.c ../lib/libapue.a -I ../include -o wait1clean: 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 fork2shell1: shell1.c g++ -g -Wall shell1.c ../lib/libapue.a -I ../include -o shell1shell2: shell2.c g++ -g -Wall shell2.c ../lib/libapue.a -I ../include -o shell2fork1: fork1.c g++ -g -Wall fork1.c ../lib/libapue.a -I ../include -o fork1fork2: fork2.c g++ -g -Wall fork2.c ../lib/libapue.a -I ../include -o fork2vfork1: vfork1.c g++ -g -Wall vfork1.c ../lib/libapue.a -I ../include -o vfork1wait1: wait1.c g++ -g -Wall wait1.c ../lib/libapue.a -I ../include -o wait1clean: rm shell1 shell2 fork1 vfork1 wait1 fork2
UNIX环境高级编程8.5exit 8.6wait waitpid
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。