用系统调用列出所有的信号;(kill –l) 用系统调用signal()让父进程捕捉键盘上来的中断信号(Ctrl-C); 当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止 Child Process 1 is Killed by Parent! Child Process 2 is Killed by Parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent Process is Killed!linux怎么运行
时间: 2023-12-03 17:46:30 浏览: 76
可以使用以下步骤来运行这个程序:
1. 打开一个文本编辑器,将程序代码粘贴进去并保存为一个以 .c 为后缀的文件,比如 signal_example.c。
2. 打开终端窗口,进入保存代码的目录。
3. 输入以下命令来编译程序:
```
gcc -o signal_example signal_example.c
```
这将生成一个可执行文件 signal_example。
4. 输入以下命令来运行程序:
```
./signal_example
```
这将启动程序并开始运行。当你按下 Ctrl-C 时,父进程将捕捉到 SIGINT 信号并向两个子进程发送 SIGTERM 信号,子进程将输出相应的信息并终止。最后,父进程将等待两个子进程终止后输出相应的信息并终止。
相关问题
用系统调用列出所有的信号;(kill –l) 用系统调用signal()让父进程捕捉键盘上来的中断信号(Ctrl-C); 当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止 Child Process 1 is Killed by Parent! Child Process 2 is Killed by Parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent Process is Killed!
下面是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void child_process_1_handler(int signum) {
printf("Child Process 1 is Killed by Parent!\n");
exit(0);
}
void child_process_2_handler(int signum) {
printf("Child Process 2 is Killed by Parent!\n");
exit(0);
}
int main() {
pid_t pid1, pid2;
int status;
// 列出所有信号
system("kill -l");
// 注册信号处理函数
signal(SIGINT, SIG_IGN); // 父进程忽略Ctrl-C
signal(SIGTERM, SIG_DFL); // 父进程默认处理SIGTERM
// 创建子进程1
pid1 = fork();
if (pid1 == 0) {
// 子进程1
signal(SIGINT, child_process_1_handler);
while (1) {
printf("Child Process 1 is running...\n");
sleep(1);
}
} else if (pid1 > 0) {
// 父进程
// 创建子进程2
pid2 = fork();
if (pid2 == 0) {
// 子进程2
signal(SIGINT, child_process_2_handler);
while (1) {
printf("Child Process 2 is running...\n");
sleep(1);
}
} else if (pid2 > 0) {
// 父进程
printf("Parent Process is running...\n");
// 等待子进程结束
waitpid(pid1, &status, 0);
waitpid(pid2, &status, 0);
printf("Parent Process is Killed!\n");
} else {
// 创建子进程2失败
perror("fork");
exit(1);
}
} else {
// 创建子进程1失败
perror("fork");
exit(1);
}
return 0;
}
```
该程序首先使用 `system` 函数调用 `kill -l` 来列出所有的信号。然后创建两个子进程,并为它们分别注册信号处理函数。父进程忽略 `SIGINT` 信号,而子进程1和子进程2分别处理 `SIGINT` 信号,输出相应的信息并调用 `exit` 终止进程。
父进程等待两个子进程结束后,输出相应的信息并终止进程。注意需要使用 `waitpid` 函数等待子进程结束,防止出现僵尸进程。
用系统调用列出所有的信号;(kill –l) 用系统调用signal()让父进程捕捉键盘上来的中断信号(Ctrl-C); 当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止 Child Process 1 is Killed by Parent! Child Process 2 is Killed by Parent! 父进程等待两个子进程终止后,输出如下的信息后终止: Parent Process is Killed!
下面是一个示例程序,实现了上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void child1_handler(int sig) {
printf("Child Process 1 is Killed by Parent!\n");
exit(0);
}
void child2_handler(int sig) {
printf("Child Process 2 is Killed by Parent!\n");
exit(0);
}
int main() {
pid_t pid1, pid2;
signal(SIGINT, SIG_IGN); // 忽略 Ctrl+C 中断信号
signal(SIGTERM, SIG_DFL); // 恢复默认的终止信号处理程序
// 创建子进程1
pid1 = fork();
if (pid1 < 0) {
perror("fork");
exit(1);
} else if (pid1 == 0) { // 子进程1
signal(SIGTERM, child1_handler); // 设置信号处理函数
while (1) {
printf("Child Process 1 is running...\n");
sleep(1);
}
} else { // 父进程
// 创建子进程2
pid2 = fork();
if (pid2 < 0) {
perror("fork");
exit(1);
} else if (pid2 == 0) { // 子进程2
signal(SIGTERM, child2_handler); // 设置信号处理函数
while (1) {
printf("Child Process 2 is running...\n");
sleep(1);
}
} else { // 父进程
// 等待子进程退出
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
printf("Parent Process is Killed!\n");
exit(0);
}
}
}
```
该程序使用了 `signal()` 系统调用来设置信号处理函数。在父进程中,忽略了 Ctrl+C 中断信号;在子进程中,分别设置了捕捉到 SIGTERM 信号后的处理函数。在父进程中,使用了 `waitpid()` 等待子进程退出,并在两个子进程都退出后输出相应的信息。
阅读全文