简体   繁体   中英

Weird behaviour of sigwait

I am using sigwait to block my thread for some signals. These signals have been added in the sigs set. AS per the doc sigwait is supposed to wait only for the signals passed to it as a set in the argument and is not supposed to change th signal mask of that thread.. But for some reason i dont know, it is changing the signal mask of the thread. It is blocking all signals other than the ones in sigs. I dont wish this to happen. Can someone help me with the same. Thanx in advance

The code snippet:

sigset_t sigs;
int sig_recvd;
sigset_t old_mask;
sigemptyset(&sigs);
sigaddset(&sigs, SIGUSR1);
sigaddset(&sigs, SIGTERM);
sigaddset(&sigs, SIGHUP);
sigaddset(&sigs, SIGINT);
sigaddset(&sigs, SIGPIPE);
sigaddset(&sigs, SIGCHLD);

sigprocmask(SIG_BLOCK, &sigs, &old_mask);

do
{

    sigwait(&sigs, &sig_recvd);
    //Switch for some signal handling

} while(1);

Sigblk before sigwait: 0000000080014203
Sigblk during sigwait: fffffffefffabcfc


I dont what is wrong with the sigwait function but when i did the same with sigwaitinfo, things worked out for me. Couldnt figure out what made the later work, but for now my problem is solved. But i would like to know if there are any differences in the implementation of the the two

I am not quite sure what your question is, if your question is "Why sigwait changes sigmast during its execution,not after its execution", then it is the case, sigwait will unlock the signal when it is executing, or else, how can it expect signal to come? here is excerpt from APUE: If one of the signals specified in the set is pending at the time sigwait is called, then sigwait will return without blocking. Before returning, sigwait removes the signal from the set of signals pending for the process. To avoid erroneous behavior, a thread must block the signals it is waiting for before calling sigwait. The sigwait function will atomically unblock the signals and wait until one is delivered. Before returning, sigwait will restore the thread's signal mask. If the signals are not blocked at the time that sigwait is called, then a timing window is opened up where one of the signals can be delivered to the thread before it completes its call to sigwait If one of the signals specified in the set is pending at the time sigwait is called, then sigwait will return without blocking. Before returning, sigwait removes the signal from the set of signals pending for the process. To avoid erroneous behavior, a thread must block the signals it is waiting for before calling sigwait. The sigwait function will atomically unblock the signals and wait until one is delivered. Before returning, sigwait will restore the thread's signal mask. If the signals are not blocked at the time that sigwait is called, then a timing window is opened up where one of the signals can be delivered to the thread before it completes its call to sigwait If one of the signals specified in the set is pending at the time sigwait is called, then sigwait will return without blocking. Before returning, sigwait removes the signal from the set of signals pending for the process. To avoid erroneous behavior, a thread must block the signals it is waiting for before calling sigwait. The sigwait function will atomically unblock the signals and wait until one is delivered. Before returning, sigwait will restore the thread's signal mask. If the signals are not blocked at the time that sigwait is called, then a timing window is opened up where one of the signals can be delivered to the thread before it completes its call to sigwait .

为什么不使用pthread_sigmask()而不是sigprocmask()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM