1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   | /*
 * blocksignal.c -  Blocage de signal avec sigprocmask()
 *
 *                  Jeudi 15 avril 2010 01h35
 *                  http://www.developpez.net/forums/d907240/general-developpement/programmation-linux/masquer-signal/
 */
 
#define _XOPEN_SOURCE 600 
 
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
 
void handler (int x)
{
    x=x; /* Utilisation du paramètre pour éviter les warnings */
 
    printf ("SIGINT reçu. On sort.\n");
    exit (1);
}
 
int main (void)
{
    sigset_t ss;
 
    printf ("Bonjour. Mon PID est %d.\n",(int)getpid());
 
    signal (SIGINT,handler);
 
    sigemptyset (&ss);
    sigaddset   (&ss,SIGINT);
 
    sigprocmask (SIG_BLOCK,&ss,NULL);
    printf ("SIGINT bloqué pour 10 secondes.\n");
    sleep (10);
 
    sigprocmask (SIG_UNBLOCK,&ss,NULL);
    printf ("SIGINT débloqué. Attente de 10 nouvelles secondes.\n");
    sleep (10);
 
    printf ("Sortie normale.\n");
 
    return 0;
} | 
Partager