basil
basil

Reputation: 720

Understanding sigwait() and how it sets parameters

The man page for sigwait() is a big confusing to me. The signature for sigwait is

 int sigwait(const sigset_t *set, int *sig);

Say I have code similar to the following

 sigset_t wset;
 sigemptyset(&wset);
 sigaddset(&wset,SIGHUP);
 sigaddset(&wget,SIGINT);
 sigaddset(&wget,SIGTERM);
 int sig;
 sigwait(&wset,&sig);

If I understand correctly, this will wait on a signal to be sent. If the signal is found in the signal set (i.e. sigset_t wset), it will set the signal number (i.e. int sig), as found in the set (i.e. SIGHUP, SIGINT, SIGTERM in this example). Otherwise, it will set the signal number to 0. Is my understanding correct?

I would try this out, but I don't have access to a compiler at the moment.

edit: for further clarification

Ok so, I send one of the signals above:

 int res = sigwait(&wset,&sig);
 // res = 0, sig = SIGHUP, SIGINT, or SIGTERM

And I send a signal not included in the set:

int res = sigwait(&wset,&sig);
// res = EINVAL (i.e. non-zero) and don't worry about sig

Is this correct?

Upvotes: 0

Views: 1937

Answers (1)

RalfFriedl
RalfFriedl

Reputation: 1130

As you say, in your example sigwait will wait for one of SIGHUP, SIGINT, SIGTERM.

Otherwise, it will set the signal number to 0.

There is no otherwise. The only defined error is

EINVAL The set argument contains an invalid or unsupported signal number.

Edit To state this explicitly, the only defined error for this function is an invalid input for the set argument. As the set you constructed in your example is not invalid, the function will not return an error, it will wait until one of the signals arrives.

Upvotes: 1

Related Questions