当前位置: 技术问答>linux和unix
紧急求助:关于线程中接收alarm信号的问题
来源: 互联网 发布时间:2016-03-16
本文导语: 下面的程序在只用一个线程的程序中正常运行,可当把这段程序放到一个新开的线程中时,就不能运行,一直阻塞在sigtimedwait函数处,不能接收到alarm的信号,就连把tv的值设为{1,0}时也不能返回。这是什么原因呢?...
下面的程序在只用一个线程的程序中正常运行,可当把这段程序放到一个新开的线程中时,就不能运行,一直阻塞在sigtimedwait函数处,不能接收到alarm的信号,就连把tv的值设为{1,0}时也不能返回。这是什么原因呢?难道sigtimedwait函数不能在新线程中运行吗?请高人指点一下。。
sigset_t wait_set;
int sig_no;
const struct timespec tv = {20,0};
siginfo_t sig_info ;
sigemptyset(&wait_set);
sigaddset(&wait_set, SIGALRM);
sigprocmask(SIG_BLOCK, &wait_set, NULL);
while(1)
{
alarm(1);
sig_no=sigtimedwait(&wait_set,&sig_info,&tv);
printf("I am here!");
fflush(0);
}
sigset_t wait_set;
int sig_no;
const struct timespec tv = {20,0};
siginfo_t sig_info ;
sigemptyset(&wait_set);
sigaddset(&wait_set, SIGALRM);
sigprocmask(SIG_BLOCK, &wait_set, NULL);
while(1)
{
alarm(1);
sig_no=sigtimedwait(&wait_set,&sig_info,&tv);
printf("I am here!");
fflush(0);
}
|
NOTES
In normal usage, the calling program blocks the signals in set via a prior
call to sigprocmask() (so that the default disposition for these signals
does not occur if they are delivered between successive calls to sigwait-
info() or sigtimedwait()) and does not establish handlers for these sig-
nals. In a multithreaded program, the signal should be blocked in all
threads to prevent the signal being delivered to a thread other than the
one calling sigwaitinfo() or sigtimedwait()).
POSIX leaves the meaning of a NULL value for the timeout argument of sig-
timedwait() unspecified, permitting the possibility that this has the same
meaning as a call to sigwaitinfo(), and indeed this is what is done on
Linux.
在多线程的程序里,你需要在别的线程中将信号屏蔽,否则该信号有可能被别的线程捕获,而不是被你希望的线程捕获。
In normal usage, the calling program blocks the signals in set via a prior
call to sigprocmask() (so that the default disposition for these signals
does not occur if they are delivered between successive calls to sigwait-
info() or sigtimedwait()) and does not establish handlers for these sig-
nals. In a multithreaded program, the signal should be blocked in all
threads to prevent the signal being delivered to a thread other than the
one calling sigwaitinfo() or sigtimedwait()).
POSIX leaves the meaning of a NULL value for the timeout argument of sig-
timedwait() unspecified, permitting the possibility that this has the same
meaning as a call to sigwaitinfo(), and indeed this is what is done on
Linux.
在多线程的程序里,你需要在别的线程中将信号屏蔽,否则该信号有可能被别的线程捕获,而不是被你希望的线程捕获。