📄 test8.c
字号:
#include <stdio.h>#include <sys/types.h>#include <errno.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <sys/wait.h>static void child_process_main(void);void restart(void);void monitoring(pid_t pid);int main(int argc, char **argv){ pid_t pid; if ((pid = fork()) < 0) { fprintf(stderr, "fork failed: %s\n", strerror(errno)); exit(1); } else if (pid == 0) { // Child process child_process_main(); exit(0); //abort(); } else { monitoring(pid); } return 0;}static void child_process_main(void){ printf("I'm child process, pid = %d, sleep 50 seconds.\n", getpid()); sleep(50); printf("child process is waked up.\n");}void monitoring(pid_t pid){ int status; pid_t returned_pid; // XXX: parent process will be blocked until child process exited. // pid_t wait(int *stat_loc); returned_pid = wait(&status); if (returned_pid != pid) { printf("wait() returned pid is not equal fork() returned pid.\n"); } printf("child %d is exited, status = %d.\n", returned_pid, status); if (WIFEXITED(status)) { printf("child process is normal termination, status = %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("child process is abnormal termination, status = %d.\n", WTERMSIG(status));#ifdef WCOREDUMP if (WCOREDUMP(status)) { printf("core dump file generated.\n"); } else { printf("no core dump file generated.\n"); }#endif } else { printf("child process is paused.\n"); } // parent process printf("I'm parent process, continue.\n"); restart();}void restart(void){ printf("Restarting child process ...\n"); pid_t pid; if ((pid = fork()) < 0) { // FIXME: process this error. } else if (pid == 0) { child_process_main(); exit(0); } else { monitoring(pid); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -