The Linux Programming Interface

(nextflipdebug5) #1

466 Chapter 22


z Loop until gotSigquit is set r. Each loop iteration performs the following steps:


  • Display the current value of the signal mask using our printSigMask() function.

  • Simulate a critical section by executing a CPU busy loop for a few seconds.

  • Display the mask of pending signals using our printPendingSigs() function
    (Listing 20-4).

  • Uses sigsuspend() to unblock SIGINT and SIGQUIT and wait for a signal (if one
    is not already pending).
    z Use sigprocmask() to restore the process signal mask to its original state t, and
    then display the signal mask using printSigMask() y.


Listing 22-5: Using sigsuspend()
–––––––––––––––––––––––––––––––––––––––––––––––––––– signals/t_sigsuspend.c
#define _GNU_SOURCE /* Get strsignal() declaration from <string.h> */
#include <string.h>
#include <signal.h>
#include <time.h>
#include "signal_functions.h" /* Declarations of printSigMask()
and printPendingSigs() */
#include "tlpi_hdr.h"

static volatile sig_atomic_t gotSigquit = 0;

static void
handler(int sig)
{
printf("Caught signal %d (%s)\n", sig, strsignal(sig));
/* UNSAFE (see Section 21.1.2) */
if (sig == SIGQUIT)
gotSigquit = 1;
}

int
main(int argc, char *argv[])
{
int loopNum;
time_t startTime;
sigset_t origMask, blockMask;
struct sigaction sa;

q printSigMask(stdout, "Initial signal mask is:\n");

sigemptyset(&blockMask);
sigaddset(&blockMask, SIGINT);
sigaddset(&blockMask, SIGQUIT);
w if (sigprocmask(SIG_BLOCK, &blockMask, &origMask) == -1)
errExit("sigprocmask - SIG_BLOCK");

sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = handler;
Free download pdf