作业社区
探索学习新天地,共享知识资源!
犹豫就会败北~ 的学生作业:
#include #include #include #include #include #include #include void custom_handle_sig(int signum) { printf("[INFO] receive signal: %s\n", strsignal(signum)); } int main() { pid_t pid_1, pid_2; int ret; __sighandler_t sig; sig = signal(SIGUSR2, custom_handle_sig); if (sig == SIG_ERR) { perror("[ERROR] signal() failed."); exit(EXIT_FAILURE); } pid_1 = fork(); if (pid_1 == -1) { perror("[ERROR] fork() failed: "); exit(EXIT_FAILURE); } else if (pid_1 == 0) { printf("child a start. pid: %d\n", getpid()); pause(); printf("child a end.\n"); } else if (pid_1 > 0) { pid_2 = fork(); if (pid_2 == -1) { perror("[ERROR] fork() failed: "); exit(EXIT_FAILURE); } else if (pid_2 == 0) { printf("child b start. pid: %d\n", getpid()); pause(); printf("child B end.\n"); } else if (pid_2 > 0) { printf("fater process .\n"); sleep(3); kill(pid_1, SIGUSR1); kill(pid_2,SIGUSR2); waitpid(pid_1, NULL, 0); waitpid(pid_2, NULL, 0); printf("father process end.\n"); } } return 0; }
+7