Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 14th, 2012  |  syntax: C  |  size: 2.66 KB  |  views: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /************************************************************************/
  2. /*      Operating Systems - Fall
  3. /*      Originally developed at KSU by a teaching assistant             */
  4. /*                                                                      */
  5. /*      Description :  The following library is a collection of         */
  6. /*                     routines for using binary semaphores in C:       */
  7. /*                      1. seminit - to initialize a semaphore.         */
  8. /*                      2. P - to perform a P(S) (wait) operation.      */
  9. /*          3. V - to perform a V(S) (signal) operation.        */
  10. /*                  4. semkill - to remove a semaphore              */
  11. /*                                                          */
  12. /*                     These routines call system routines:             */
  13. /*                      1. semget - to get a semaphore                  */
  14. /*                      2. semctl - semaphore control operations        */
  15. /*                      3. semop  - semaphore operations                */
  16. /*                                                                      */
  17. /*                     Complete manual entries can be obtained by:      */
  18. /*                      man semctl | col -b | lpr                       */
  19. /************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/ipc.h>
  24. #include <sys/sem.h>
  25.  
  26. union arg{                      /* This structure is used to call semctl */
  27.         int val;               
  28.         struct semid_ds *buf;
  29.         char *array;
  30. };
  31.  
  32. /*
  33.  * Create semaphore based on "key" parameter to "initval"
  34.  */
  35.  
  36.  
  37. void sem_create(int semid, int initval)
  38. {
  39.  int semval;
  40. union semun
  41. {
  42.  int val;
  43.  struct semid_ds *buf;
  44.  unsigned short *array;
  45. }s;
  46.  
  47. s.val=initval;
  48. if((semval=semctl(semid,0,SETVAL,s))<0)
  49.   printf("\n Erroe in executing semctl");
  50. }
  51.  
  52. /*
  53.  * Remove semaphore with semaphore id (sid) from the kernel
  54.  */
  55. static void semkill (sid)
  56. int sid;
  57. {
  58.     if (semctl(sid,0,IPC_RMID,0) == -1)
  59.         perror("semctl (kill)");
  60.     printf("Semaphore with value of sid = %d is killed \n",sid);
  61. }
  62.  
  63. /*
  64.  * Perform the designated "op" operation on the semaphore. If "op" is -1,
  65.  * then this implements the "P" operation; it decrements the value of the
  66.  * semaphore (semval) if it was >0,
  67.  * and blocks the caller if it was zero (semval==0)
  68.  * If "op" is 1, then this is simply added to current value of
  69.  * the semaphore ("V" operation).
  70.  */
  71. static void semcall(sid, op)
  72. int sid;
  73. int op;
  74. {
  75.     struct sembuf sb;
  76.  
  77.     sb.sem_num = 0; /* semaphore number within sid */
  78.     sb.sem_op = op;
  79.     sb.sem_flg = 0; /* blocking call */
  80.     if (semop(sid, &sb, 1) == -1)
  81.         perror("semop");
  82. }
  83.  
  84. /*
  85.  * P operation on semaphore "sid". Should be called upon entry to critical
  86.  * region.
  87.  */
  88. static void P(sid)
  89. int sid;
  90. {
  91.     semcall(sid, -1);
  92. }
  93.  
  94. /*
  95.  * V operation on semaphore "sid". Should be called upon exit from critical
  96.  * region.
  97.  */
  98. static void V(sid)
  99. int sid;
  100. {
  101.     semcall(sid, 1);
  102. }