当前位置: 技术问答>linux和unix
子程序無法收到 ctrl+c 的信息 SIGINT !
来源: 互联网 发布时间:2015-10-15
本文导语: father.cpp #include #include #include #include void sig(int s) { printf("father received signal %dn", s); return; } int main() { if (signal(SIGINT, sig) == SIG_ERR) { printf("can't catch SIGINTn"...
father.cpp
#include
#include
#include
#include
void sig(int s)
{
printf("father received signal %dn", s);
return;
}
int main()
{
if (signal(SIGINT, sig) == SIG_ERR)
{
printf("can't catch SIGINTn");
exit(1);
}
int pid=fork();
if(pid==0)
{
execl("/root/child","child",(char*)0);
}
while(1);
}
---------------------------------------------------
child.cpp
#include
#include
#include
#include
void sig(int s)
{
printf("child received signal %dn", s);
return;
}
int main()
{
setpgrp(); // 自己成為進程組長
if (signal(SIGINT, sig) == SIG_ERR)
{
printf("can't catch SIGINTn");
exit(1);
}
while(1);
}
output:
[root@localhost root]# ./father
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
SID PGID PID COMMAND
18808 18808 18808 bash -------> bash 本身 pgid=18808
18808 19216 19216 father -------> bash 產生新的 pgid=19216
18808 19217 19217 child -------> 故意將 child pgid=19216變成19217 .
在同一session=18808 中, 為何 pgid=19217的child 沒收到 SIGINT ?
緊急~~ 知道的說一下 ..
#include
#include
#include
#include
void sig(int s)
{
printf("father received signal %dn", s);
return;
}
int main()
{
if (signal(SIGINT, sig) == SIG_ERR)
{
printf("can't catch SIGINTn");
exit(1);
}
int pid=fork();
if(pid==0)
{
execl("/root/child","child",(char*)0);
}
while(1);
}
---------------------------------------------------
child.cpp
#include
#include
#include
#include
void sig(int s)
{
printf("child received signal %dn", s);
return;
}
int main()
{
setpgrp(); // 自己成為進程組長
if (signal(SIGINT, sig) == SIG_ERR)
{
printf("can't catch SIGINTn");
exit(1);
}
while(1);
}
output:
[root@localhost root]# ./father
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
father received signal 2 -------> ctrl + c
SID PGID PID COMMAND
18808 18808 18808 bash -------> bash 本身 pgid=18808
18808 19216 19216 father -------> bash 產生新的 pgid=19216
18808 19217 19217 child -------> 故意將 child pgid=19216變成19217 .
在同一session=18808 中, 為何 pgid=19217的child 沒收到 SIGINT ?
緊急~~ 知道的說一下 ..
|
去掉这句话就可以setpgrp(); // 自己成為進程組長
|
你的这些数据怎么出来的?
SID PGID PID COMMAND
18808 18808 18808 bash -------> bash 本身 pgid=18808
18808 19216 19216 father -------> bash 產生新的 pgid=19216
18808 19217 19217 child -------> 故意將 child pgid=19216變成19217 .
SID PGID PID COMMAND
18808 18808 18808 bash -------> bash 本身 pgid=18808
18808 19216 19216 father -------> bash 產生新的 pgid=19216
18808 19217 19217 child -------> 故意將 child pgid=19216變成19217 .
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。