Advertisement
Guest User

Untitled

a guest
May 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. Signal Handling -- signal()
  2. An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways:
  3.  
  4. The process can let the default action happen
  5. The process can block the signal (some signals cannot be ignored)
  6. the process can catch the signal with a handler.
  7. Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself
  8. Receiving signals is straighforward with the function:
  9.  
  10. int (*signal(int sig, void (*func)()))() -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise.
  11.  
  12.  
  13.  
  14.  
  15. func() can have three values:
  16.  
  17.  
  18. SIG_DFL
  19. -- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig.
  20. SIG_IGN
  21. -- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL).
  22. A function address
  23. -- a user specified function.
  24. SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file.
  25.  
  26.  
  27. Thus to ignore a ctrl-c command from the command line. we could do:
  28.  
  29.  
  30.    signal(SIGINT, SIG_IGN);
  31.  
  32.  
  33. TO reset system so that SIGINT causes a termination at any place in our program, we would do:
  34.  
  35.  
  36.    signal(SIGINT, SIG_DFL);
  37.  
  38.  
  39.  
  40.  
  41.  
  42. So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program:
  43.  
  44.  
  45.  
  46.  
  47. #include <stdio.h>
  48.  
  49. void sigproc(void);
  50.  
  51. void quitproc(void);
  52.  
  53. main()
  54. { signal(SIGINT, sigproc);
  55.          signal(SIGQUIT, quitproc);
  56.          printf(``ctrl-c disabled use ctrl- to quitn'');
  57.          for(;;); /* infinite loop */}
  58.  
  59. void sigproc()
  60. {        signal(SIGINT, sigproc); /*  */
  61.          /* NOTE some versions of UNIX will reset signal to default
  62.          after each call. So for portability reset signal each time */
  63.  
  64.          printf(``you have pressed ctrl-c n'');
  65. }
  66.  
  67. void quitproc()
  68. {        printf(``ctrl- pressed to quitn'');
  69.          exit(0); /* normal exit status */
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement