当前位置: 技术问答>linux和unix
关于kill()系统调用的一个问题
来源: 互联网 发布时间:2017-01-05
本文导语: #include #include #include #include #include #include void send_signal(int pid,int signal) { if(kill(pid,signal) == 0) printf("send signal success.n"); else printf("send signal failed.n"); } void received_signal(int signal) { if(signal ==...
#include
#include
#include
#include
#include
#include
void send_signal(int pid,int signal)
{
if(kill(pid,signal) == 0)
printf("send signal success.n");
else
printf("send signal failed.n");
}
void received_signal(int signal)
{
if(signal == SIGUSR1)
printf("22222222222222222n");
}
void handler(int signal)
{
if (signal == SIGUSR1) {
printf("11111111111111111111111n");
}
}
int main()
{
pid_t pid;
int status = 0;
int rc;
if((pid = fork()) == 0) {
signal(SIGUSR1,received_signal);
printf("i am child process pid = %d.n",getpid());
while (1);
}
if ((pid = fork()) == 0) {
signal(SIGUSR1,handler);
printf("the second process pid = %dn",getpid());
while (1);
}
sleep(1);
printf("pid = %dn",(pid_t)pid);
rc = kill(0,SIGUSR1);
printf("rc = %dn",rc);
printf("333333333333333333333333333333n");
printf("i am parent process.pid = %dn",getpid());
wait(&status);
exit(0);
}
这是我写的一个小的测试程序,想通过kill向进程组发送信号。
问题是:把kill的确把信号发送到所在的进程组了,可在执行
kill函数时好像就阻塞了,下面的很多代码就不执行了,求解!
#include
#include
#include
#include
#include
void send_signal(int pid,int signal)
{
if(kill(pid,signal) == 0)
printf("send signal success.n");
else
printf("send signal failed.n");
}
void received_signal(int signal)
{
if(signal == SIGUSR1)
printf("22222222222222222n");
}
void handler(int signal)
{
if (signal == SIGUSR1) {
printf("11111111111111111111111n");
}
}
int main()
{
pid_t pid;
int status = 0;
int rc;
if((pid = fork()) == 0) {
signal(SIGUSR1,received_signal);
printf("i am child process pid = %d.n",getpid());
while (1);
}
if ((pid = fork()) == 0) {
signal(SIGUSR1,handler);
printf("the second process pid = %dn",getpid());
while (1);
}
sleep(1);
printf("pid = %dn",(pid_t)pid);
rc = kill(0,SIGUSR1);
printf("rc = %dn",rc);
printf("333333333333333333333333333333n");
printf("i am parent process.pid = %dn",getpid());
wait(&status);
exit(0);
}
这是我写的一个小的测试程序,想通过kill向进程组发送信号。
问题是:把kill的确把信号发送到所在的进程组了,可在执行
kill函数时好像就阻塞了,下面的很多代码就不执行了,求解!
|
If pid is 0, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal
to the process group ID of the sender, and for which the process has permission to send a signal.
if the value of pid causes sig to be generated for the sending process, and if sig is not blocked for the calling thread and if no
other thread has sig unblocked or is waiting in a sigwait() function for sig, either sig or at least one pending unblocked signal
shall be delivered to the sending thread before kill() returns.
意思就是你给父进程同组发送信号,同样也会发给父进程自己,在kill返回之前这个信号被处理,而父进程没有注册SIGUSR1,你可以查一下SIGUSR1默认的处理是什么。
两个子进程和父进程都是同组的,接受到信号后按道理是会打印的啊。
把while(1)换成while(1){pause();}看看。
to the process group ID of the sender, and for which the process has permission to send a signal.
if the value of pid causes sig to be generated for the sending process, and if sig is not blocked for the calling thread and if no
other thread has sig unblocked or is waiting in a sigwait() function for sig, either sig or at least one pending unblocked signal
shall be delivered to the sending thread before kill() returns.
意思就是你给父进程同组发送信号,同样也会发给父进程自己,在kill返回之前这个信号被处理,而父进程没有注册SIGUSR1,你可以查一下SIGUSR1默认的处理是什么。
两个子进程和父进程都是同组的,接受到信号后按道理是会打印的啊。
把while(1)换成while(1){pause();}看看。
|
fork()这个函数会返回2次,若返回大于0的任何数时它代表的是子进程的进程号;
若返回0,则表示父进程的进程号(也就是当前进程),而你
这个kill函数却要杀死进程号为0进程,这不扯了,把自己(父进程)给杀掉了,
所以当然后面没有输出了。
若返回0,则表示父进程的进程号(也就是当前进程),而你
rc = kill(0, SIGUSR1);
这个kill函数却要杀死进程号为0进程,这不扯了,把自己(父进程)给杀掉了,
所以当然后面没有输出了。