Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /* After the #includes, the function prototypes and the global variable, we come to the main function. There the semaphore is created with a call to semget, which returns the semaphore ID. If the program is the first to be called (i.e. it's called with a parameter and argc > 1), a call is made to set_semvalue to initialize the semaphore and op_char is set to X. */
  2.  
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/sem.h>
  9. //#include "semun1.h"
  10.  
  11. static int set_semvalue(void);
  12. static void del_semvalue(void);
  13. static int semaphore_p(void);
  14. static int semaphore_v(void);
  15. static int sem_id;
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. int i;
  20. int pause_time;
  21. char op_char = 'O';
  22. srand((unsigned int)getpid());
  23. sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT);
  24.  
  25. if (argc > 1) {
  26. if (!set_semvalue())
  27. {
  28. fprintf(stderr, "Failed to initialize semaphore\n");
  29. exit(EXIT_FAILURE);
  30. }
  31.  
  32. op_char = 'X';
  33. sleep(2);
  34.  
  35. }
  36.  
  37.  
  38. /* Then we have a loop which enters and leaves the critical section ten times. There, we first make a call
  39. to semaphore_p which sets the semaphore to wait, as this program is about to enter the critical section.
  40. */
  41.  
  42. for(i = 0; i < 10; i++) {
  43. if (!semaphore_p()) exit(EXIT_FAILURE);
  44. printf("%c", op_char);
  45. fflush(stdout);
  46. pause_time = rand() % 3;
  47. sleep(pause_time);
  48. printf("%c", op_char); fflush(stdout);
  49.  
  50.  
  51. /* After the critical section, we call semaphore_v, setting the semaphore available, before going through the for loop again after a random wait. After the loop, the call to del_semvalue is made to clean up the code. */
  52.  
  53.  
  54. if (!semaphore_v()) exit(EXIT_FAILURE);
  55.  
  56. pause_time = rand() % 2;
  57. sleep(pause_time);
  58.  
  59. }
  60.  
  61. printf("\n%d - finished\n", getpid());
  62.  
  63. if (argc > 1)
  64. {
  65. sleep(10);
  66. del_semvalue();
  67. }
  68.  
  69. exit(EXIT_SUCCESS);
  70. }
  71.  
  72.  
  73.  
  74. /* The function set_semvalue initializes the semaphore using the SETVAL command in a
  75. semctl call. We need to do this before we can use the semaphore. */
  76.  
  77.  
  78. static int set_semvalue(void)
  79. {
  80. union semun sem_union;
  81.  
  82. sem_union.val = 1;
  83. if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0);
  84. return(1);
  85. }
  86.  
  87.  
  88. /* The del_semvalue function has almost the same form, except the call to semctl uses
  89. the command IPC_RMID to remove the semaphore's ID. */
  90.  
  91. static void del_semvalue(void)
  92. {
  93. union semun sem_union;
  94.  
  95. if(semctl(sem_id,0, IPC_RMID, sem_union) == -1)
  96. fprintf(stderr, "Failed to delete semaphore\n");
  97. }
  98.  
  99. static int semaphore_p(void) {
  100. struct sembuf sem_b;
  101.  
  102.  
  103. sem_b.sem_num = 0;
  104. sem_b.sem_op = -1; /* P() */
  105. sem_b.sem_flg = SEM_UNDO;
  106.  
  107. if (semop(sem_id, &sem_b, 1) == -1)
  108. {
  109. fprintf(stderr, "semaphore_p failed\n");
  110. return(0);
  111. }
  112. return(1);
  113. }
  114.  
  115.  
  116. static int semaphore_v(void)
  117. {
  118. struct sembuf sem_b;
  119.  
  120. sem_b.sem_num = 0;
  121.  
  122. sem_b.sem_op = 1; /* V() */
  123. sem_b.sem_flg = SEM_UNDO;
  124. if (semop(sem_id, &sem_b, 1) == -1)
  125. {
  126. fprintf(stderr, "semaphore_v failed\n");
  127. return(0);
  128. }
  129. return(1);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement