- 论坛徽章:
- 0
|
下面是一段测试程序, 主进程创建一个子进程,子进程创建一个线程。
问题是:主进程向子进程发中断信号后, 子进程的线程不暂停,还在继续执行。
我觉得理论上有点说不过去。
那位能给我说说怎么回事?
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
void stop(int signumber)
{
puts("stop a moment") ;
pause() ;
}
void comeon(int signumber)
{
puts("go on") ;
return ;
}
void *print()
{
int i = 0 ;
while(1)
{
printf("hello world%d\n", i++);
sleep(1) ;
}
return ;
}
int main()
{
pid_t pid ;
if((pid = fork()) == 0)
{
pthread_t th_hello ;
if(signal(SIGCONT, &comeon) == SIG_ERR)
puts("comeon error") ;
if(signal(SIGINT, &stop) == SIG_ERR)
puts("stop error") ;
pthread_create(&th_hello, NULL, print, 0);
pthread_join(th_hello, NULL);
}
else if( pid > 0)
{
sleep(4) ;
kill(pid, SIGINT) ;
}
return 0 ;
} |
|