亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5584 | 回復(fù): 7
打印 上一主題 下一主題

[C] 一個(gè)父子進(jìn)程信號(hào)通信的小程序 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-04-02 11:09 |只看該作者 |倒序?yàn)g覽
小弟新手, 寫了一個(gè)父子進(jìn)程信號(hào)通信的小程序, 運(yùn)行的時(shí)候有點(diǎn)問題,請(qǐng)大家?guī)兔纯?br />

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <signal.h>
  9. #include <sys/uio.h>
  10. #include <sys/wait.h>
  11. #include <sys/errno.h>

  12. #define MAXCNT 4

  13. void sig_usr(int signo)
  14. {
  15.         if( signo == SIGCONT)
  16.                 printf("received SIGCONT\n");
  17.         else
  18.                 printf("received unexpected signal(%d)\n", signo);

  19.         return;
  20. }

  21. int main(void)
  22. {
  23.         int idx = 0;
  24.         int status;

  25.         pid_t pid;

  26.         if( (pid=fork()) < 0 ) {
  27.                 printf("fork error\n");
  28.                 exit(1);
  29.         }else if (pid == 0) {                   /* child */
  30.                 while( idx < MAXCNT ) {
  31.                         if( signal(SIGCONT, sig_usr) == SIG_ERR ) {
  32.                                 printf("c signal error\n");
  33.                                 exit(2);
  34.                         }
  35.                         printf("child[%d]\n", idx);
  36.                         fflush(stdout);
  37.                         if( idx < MAXCNT - 1 ){
  38.                                 printf("child pause ...\n");
  39.                                 fflush(stdout);
  40.                                 kill(getppid(), SIGCONT);
  41.                                 pause();
  42.                         }
  43.                         printf("child up ...\n");
  44.                         fflush(stdout);
  45.                         idx++;
  46.                 }
  47.                 exit(99);
  48.         }else {                                 /* parent */
  49.                 while( idx < MAXCNT ) {
  50.                         if( signal(SIGCONT, sig_usr) == SIG_ERR ) {
  51.                                 printf("p signal error\n");
  52.                                 exit(3);
  53.                         }
  54.                         printf("parent[%d]\n", idx);
  55.                         fflush(stdout);
  56.                         kill(pid, SIGCONT);
  57.                         if( idx < MAXCNT - 1 ) {
  58.                                 printf("parent pause ...\n");
  59.                                 fflush(stdout);
  60.                                 kill(pid, SIGCONT);
  61.                                 pause();
  62.                         }
  63.                         printf("parent up ...\n");
  64.                         fflush(stdout);
  65.                         idx++;
  66.                 }
  67.                 if( waitpid(pid, &status, 0) < 0 ) {
  68.                         printf("waitpid error\n");
  69.                         exit(3);
  70.                 }
  71.         }
  72.         return 0;
  73. }
復(fù)制代碼


編譯: gcc -Wall -O2 -o test test.c
運(yùn)行: ./test


  1. [root@linux /home/test]./test
  2. child[0]
  3. child pause ...
  4. parent[0]
  5. parent pause ...
  6. received SIGCONT
  7. child up ...
  8. child[1]
  9. child pause ...
  10. received SIGCONT
  11. parent up ...
  12. parent[1]
  13. parent pause ...
  14. received SIGCONT
  15. child up ...
  16. child[2]
  17. child pause ...
  18. received SIGCONT
  19. parent up ...
  20. parent[2]
  21. parent pause ...
  22. received SIGCONT                          <=== seemed hang here

復(fù)制代碼


父進(jìn)程在等待之后發(fā)送 SIGCONT, 子進(jìn)程沒有 up,  CTRL+Z stop進(jìn)程之后, fg 在運(yùn)行它又成功了. 希望大家?guī)兔Ψ治鲆幌? 謝謝!

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-04-02 11:23 |只看該作者

  1. [root@linux test]# ./test
  2. child[0]
  3. child pause ...
  4. parent[0]
  5. parent pause ...
  6. received SIGCONT
  7. child up ...
  8. child[1]
  9. child pause ...
  10. received SIGCONT
  11. parent up ...
  12. parent[1]
  13. parent pause ...
  14. received SIGCONT
  15. child up ...
  16. child[2]
  17. child pause ...
  18. received SIGCONT
  19. parent up ...
  20. parent[2]
  21. parent pause ...
  22. received SIGCONT

  23. [1]+  Stopped                 ./test                            <=== CTRL+Z

  24. [root@linux test]# fg
  25. ./test
  26. received SIGCONT
  27. parent up ...
  28. parent[3]
  29. parent up ...
  30. received SIGCONT
  31. child up ...
  32. child[3]
  33. child up ...
  34. [root@linux test]# echo $?
  35. 0
  36. [root@linux test]#
復(fù)制代碼

論壇徽章:
24
15-16賽季CBA聯(lián)賽之北京
日期:2018-08-17 18:43:33技術(shù)圖書徽章
日期:2018-08-22 12:53:57技術(shù)圖書徽章
日期:2018-08-22 12:54:20技術(shù)圖書徽章
日期:2018-08-22 12:54:3015-16賽季CBA聯(lián)賽之福建
日期:2018-10-19 16:58:1619周年集字徽章-慶
日期:2019-08-27 13:28:5619周年集字徽章-19
日期:2019-08-27 13:31:2619周年集字徽章-19
日期:2019-08-27 13:31:2615-16賽季CBA聯(lián)賽之同曦
日期:2019-09-05 12:03:2819周年集字徽章-周
日期:2019-09-06 18:54:5415-16賽季CBA聯(lián)賽之上海
日期:2018-07-25 11:55:2615-16賽季CBA聯(lián)賽之青島
日期:2018-07-10 14:13:18
3 [報(bào)告]
發(fā)表于 2008-04-02 15:09 |只看該作者
能不能試一試用別的信號(hào)呢? 比如用戶自定義那幾個(gè).

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-04-02 16:50 |只看該作者
程序執(zhí)行流程的問題。有時(shí)候會(huì)出現(xiàn)這種情況:

父進(jìn)程還沒掛起,子進(jìn)程就給它發(fā)SIGCOUT信號(hào),當(dāng)它掛起時(shí),卻等不到子進(jìn)程的
SIGCOUT信號(hào),因?yàn)榇藭r(shí)子進(jìn)程也在等待父進(jìn)程的信號(hào),這樣系統(tǒng)就處于一種死鎖的狀態(tài)了。

具體的問題也就是, signal注冊(cè)信號(hào)和pause掛起之間有個(gè)時(shí)間窗口,在這個(gè)窗口內(nèi)信號(hào)已經(jīng)
發(fā)生過,程序當(dāng)然就一直處于pause狀態(tài)了。

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-04-02 17:17 |只看該作者

回復(fù) #3 incle 的帖子

SIGCONT 信號(hào)系統(tǒng)默認(rèn)的處理方式可以對(duì)付這個(gè)
SIGUSR1, SIGUSR2 還要在去定義

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-04-02 17:20 |只看該作者

回復(fù) #4 G00GLE 的帖子


  1. child up ...
  2. child[2]
  3. child pause ...            <= child pause
  4. received SIGCONT     <= parent received
  5. parent up ...
  6. parent[2]
  7. parent pause ...
  8. received SIGCONT     <= child received                   <=== seemed hang here
復(fù)制代碼


child 已經(jīng)掛起了, 然后在收到的父進(jìn)程的SIGCONT 信號(hào)的

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-04-02 19:19 |只看該作者
這時(shí)候應(yīng)該使用可靠信號(hào)語義實(shí)現(xiàn),用pause有競爭。
man sigsuspend
man sigprocmask

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2008-04-03 18:26 |只看該作者

回復(fù) #7 JohnBull 的帖子

謝謝版主提示, 我對(duì)上面的程序做了修改,大致能運(yùn)行正確了, 父子進(jìn)程互相傳遞信號(hào), 交替打印


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <signal.h>
  9. #include <sys/uio.h>
  10. #include <sys/wait.h>
  11. #include <sys/errno.h>

  12. #define MAXCNT 4

  13. volatile sig_atomic_t    contflag;    /* set nonzero by signal handler */

  14. void sig_usr(int signo)
  15. {
  16.         if( signo == SIGCONT) {
  17.                 contflag = 1;
  18.                 printf("received SIGCONT\n");
  19.         } else
  20.                 printf("received unexpected signal(%d)\n", signo);

  21.         return;
  22. }

  23. int main(void)
  24. {
  25.         int idx = 0;
  26.         int status;
  27.         pid_t pid;

  28.         sigset_t newmask;
  29.         sigset_t oldmask;
  30.         sigset_t zeromask;

  31.         sigemptyset(&zeromask);
  32.         sigemptyset(&newmask);
  33.         sigaddset(&newmask, SIGCONT);
  34.         if( (pid=fork()) < 0 ) {
  35.                 printf("fork error\n");
  36.                 exit(1);
  37.         }else if (pid == 0) {                   /* child */
  38.                 while( idx < MAXCNT ) {
  39.                         if( signal(SIGCONT, sig_usr) == SIG_ERR ) {
  40.                                 printf("c signal error\n");
  41.                                 exit(2);
  42.                         }
  43.                         if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
  44.                                 printf("SIG_BLOCK error\n");
  45.                                 exit(1);
  46.                         }
  47.                         printf("child[%d]\n", idx);
  48.                         printf("child pause ...\n");
  49.                         kill(getppid(), SIGCONT);
  50.                         while( contflag == 0)
  51.                                 sigsuspend(&zeromask);
  52.                         printf("child up ...\n");
  53.                         contflag = 0;
  54.                         if( sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0 ) {
  55.                                 printf("SIG_SETMASK error\n");
  56.                                 exit(2);
  57.                         }
  58.                         idx++;
  59.                 }
  60.                 kill(getppid(), SIGCONT);       /* child done */
  61.                 exit(99);
  62.         }else {                                 /* parent */
  63.                 while( idx < MAXCNT ) {
  64.                         if( signal(SIGCONT, sig_usr) == SIG_ERR ) {
  65.                                 printf("p signal error\n");
  66.                                 exit(3);
  67.                         }
  68.                         if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
  69.                                 printf("SIG_BLOCK error\n");
  70.                                 exit(3);
  71.                         }
  72.                         printf("parent pause ...[%d]\n", idx);
  73.                         kill(pid, SIGCONT);
  74.                         while( contflag == 0)
  75.                                 sigsuspend(&zeromask);
  76.                         printf("parent up ...\n");
  77.                         contflag = 0;
  78.                         if( sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0 ) {
  79.                                 printf("SIG_SETMASK error\n");
  80.                                 exit(3);
  81.                         }
  82.                         idx++;
  83.                 }
  84.                 if( waitpid(pid, &status, 0) < 0 ) {
  85.                         printf("waitpid error\n");
  86.                         exit(3);
  87.                 }
  88.         }
  89.         return 0;
  90. }

復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP