#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> void waiting(), stop(); int wait_mark; int main() { int p1, p2; signal(SIGINT, stop); while ((p1 = fork()) == -1); if (p1 > 0) { ① while ((p2 = fork()) == -1); if (p2 > 0) { ② wait_mark = 1; waiting(0); kill(p1, 10); kill(p2, 12); wait(); wait(); printf("Parent process is killed!\n"); exit(0); } else { wait_mark = 1; signal(12, stop); waiting(); lockf(1, 1, 0); printf("Child process 2 is killed by parent!\n"); lockf(1, 0, 0); exit(0); } } else { wait_mark = 1; signal(10, stop); waiting(); lockf(1, 1, 0); printf("Child process 1 is killed by parent!\n"); lockf(1, 0, 0); exit(0); } } void waiting() { while (wait_mark != 0); } void stop() { wait_mark = 0; }如果把signal(SIGINT, stop)放在程序中的①号和②号位置(见实验指导书中参考程序),结果会怎样?并分析原因。
时间: 2024-02-14 17:33:44 浏览: 42
如果将 `signal(SIGINT, stop)` 放在程序中的 `①` 号和 `②` 号位置,那么程序的输出结果会发生变化。
具体而言,在 `①` 号位置放置 `signal(SIGINT, stop)`,在 `②` 号位置不放置。这意味着父进程在创建子进程 `p1` 之后就会安装信号处理函数,而在创建子进程 `p2` 之前不会安装信号处理函数。因此,子进程 `p1` 在接收到信号后可以正确地停止等待,但子进程 `p2` 将不会响应相应的信号。
在 `②` 号位置放置 `signal(SIGINT, stop)`,在 `①` 号位置不放置。这意味着父进程在创建子进程 `p1` 之后不会安装信号处理函数,而在创建子进程 `p2` 之前会安装信号处理函数。因此,子进程 `p2` 在接收到信号后可以正确地停止等待,但子进程 `p1` 将不会响应相应的信号。
综上所述,为了保证程序的正确性,应该在子进程创建之前就安装信号处理函数,即在 `while` 循环之前安装。
相关问题
解释#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> void waiting(), stop(); int wait_mark; int main() { int p1, p2; signal(SIGINT, stop); while ((p1 = fork()) == -1); if (p1 > 0) { while ((p2 = fork()) == -1); if (p2 > 0) { wait_mark = 1; waiting(0); kill(p1, 10); kill(p2, 12); wait(); wait(); printf("Parent process is killed!\n"); exit(0); } else { wait_mark = 1; signal(12, stop); waiting(); lockf(1, 1, 0); printf("Child process 2 is killed by parent!\n"); lockf(1, 0, 0); exit(0); } } else { wait_mark = 1; signal(10, stop); waiting(); lockf(1, 1, 0); printf("Child process 1 is killed by parent!\n"); lockf(1, 0, 0); exit(0); } } void waiting() { while (wait_mark != 0); } void stop() { wait_mark = 0; }
这是一个使用信号处理和进程控制的简单的父子进程通信程序。程序中包含了四个头文件:stdio.h、signal.h、unistd.h和stdlib.h。
程序中定义了两个函数waiting()和stop(),分别用于进程等待和停止等待。同时定义了一个全局变量wait_mark,用于标记进程是否等待。
在主函数中,使用了fork()函数创建了两个子进程p1和p2。如果fork()返回值大于0,则说明当前进程为父进程,否则为子进程。父进程使用waiting()函数等待子进程完成,然后向子进程发送信号。子进程使用signal()函数注册信号处理函数,当接收到相应的信号时,执行stop()函数停止等待。
使用lockf()函数实现对输出的互斥,避免多个进程同时向标准输出输出。程序输出父进程和子进程被杀死的信息。最后使用exit()函数退出进程。
需要注意的是,这段代码可能存在一些问题,例如信号处理函数的稳定性和进程控制的正确性等。此外,程序的输出结果可能会因为进程调度顺序的不同而有所差异。
怎么理解:#include<stdio.h> #include<signal.h> #include<unistd.h> #include<stdlib.h> void waiting(),stop(); int wait_mark; main() { int p1,p2; signal(SIGINT,stop); while((p1=fork())== -1); if(p1>0) { while((p2=fork())== -1); if(p2>0) { wait_mark=1; waiting(0); kill(p1,10); kill(p2,12); wait(); wait(); printf("parent proces is killed!\n"); exit(0); } else { wait_mark=1; signal(12,stop); waiting(); lockf(1,1,0); printf("child process 2 is killed by parent!\n"); lockf(1,0,0); exit(0); } } else { wait_mark=1; signal(10,stop); waiting(); lockf(1,1,0); printf("child process 1 is killed by parent!\n"); lockf(1,0,0); exit(0); } } void waiting() { while(wait_mark!=0); } void stop() { wait_mark=0; }
这段代码是一个使用fork函数创建子进程的例子,同时使用了信号通信来实现父子进程之间的协同工作。
在主函数中,程序会包含几个头文件,其中包括signal.h头文件,该头文件中包含了一些与信号相关的函数和宏定义。
接下来,程序会调用signal函数来注册一个SIGINT信号处理函数stop。然后,程序会使用fork函数创建两个子进程p1和p2,如果fork返回-1,说明创建子进程失败,程序会一直循环等待直到创建成功。
如果p1大于0,说明这是父进程,那么程序会继续使用fork函数创建第二个子进程p2,如果p2也大于0,那么说明这还是父进程,程序会将wait_mark赋值为1,并调用waiting函数等待子进程发送信号。然后,程序会使用kill函数发送信号给子进程p1和p2。
如果p1等于0,那么说明这是第一个子进程,程序会将wait_mark赋值为1,并继续执行该子进程的代码。在子进程中,程序会使用signal函数注册一个SIGUSR1信号处理函数stop,然后调用waiting函数等待父进程发送信号。如果接收到父进程发送的SIGUSR1信号,程序会输出一条信息并退出。
如果p2等于0,那么说明这是第二个子进程,程序会将wait_mark赋值为1,并继续执行该子进程的代码。在子进程中,程序会使用signal函数注册一个SIGUSR2信号处理函数stop,然后调用waiting函数等待父进程发送信号。如果接收到父进程发送的SIGUSR2信号,程序会输出一条信息并退出。
在等待子进程发送信号过程中,如果接收到SIGINT信号,程序会执行stop函数来停止等待。如果接收到子进程发送的信号,程序会输出一条信息并退出。最后,程序会使用lockf函数来实现对输出的互斥,避免多个进程同时输出导致信息混乱。
总体来说,这段代码主要是演示了如何使用fork函数创建子进程,并在父子进程之间通过信号通信来实现协同工作。同时,程序还使用了锁来实现对输出的互斥,确保输出的正确性。
相关推荐
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)