Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------
- // A simplified demonstration of signal handling; part 3
- #include <stdlib.h> // Libraries which may be needed
- #include <stdio.h>
- #include <signal.h>
- struct sigaction sigaction_structure; // A data structure, filled in below
- void broken(); // Declaration only - definition below
- //------------------------------------------------------------------------------
- int num; // A global variable
- int *ptr; // and a pointer
- //------------------------------------------------------------------------------
- int main (int argc, char *argv[]) // The 'root' programme; execution start
- {
- // Set three fields in a predefined 'struct'
- sigaction_structure.sa_handler = &broken; // Pointer to a -function-
- sigemptyset(&sigaction_structure.sa_mask); // Call defines 'empty' field
- sigaction_structure.sa_flags = 0; // No flags
- // Link the structure to signal "SIGSEGV"
- sigaction(SIGSEGV, &sigaction_structure, NULL); // Link
- // Link the structure to signal "SIGINT"
- sigaction(SIGINT, &sigaction_structure, NULL); // Link
- // Link the structure to signal "SIGTERM"
- sigaction(SIGTERM, &sigaction_structure, NULL); // Link
- // Link the structure to signal "SIGSTP"
- sigaction(SIGTSTP, &sigaction_structure, NULL); // Link
- num = 0;
- while (num < 1000000000) num++; // Quite a long loop!
- int *p = NULL;
- printf("Trying to dereffrence \n", *p);
- exit(0);
- }//main
- //------------------------------------------------------------------------------
- void broken(int signum) // The argument indicates which signal was activated
- {
- switch (signum) // signum values are defined in "signal.h"
- {
- case SIGINT: printf("\nYou caused SIGINT by CTRL-C \n"); break;
- case SIGSEGV: printf("\nYou caused SIGSEGV by a Segmentation Fault \n"); break;
- case SIGTERM: printf("\nYou caused SIGTERM by killing the process\n"); break;
- case SIGTSTP: printf("\nYou caused SIGSTP by CTRL-Z \n"); break;
- default: printf("You'll have to write some more traps.\n"); break;
- }
- exit(1);
- }//broken
- //------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement