• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            求教一個簡單的多進程和信號的問題

            Posted on 2009-01-04 20:04 Prayer 閱讀(438) 評論(0)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX
            /*編制一段程序,使用系統調用fork()創建兩個子進程,
            再用系統調用signal()讓父進程捕捉鍵盤上來的中斷信號(即按Del鍵),
            當捕捉到中斷信號后,父進程用系統調用kill()向兩個子進程發出信號,
            子進程捕捉到信號后,分別輸出下列信息后終止:
                child process 1 is killed by parent!
                child process 2 is killed by parent!
                父進程等待兩個子進程終止后,輸出以下信息后終止:
                parent process is killed !
            */
            #include<stdio.h>
            #include<signal.h>
            #include<unistd.h>
            void waiting();
            void stop();
            int wait_mark;
            main()
            {
                int p1=-1, p2=-1;
                //signal(SIGINT, stop); // position A
                while ((p1 = fork()) == -1);
                    if (p1>0)
                   {   /*parent*/
                       while ((p2 = fork()) == -1);
                       if (p2>0)
                      {   /*parent*/
                          wait_mark=1;
                          signal(SIGINT, stop);// position B
                          waiting();
                          kill(p1,16);             //*send signal 16 to end the process p1
                          kill(p2,17);            //*send signal 17 to end the process p2
                          wait(0);                 //*waiting for the ending of p1
                          wait(0);                //*waiting for the ending of p2
                          printf("parent process is killed!\n");
                          exit(0);                //*quit from the parent process
                      }
                       else                                    //*p2 work
                      {    /*child p2*/
                           wait_mark=1;
                           signal(17, stop);
                           waiting();
                           lockf(1,1,0);
                           printf("child process 2 is killed by parent!\n");
                           lockf(1,0,0);
                        exit(0);                          //* p2  quit
                      }
                   }
                   else                                       //*p1 work
                   {      /*child p1*/
                          wait_mark=1;
                          signal(16, stop);         
                          waiting();
                          lockf(1,1,0);                          
                          printf("child process 1 is killed by parent!\n");
                          lockf(1,0,0);                          
                          exit(0);                           //* p1 quit
                 }
            }

            void waiting( )
            {
                  while (wait_mark != 0);
            }

            void stop()
            {
                  wait_mark=0;
            }


            又研究了一下,在兩個子進程開始就加入signal(SIGINT,SIG_IGN),就滿足要求了。看來原來的Ctrl-c信號對所有進程都起作用了,所以得把它給禁了,才能合要求。   
            久久亚洲熟女cc98cm| 日日狠狠久久偷偷色综合免费| 亚洲欧美国产精品专区久久 | 久久精品国产亚洲av麻豆小说 | 国产AV影片久久久久久| 久久99精品久久久久久9蜜桃| 亚洲欧美日韩久久精品| 久久久久人妻精品一区| 久久免费观看视频| 久久99国产综合精品女同| 日日狠狠久久偷偷色综合96蜜桃| 亚洲AV无码久久| 亚洲精品tv久久久久| 精品国产一区二区三区久久| 久久噜噜久久久精品66| 99久久er这里只有精品18| 日本亚洲色大成网站WWW久久| 精品久久久久久中文字幕人妻最新| 香港aa三级久久三级| 亚洲AV日韩AV永久无码久久| 久久人人爽人人精品视频| 伊人色综合久久| 久久66热人妻偷产精品9| 久久精品国产亚洲av麻豆蜜芽| 国产精品99久久不卡| 精品国产乱码久久久久久郑州公司 | 久久青青草原精品国产| 一本大道久久香蕉成人网| 久久久久九国产精品| 国产亚洲成人久久| 亚洲一本综合久久| 国产精品一久久香蕉产线看| 久久天天躁狠狠躁夜夜96流白浆| 久久久久久久综合狠狠综合| 热RE99久久精品国产66热| 久久久久国产一区二区| 亚洲精品美女久久久久99小说| 久久久噜噜噜久久| 麻豆久久久9性大片| 亚洲国产成人久久综合碰| 一本久久免费视频|