Advertisement
heavenriver

signals.c

Nov 27th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1.  /* basic signals, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <stdlib.h> // for exit
  25.  # include <signal.h>
  26.  # include <sys/shm.h> // for shmdet
  27.  # include <sys/sem.h>
  28.  # include <fcntl.h> // for O_RDWR
  29.  
  30.  # define BUFFER 256
  31.  # define exception(x) { puts(x); exit(1); }
  32.  
  33.  void timeout();
  34.  char c; // input char
  35.  
  36.  int main(int argc, char * argv[])
  37.     {
  38.      /* ALARM example; NOTE: DOES NOT STOP */
  39.      alarm(2); // 2t left before alarm
  40.      signal(SIGALRM, timeout);
  41.      printf("Alarm signal sent\n");
  42.      // while(1) read(0, &c, 1); // reads chars from input
  43.      
  44.      /* KILL example */
  45.      int pid; // ID of the process to be killed
  46.      printf("Enter server PID: ");
  47.      scanf("%d", &pid);
  48.      kill(pid, SIGTERM);
  49.     }
  50.  
  51.   /* waits on timeout, then signals */
  52.   void timeout()
  53.     {
  54.      printf("Wake up call\n");
  55.      signal(SIGALRM, timeout); // sends timeout; NOTE: the wakeup executes at regular intervals until the PID is entered, possibly due to recursion?
  56.      alarm(2);
  57.     }
  58.    
  59.  /* samples at pgs. 63-64 with server-client model */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement