LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 634|回复: 1

请帮忙看看这段代码

[复制链接]
发表于 2006-5-11 11:44:47 | 显示全部楼层 |阅读模式
[php]
# include <stdio.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <signal.h>

pid_t ch1,ch2;
void handler();
void fun1();
void fun2();
main()
{
        signal(2,handler);
        if((ch1=fork())<0) exit(0);
        else if(ch1==0){
                signal(16,fun1);
                printf("this is child 1 process running.\n");
                sleep(10);
                exit(0);
        }
        else {
                if((ch2=fork())<0) exit(0);
                else if(ch2==0) {
                        signal(17,fun2);
                        printf("this is child 2 process running.\n");
                        sleep(10);
                        exit(0);
                }
                else {
                        printf("this is the parent process.\n");
//                        kill(getpid(),2);
                        wait(0);
                        wait(0);
                        printf("parent process is killed!\n");
                        exit(0);
                }
        }
}
void  handler()
{
        printf("bengin interupt handler.\n");
        kill(ch1,16);
        kill(ch2,17);
        printf("finished\n");
}

void fun1()
{
        printf("child process 1 is killed by parent!!\n");
}

void fun2()
{
        printf("child process 2 is killed by parent!!\n");
}
[/php]


父进程创建2个子进程,然后父进程捕捉键盘中断,收到中断后触发handler分别向两个子进程发送信号16,17,子进程收到信号后,触发fun1,fun2输出两句话。

运行的时候按 ^C
输出结果却是这样:

this is child 1 process running.
this is child 2 process running.
this is the parent process.
bengin interupt handler.
finished
bengin interupt handler.
child process 1 is killed by parent!!
bengin interupt handler.
child process 1 is killed by parent!!
finished
Stack fault

如果加上注释的那段kill(getpid(),2);结果就是正确的。

不明百,请大家指教,我的系统是gentoo

gcc version 3.4.5 (Gentoo 3.4.5-r1, ssp-3.4.5-1.0, pie-8.7.9)
glibc-2.3.6-r3
 楼主| 发表于 2006-5-11 13:44:21 | 显示全部楼层
不能沉下去啊
回复 支持 反对

使用道具 举报

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

本版积分规则

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