鱼C论坛

 找回密码
 立即注册
查看: 2496|回复: 11

程序 在函数中未退出 红色的地方未执行。

[复制链接]
发表于 2021-11-13 23:30:03 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 liyiqi 于 2021-11-14 09:41 编辑
  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;[i][/i]
  140.       [backcolor=Red]  cout << j;
  141.         return ;[/backcolor]
  142. }
复制代码
2021-11-13 19-50-30 的屏幕截图.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-13 23:31:31 | 显示全部楼层
是最后一个函数的后两条语句
没有标上,不好意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-13 23:43:49 | 显示全部楼层

回帖奖励 +5 鱼币

  1. 重新发一下代码,用代码格式发,像这样
  2. 你要标注代码的话,用图片的形式标
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-13 23:56:16 | 显示全部楼层

不太会用这个论坛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-13 23:58:37 | 显示全部楼层
liyiqi 发表于 2021-11-13 23:56
不太会用这个论坛

Untitled.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-13 23:59:19 | 显示全部楼层
本帖最后由 liyiqi 于 2021-11-14 00:03 编辑
  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
回复 支持 反对

使用道具 举报

发表于 2021-11-14 14:12:19 | 显示全部楼层

回帖奖励 +5 鱼币

帮顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-14 14:30:20 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-14 20:14:29 | 显示全部楼层
回复别人要点 "回复" 按钮,不然别人是收不到通知的,除非别人主动点进来。

  1. #include <iostream>
  2. #include <signal.h>
  3. #include <string>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <unistd.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.     int pfd[2];
  18.     char buf[1024];
  19.     string send1 = "I send you ";
  20.     string send2 = " time";
  21.     string send;
  22.     /*创建匿名管道*/
  23.     if(pipe(pfd) == -1) {
  24.         cout << "创建失败" << endl;
  25.     } else {
  26.         cout << "创建管道成功" << endl;
  27.     }

  28.     childPid_1 = fork();

  29.     if(childPid_1 < 0) {
  30.         cout << "创建失败" << endl;
  31.     } else {
  32.         if(childPid_1 == 0) {
  33.             //忽略SIGINT
  34.             signal(SIGINT, SIG_IGN);
  35.             // //捕捉父进程的信号,退出循环
  36.             //捕捉父进程的信号
  37.             if(signal(SIGUSR1, process_cap1) == SIG_ERR) {
  38.                 cout << "error";
  39.             }
  40.             cout << "创建子进程——1成功  ";
  41.             childPid_1 = getpid();
  42.             cout << "ppid  " << getppid() << "  pid  " << getpid() << endl;
  43.             while(i) {
  44.                 /* 每发送一次 休眠一秒*/
  45.                 sleep(1);
  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.                 write(pfd[1], send.c_str(), send.size());
  53.                 /* 每发送一次 休眠一秒*/
  54.                 //sleep(1);
  55.             }

  56.             cout << "Child Process 1 is killed by Parent" << endl;
  57.             exit(0);
  58.         } else {
  59.             // sleep(5);
  60.             /*执行父进程创建子进程-2操作*/
  61.             childPid_2 = fork();
  62.             if(childPid_2 < 0) {
  63.                 cout << "创建失败" << endl;
  64.             } else if(childPid_2 == 0) {
  65.                 //忽略SIGINT
  66.                 signal(SIGINT, SIG_IGN);
  67.                 //捕捉父进程信号
  68.                 if(signal(SIGUSR2, process_cap2) == SIG_ERR) {
  69.                     cout << "error";
  70.                 }

  71.                 cout << "创建子进程——2成功";
  72.                 childPid_2 = getpid();
  73.                 cout << "  ppid  " << getppid() << "  pid  " << getpid()
  74.                      << endl;
  75.                 /*执行子进程-2操作**/
  76.                 while(j) {
  77.                     /*
  78.                     read(pfd[0], buf, 1024);
  79.                     cout << buf << endl;
  80.                     */
  81.                     size_t nbytes = read(pfd[0], buf, 1024);
  82.                     cout << std::string(buf, buf + nbytes) << endl;
  83.                     sleep(1);
  84.                     if(j == false) {
  85.                         break;
  86.                     }
  87.                 }

  88.                 cout << "Child Process 2 is killed by Parent!" << endl;
  89.                 exit(0);

  90.             } else {
  91.                 /*执行来自用户的操作 ctrl+c */
  92.                 signal(SIGINT, cap);

  93.                 while(true)
  94.                     ;
  95.             }
  96.         }
  97.     }
  98. }

  99. void cap(int signum) {
  100.     cout << getpid() << "进程"
  101.          << "捕捉到SIGINT信号" << endl;
  102.     kill(childPid_1, SIGUSR1);
  103.     waitpid(childPid_1, NULL, 0);

  104.     kill(childPid_2, SIGUSR2);
  105.     waitpid(childPid_2, NULL, 0);

  106.     cout << "Parent Process is killed";
  107.     signal(SIGINT, SIG_DFL);
  108. }

  109. void process_cap1(int signum) {

  110.     cout << getpid() << "进程"
  111.          << "捕捉到SIGUSR1信号" << endl;
  112.     //将 i 设置为负数 退出while
  113.     i = -1;
  114. }

  115. void process_cap2(int signum) {
  116.     cout << j;
  117.     j = false;
  118.     cout << getpid() << "进程"
  119.          << "捕捉到SIGUSR2信号" << endl;
  120.     cout << j;
  121.     return;
  122. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-15 08:40:56 | 显示全部楼层

回帖奖励 +5 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-15 15:52:32 | 显示全部楼层

回帖奖励 +5 鱼币

路过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-29 09:23:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 13:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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