鱼C论坛

 找回密码
 立即注册
查看: 1275|回复: 0

有关Linux 下多进程与信号的问题,求助大神!之前发的贴子,一直没有解决.

[复制链接]
发表于 2021-12-2 20:02:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
输入ctrl + c ,程序打印了一部分没有执行, 设p2 = 0 while 循环没有退出
运行环境 ubantu 编译器 g++
  1. #include <unistd.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <signal.h>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>

  7. using namespace std;

  8. pid_t childPid_1;
  9. pid_t childPid_2;
  10. int i = 1;
  11. bool j = true;

  12. /*处理信号函数*/
  13. void cap(int signum);
  14. void process_cap1 (int signum);
  15. void process_cap2 (int signum);

  16. int main(int argc,char *arg[])
  17. {
  18.       
  19.         int pfd[2];
  20.         char buf[1024];
  21.         string send1 = "I send you ";
  22.         string send2 = " time";
  23.         string send;
  24.         /*创建匿名管道*/
  25.         if(pipe(pfd) == -1){
  26.                 cout << "创建失败" << endl;
  27.         } else {
  28.          cout << "创建管道成功" << endl;
  29.         }
  30.       
  31.         childPid_1 = fork ();

  32.         if (childPid_1 < 0){
  33.                 cout << "创建失败" << endl;
  34.         } else {
  35.                 if (childPid_1 == 0){
  36.                 //忽略SIGINT
  37.                 signal (SIGINT,SIG_IGN);
  38.                 cout << "创建子进程——1成功  " ;
  39.                 childPid_1 = getpid ();
  40.                 cout << "ppid  "
  41.                      << getppid()  
  42.                      << "  pid  "
  43.                      << getpid()
  44.                      << endl;
  45.                 while (i){
  46.                         /*执行子进程-1操作**/
  47.                         send = send1+to_string(i);
  48.                         send += send2;
  49.                         const char* s = send.c_str();
  50.                         i++;
  51.                         write(pfd[1],s,1024);
  52.                         /* 每发送一次 休眠一秒*/
  53.                         sleep(1);
  54.                         //捕捉父进程的信号,退出循环
  55.                         if (signal(SIGUSR1,process_cap1) == SIG_ERR){
  56.                                  cout << "error";
  57.                         }
  58.                        
  59.                        
  60.                 }
  61.                        
  62.                        

  63.                         cout << "Child Process 1 is killed by Parent" <<
  64.                         endl;
  65.                         exit(0);
  66.         } else {
  67.                 //sleep(5);
  68.                 /*执行父进程创建子进程-2操作*/
  69.                 childPid_2 = fork();
  70.                 if (childPid_2 < 0){
  71.                         cout <<  "创建失败" << endl;
  72.                 } else if (childPid_2 == 0){
  73.                         //忽略SIGINT
  74.                          signal (SIGINT,SIG_IGN);
  75.                               
  76.                         cout << "创建子进程——2成功" ;
  77.                         childPid_2 = getpid();
  78.                         cout << "  ppid  "
  79.                              << getppid()
  80.                              << "  pid  "
  81.                              << getpid()
  82.                              << endl;
  83.                         /*执行子进程-2操作**/
  84.                         while (j){
  85.                                 read(pfd[0],buf,1024);
  86.                                 cout << buf << endl;      
  87.                                 //捕捉父进程信号
  88.                                 if (signal (SIGUSR2,process_cap2) == SIG_ERR)  {
  89.                                         cout << "error";
  90.                                 }
  91.                                 if (j == false){
  92.                                         break;
  93.                                 }
  94.                                        
  95.                         }                              

  96.                         cout << "Child Process 2 is killed by Parent!" <<  endl;
  97.                         exit (0);
  98.                        

  99.                 } else {
  100.                         /*执行来自用户的操作 ctrl+c */
  101.                          signal (SIGINT,cap);
  102.                        
  103.                         while (true);

  104.                 }
  105.         }
  106.         }
  107.       
  108. }

  109. void cap (int signum)
  110. {
  111.         cout << getpid()
  112.              << "进程"
  113.              << "捕捉到SIGINT信号"
  114.              << endl;
  115.         kill (childPid_1,SIGUSR1);
  116.         waitpid (childPid_1,NULL,0);

  117.         kill (childPid_2,SIGUSR2);
  118.         waitpid (childPid_2,NULL,0);
  119.       
  120.                        
  121.         cout << "Parent Process is killed";
  122.         signal(SIGINT,SIG_DFL);
  123. }

  124. void process_cap1 (int signum){
  125.                
  126.         cout << getpid()
  127.              << "进程"
  128.              << "捕捉到SIGUSR1信号"
  129.              << endl;
  130.         //将 i 设置为负数 退出while
  131.         i = -1;
  132. }

  133. void process_cap2 (int signum){
  134.         cout << j;
  135.         j = false;
  136.         cout << getpid()
  137.              << "进程"
  138.              << "捕捉到SIGUSR2信号"
  139.              << endl;
  140.         cout << j;
  141.         return ;
  142. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 08:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表