The Linux Programming Interface

(nextflipdebug5) #1
Signals: Advanced Features 459

Listing 22-2: Using sigqueue() to send realtime signals


–––––––––––––––––––––––––––––––––––––––––––––––––––––– signals/t_sigqueue.c
#define _POSIX_C_SOURCE 199309
#include <signal.h>
#include "tlpi_hdr.h"


int
main(int argc, char *argv[])
{
int sig, numSigs, j, sigData;
union sigval sv;


if (argc < 4 || strcmp(argv[1], "--help") == 0)
usageErr("%s pid sig-num data [num-sigs]\n", argv[0]);


/ Display our PID and UID, so that they can be compared with the
corresponding fields of the siginfo_t argument supplied to the
handler in the receiving process
/


printf("%s: PID is %ld, UID is %ld\n", argv[0],
(long) getpid(), (long) getuid());


sig = getInt(argv[2], 0, "sig-num");
sigData = getInt(argv[3], GN_ANY_BASE, "data");
numSigs = (argc > 4)? getInt(argv[4], GN_GT_0, "num-sigs") : 1;


for (j = 0; j < numSigs; j++) {
sv.sival_int = sigData + j;
if (sigqueue(getLong(argv[1], 0, "pid"), sig, sv) == -1)
errExit("sigqueue %d", j);
}


exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––– signals/t_sigqueue.c


The value argument specifies the data to accompany the signal. This argument has
the following form:


union sigval {
int sival_int; /* Integer value for accompanying data */
void *sival_ptr; /* Pointer value for accompanying data */
};

The interpretation of this argument is application-dependent, as is the choice of
whether to set the sival_int or the sival_ptr field of the union. The sival_ptr field is
seldom useful with sigqueue(), since a pointer value that is useful in one process is
rarely meaningful in another process. However, this field is useful in other functions
that employ sigval unions, as we’ll see when we consider POSIX timers in Section 23.6
and POSIX message queue notification in Section 52.6.


Several UNIX implementations, including Linux, define a sigval_t data type as a
synonym for union sigval. However, this type is not specified in SUSv3 and is not
available on some implementations. Portable applications should avoid using it.
Free download pdf