Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1.  
  2. #define _POSIX_C_SOURCE 200809L
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <pthread.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9. void* gestoreSIGUSR(void* arg) {
  10.    
  11.     /* Creo la maschera dei segnali */
  12.     sigset_t setSIGUSR, setFULL; int sig;
  13.     sigemptyset(&setSIGUSR);
  14.     sigaddset(&setSIGUSR, SIGUSR1);
  15.     sigfillset(&setFULL);
  16.    
  17.     /* Maschero i segnali */
  18.     pthread_sigmask(SIG_SETMASK,&setFULL,NULL);
  19.    
  20.     for (;;) {
  21.         /* Attendo un segnale */
  22.         sigwait(&setSIGUSR,&sig);
  23.         printf("\nSignal received: Svolgo operazioni...\n");
  24.        
  25.         /* COMPIE OPERAZIONI */
  26.     }
  27.    
  28.     return NULL;
  29. }
  30.  
  31. void* gestoreCleanup(void* arg) {
  32.    
  33.     /* Creo la maschera dei segnali */
  34.     sigset_t setEND, setFULL; int sig;
  35.     sigemptyset(&setEND);
  36.     sigaddset(&setEND, SIGINT);
  37.     sigaddset(&setEND, SIGTERM);
  38.     sigaddset(&setEND, SIGQUIT);
  39.     sigfillset(&setFULL);
  40.    
  41.     /* Maschero i segnali */
  42.     pthread_sigmask(SIG_SETMASK,&setFULL,NULL);
  43.    
  44.     /* Attendo una terminazione */
  45.     sigwait(&setEND,&sig);
  46.     printf("\nSignal received: Svolgo cleanup...\n");
  47.    
  48.     /* Termino il thread gestore dei SIGUSR */
  49.     pthread_cancel(*(pthread_t*)arg);
  50.    
  51.     /* SVOLGO IL CLEANUP */
  52.    
  53.     /* Termino il processo */
  54.     exit(EXIT_SUCCESS);
  55.     return NULL;
  56. }
  57.  
  58. int main(int argc, char **argv)
  59. {
  60.    
  61.     sigset_t setFULL;
  62.     sigfillset(&setFULL);
  63.    
  64.     /* Maschero i segnali */
  65.     pthread_sigmask(SIG_SETMASK,&setFULL,NULL);
  66.    
  67.     /* [***]  Creo il thread gestore di SIGUSR ↓ ↓ ↓ ↓ ↓ */
  68.     pthread_t tidGestSIGUSR;
  69.    
  70.     /*      Imposto gli attributi */
  71.     pthread_attr_t attrGestSIGUSR;
  72.     if (pthread_attr_init(&attrGestSIGUSR))
  73.         { fprintf(stderr, "pthread_attr_init &attrGestSIGUSR FALLITA\n"); return -1; }
  74.     if (pthread_attr_setdetachstate(&attrGestSIGUSR, PTHREAD_CREATE_DETACHED))
  75.         { fprintf(stderr, "pthread_attr_setdetachstate &attrGestSIGUSR FALLITA\n"); return -1; }
  76.    
  77.     /*      Lancio il thread */
  78.     if(pthread_create(&tidGestSIGUSR, &attrGestSIGUSR, &gestoreSIGUSR, NULL)) {
  79.         fprintf(stderr, "pthread_create Gestore SIGUSR FALLITA\n");
  80.         return -1;
  81.     } else {
  82.         //pthread_detach(tidGestSIGUSR);
  83.         pthread_attr_destroy(&attrGestSIGUSR);
  84.     } /* ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ */
  85.    
  86.    
  87.     /*  [***] Creo il thread gestore del cleanup ↓ ↓ ↓ ↓ ↓ */
  88.     pthread_t tidGestCLEANUP;
  89.    
  90.     /*      Imposto gli attributi */
  91.     pthread_attr_t attrGestCleanup;
  92.     if (pthread_attr_init(&attrGestCleanup))
  93.         { fprintf(stderr, "pthread_attr_init &attrGestCleanup FALLITA\n"); return -1; }
  94.     if (pthread_attr_setdetachstate(&attrGestCleanup, PTHREAD_CREATE_DETACHED))
  95.         { fprintf(stderr, "pthread_attr_setdetachstate &attrGestCleanup FALLITA\n"); return -1; }
  96.    
  97.     /*      Lancio il thread */
  98.     if(pthread_create(&tidGestCLEANUP, NULL, &gestoreCleanup, (void*)&tidGestSIGUSR)) {
  99.         fprintf(stderr, "pthread_create Gestore Cleanup FALLITA\n");
  100.         return -1;
  101.     } else {
  102.         //pthread_detach(tidGestCLEANUP);
  103.         pthread_attr_destroy(&attrGestCleanup);
  104.     } /* ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ */
  105.    
  106.    
  107.     pthread_join(tidGestCLEANUP, NULL);
  108.    
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement