QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1222|回复: 4

新手求助:一个小程序

[复制链接]
发表于 2003-10-24 21:47:48 | 显示全部楼层 |阅读模式
我是个新手,提这么简单的问题请大家不要见笑。
有这样一个简单的程序,是有关linux信号控制的:
#include<stdio.h>
#include<signal.h>

void waiting(),stop();
int wait_mark;

main()
{
int p1,p2;
while((p1=fork())==-1);
if(p1>0)
      {
         while((p2=fork())==-1);
         if(p2>0)
               {
                  wait_mark=1;
                  signal(SIGINT,stop);
                  waiting();
                  kill(p1,16);
                  kill(p2,17);
                  wait(0);
                  wait(0);
                  printf("parent process is killed!\n");
                  exit(0);
               }
         else
               {
                  wait_mark=1;
                  signal(17,stop);
                  waiting();
                  lockf(stdout,1,0);
                  printf("process two is killed by parent!\n");
                  lockf(stdout,0,0);
                  exit(0);
                }
          }
   else
      {
         wait_mark=1;
         signal(16,stop);
         waiting();
         lockf(stdout,1,0);
         printf("process two is killed by parent!\n");
         lockf(stdout,0,0);
         exit(0);
       }
}

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

void stop()
{
  wait_mark=0;
}

这个程序在运行时,当按下中断信号CTRL+C(对应SIGINT)时,屏幕只输出父进程信息:parent process is killed!,两个子进程直接被杀死而没有任何输出。但理论上按下CTRL+C时,屏幕应输出:
process one is killed by parent!
process two is killed by parent!
parent process is killed!
多次修改仍不行,终端只有父进程的退出信号。真是头大,束手无策。
望哪位高手能指点一下,在下感激不尽。
发表于 2003-10-24 22:16:27 | 显示全部楼层
nothing related with kernel.
回复

使用道具 举报

发表于 2003-10-25 12:31:05 | 显示全部楼层
在子进程中屏蔽掉SIGINT,否则子进程收到SIGINT后会退出

like this:

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include&lt;sys/types.h&gt;

void waiting(void);
void stop(int);
int wait_mark;

main()
{
        pid_t p1,p2;
        sigset_t nset,oset;
        sigemptyset(&amp;nset);
        sigemptyset(&amp;oset);

        while((p1=fork())==-1);
        if(p1&gt;0)
        {
                while((p2=fork())==-1);
                if(p2&gt;0)
                {
                        wait_mark=1;
                        signal(SIGINT,stop);
                        waiting();
                        kill(p1,SIGSTKFLT);
                        kill(p2,SIGCHLD);
                        wait(NULL);
                        wait(NULL);
                        fprintf(stderr,"parent process is killed!\n");
                        exit(0);
                }
                else
                {
                        sigaddset(&amp;nset,SIGINT);
                        sigprocmask(SIG_BLOCK,&amp;nset,&amp;oset);
                        wait_mark=1;
                        signal(17,stop);
                        waiting();
                        lockf(STDOUT_FILENO,1,0);
                        fprintf(stderr,"process two is killed by parent!\n");
                        lockf(STDOUT_FILENO,0,0);
                        sigprocmask(SIG_SETMASK,&amp;oset,NULL);
                        exit(0);
                }
        }
        else
        {
                sigaddset(&amp;nset,SIGINT);
                sigprocmask(SIG_BLOCK,&amp;nset,&amp;oset);
                wait_mark=1;
                signal(16,stop);
                waiting();
                lockf(STDOUT_FILENO,1,0);
                fprintf(stderr,"process one is killed by parent!\n");
                lockf(STDOUT_FILENO,0,0);
                sigprocmask(SIG_SETMASK,&amp;oset,NULL);
                exit(0);
        }
}

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

void stop(int sig)
{
        printf("pid : %d got signal \" %d \" from parent : %d\n",getpid(),sig,getppid());
        wait_mark=0;
}
回复

使用道具 举报

 楼主| 发表于 2003-10-25 16:49:25 | 显示全部楼层
多谢z兄,回去再去试几次

斑竹见谅,小弟不是有意的
回复

使用道具 举报

发表于 2003-10-27 23:07:25 | 显示全部楼层
sure.  
回复

使用道具 举报

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

本版积分规则

GMT+8, 2024-9-28 04:18 , Processed in 0.085228 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

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