Advertisement
heavenriver

semaphores.c

Nov 27th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. /* basic semaphore operations, 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 <sys/ipc.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 RSIZE 20 // reply size
  32.  # define exception(x) { puts(x); exit(1); }
  33.  
  34.  /* semaphore buffer */
  35.  typedef struct {
  36.    unsigned short num;
  37.    short op;
  38.    short flag;
  39.  } sembuf;
  40.  
  41.  /* unsigned semaphore */
  42.  struct sem_uns {
  43.    int val;
  44.    unsigned short * array;
  45.  };
  46.  
  47.  int main(int argc, char * argv[])
  48.     {
  49.      /* type of basic semaphore operations:
  50.       *
  51.       * wait(sem_descr, sem_num)
  52.       *     {
  53.       *     struct sembuf operations[1] = {{ sem_num, -1, 0 }};
  54.       *     return sem_operation(sem_descr, operations, 1);
  55.       *     }
  56.       *
  57.       * signal(...) does the same, but with +1 in operations[].
  58.       */
  59.      
  60.      /* SEE MEMSHARE.C
  61.       *
  62.       * >> Sample at: pg. 45
  63.       * >> One function for the memory writer, one for the reader, both in a do-while(!quit)
  64.       * >>
  65.       * >> writer() contains shmat(), puts("please input"), scanf() and strcpy() (last two in do-while)
  66.       * >> reader() contains shmat(), printf() (in do-while)
  67.       * >>
  68.       * >> main() contains: shmget(), and then
  69.       * >>  if(fork()) wait(&status); else writer(shm_descriptor);
  70.       * >>  if(fork()) wait(&status); else reader(shm_descriptor);
  71.       * >>
  72.       * >> ...And then shmctl(IPC_RMID).
  73.       *
  74.       * The difference with semaphores:
  75.       * 1) sem_num/op/flag = 0/-1/0 & semop(ID, &op, 1) AFTER the writer() loop (= signal())
  76.       * 2) sem_num/op/flag = 0/0/0 & semop(ID, &op, 1) BEFORE the reader() loop (= wait())
  77.       *
  78.       * The main() uses shmget, semget, semctl (when necessary) and forks just like in MESSAGES.C
  79.       * Then removes the memory (shmctl) and semaphore (semctl) and quits
  80.       */
  81.      return 0;
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement