The Linux Programming Interface

(nextflipdebug5) #1
Signals: Advanced Features 471

printf(" si_pid=%ld, si_uid=%ld\n",
(long) si.si_pid, (long) si.si_uid);
}
}
––––––––––––––––––––––––––––––––––––––––––––––––––– signals/t_sigwaitinfo.c
The sigtimedwait() system call is a variation on sigwaitinfo(). The only difference is
that sigtimedwait() allows us to specify a time limit for waiting.

The timeout argument specifies the maximum time that sigtimedwait() should wait
for a signal. It is a pointer to a structure of the following type:

struct timespec {
time_t tv_sec; /* Seconds ('time_t' is an integer type) */
long tv_nsec; /* Nanoseconds */
};

The fields of the timespec structure are filled in to specify the maximum number of
seconds and nanoseconds that sigtimedwait() should wait. Specifying both fields of
the structure as 0 causes an immediate timeout—that is, a poll to check if any of the
specified set of signals is pending. If the call times out without a signal being deliv-
ered, sigtimedwait() fails with the error EAGAIN.
If the timeout argument is specified as NULL, then sigtimedwait() is exactly equiva-
lent to sigwaitinfo(). SUSv3 leaves the meaning of a NULL timeout unspecified, and
some UNIX implementations instead interpret this as a poll request that returns
immediately.

22.11 Fetching Signals via a File Descriptor...........................................................................


Starting with kernel 2.6.22, Linux provides the (nonstandard) signalfd() system call,
which creates a special file descriptor from which signals directed to the caller can
be read. The signalfd mechanism provides an alternative to the use of sigwaitinfo()
for synchronously accepting signals.

#define _POSIX_C_SOURCE 199309
#include <signal.h>

int sigtimedwait(const sigset_t *set, siginfo_t *info,
const struct timespec *timeout);
Returns number of delivered signal on success,
or –1 on error or timeout (EAGAIN)

#include <sys/signalfd.h>

int signalfd(int fd, const sigset_t *mask, int flags);
Returns file descriptor on success, or –1 on error
Free download pdf