SHARE
TWEET

Untitled

a guest Nov 14th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------------------------------------------
  2. // A simplified demonstration of signal handling; part 3
  3.  
  4. #include <stdlib.h>         // Libraries which may be needed
  5. #include <stdio.h>
  6. #include <signal.h>
  7.  
  8. struct sigaction sigaction_structure;   // A data structure, filled in below
  9.  
  10. void broken();              // Declaration only - definition below
  11.  
  12.  
  13. //------------------------------------------------------------------------------
  14.  
  15. int num;                // A global variable
  16. int *ptr;               //  and a pointer
  17.  
  18. //------------------------------------------------------------------------------
  19.  
  20. int main (int argc, char *argv[])   // The 'root' programme; execution start
  21. {
  22.  
  23.   // Set three fields in a predefined 'struct'
  24.   sigaction_structure.sa_handler = &broken; // Pointer to a -function-
  25.   sigemptyset(&sigaction_structure.sa_mask);    // Call defines 'empty' field
  26.   sigaction_structure.sa_flags   = 0;       // No flags
  27.  
  28.   // Link the structure to signal "SIGSEGV"
  29.   sigaction(SIGSEGV, &sigaction_structure, NULL);   // Link
  30.   // Link the structure to signal "SIGINT"
  31.   sigaction(SIGINT, &sigaction_structure, NULL);    // Link
  32.   // Link the structure to signal "SIGTERM"
  33.   sigaction(SIGTERM, &sigaction_structure, NULL);   // Link
  34.   // Link the structure to signal "SIGSTP"
  35.   sigaction(SIGTSTP, &sigaction_structure, NULL);   // Link
  36.  
  37.  
  38.  
  39.  
  40.   num = 0;
  41.   while (num < 1000000000) num++;           // Quite a long loop!
  42.  
  43.   int *p = NULL;
  44.   printf("Trying to dereffrence  \n", *p);
  45.  
  46.   exit(0);
  47. }//main
  48.  
  49. //------------------------------------------------------------------------------
  50.  
  51. void broken(int signum) // The argument indicates which signal was activated
  52. {
  53.  
  54.  
  55.   switch (signum)   // signum values are defined in "signal.h"
  56.   {
  57.     case SIGINT:  printf("\nYou caused  SIGINT by CTRL-C \n"); break;
  58.     case SIGSEGV: printf("\nYou caused  SIGSEGV by a Segmentation Fault \n"); break;
  59.     case SIGTERM: printf("\nYou caused  SIGTERM by killing the process\n"); break;
  60.     case SIGTSTP: printf("\nYou caused  SIGSTP by CTRL-Z \n"); break;
  61.     default:      printf("You'll have to write some more traps.\n"); break;
  62.   }
  63.  
  64.   exit(1);
  65. }//broken
  66.  
  67. //------------------------------------------------------------------------------
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top