找回密码
 注册
查看: 1062|回复: 3

问一个linux下关于kill()与singal的使用问题:

[复制链接]
发表于 2005-4-28 22:57:50 | 显示全部楼层 |阅读模式
是在linux下编的c程序,目的是:
使用系统调用fork()创建1个子进程,再调用系统调用signal()让父进程捕捉键盘上来的中断信号当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:
Child processl2 is killed by parent!

父进程等待子进程终止后,输出如下的信息后终止:
Parent process is killed!



#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void waiting( ),stop( );
int wait_mark;
main( )
{
int p2;
while((p2=fork( ))= =-1); /*创建子进程*/
if(p2>0)
{
wait_mark=1;
signal(SIGINT,stop); /*接收到^c信号,转stop*/
waiting( );
kill(p2,17); /*向p2发软中断信号17*/
wait(0); /*同步*/
printf("Parent process is killed!\n");
exit(0);
}
else
{
wait_mark=1;
signal(17,stop); /*接收到软中断信号17,转stop*/
waiting( );
printf("Child process 2 is killed by parent!\n");
exit(0);
}

}

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

void stop( )
{
wait_mark=0;
}


为什么运行后只有父进程输出Parent process is killed!
预期的应该是子进程先输出,然后才是父进程啊~~~
不懂啊,应该怎么改进啊
发表于 2005-4-29 11:19:36 | 显示全部楼层
将17改成SIGINT。你认为SIGINT就是17是错的。这样的程序没有移植性。
回复

使用道具 举报

发表于 2005-5-24 11:54:17 | 显示全部楼层
同问
回复

使用道具 举报

发表于 2005-5-24 11:57:23 | 显示全部楼层
象SIGINT的值是02 ,SIGQUIT的值是03,,,,,,,,
用户自定义信号SIGUSR1 和SIGUSR2的值分别是16,17,这些信号变量的值都是在signal.h这个 头文件中宏定义好的。既然如此,为什么不能象楼主这样用啊?
回复

使用道具 举报

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

本版积分规则

GMT+8, 2025-2-8 14:59 , Processed in 0.026909 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

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