|
这个程序的要求是打印出进程推出时的状态。
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdio.h>
- static void pr_exit(int);
- int
- main(void)
- {
- pid_t pid;
- int *status;
- if((pid=fork())<0) { /* fork一个子进程 */
- printf("fork error\n");
- exit(-1);
- }
- else if (pid ==0 ) { /* child process */
- sleep(2);
- if( (waitpid(pid,&status,0)) != pid ) { /* 把进程的信息贮存在status*/
- printf("waitpid error\n");
- exit(-1);
- }
- exit(0); /* KILL 子程序 */
- pr_exit(status); /* 分析status */
- }
- exit(0);
- }
-
- static void
- pr_exit(int status) /* 这个函数的作用是分析status */
- {
- int status;
- if(WIFEXITED(status))
- printf("exit status = %d\n",WEXITSTATUS(status));
- else if(WIFSIGNALED(status))
- printf("exit status = %d%s\n",WTERMSIG(status),
- #ifdef WCOREDUMP
- WCOREDUMP(status) ? "(core file generated)" : "" );
- #else
- "");
- #endif
- else (WIFSTOPED(status))
- printf("child stopped ,signal number = %d\n",
- WSTOPSIG(status));
- }
- # gcc -c wait.wen.c
- wait.wen.c: In function `main':
- wait.wen.c:17: warning: passing arg 2 of `waitpid' from incompatible pointer type
- wait.wen.c:22: warning: passing arg 1 of `pr_exit' makes integer from pointer without a cast
- wait.wen.c: In function `pr_exit':
- wait.wen.c:30: warning: declaration of `status' shadows a parameter
- wait.wen.c:41: parse error before "printf"
复制代码
|
|