Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <sys/sem.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <semaphore.h>
  11.  
  12. #define CLE_UNIQUE 1234
  13. #define NUM_SEMAPHORE 0
  14.  
  15. /*struct sembuf
  16. {
  17. unsigned short sem_num;
  18. short sem_op;
  19. short sem_flag;
  20. };*/
  21.  
  22. union semun
  23. {
  24. int val;
  25. struct semid_ds * buf;
  26. ushort * array;
  27. };
  28.  
  29.  
  30.  
  31. int main(int argc, char* argv[])
  32. {
  33. int f = 0;
  34.  
  35. union semun init_jeton, tester_jeton;
  36. init_jeton.val = 1;
  37. tester_jeton.val = 0;
  38. struct sembuf prendre_jeton = { NUM_SEMAPHORE, -1, IPC_NOWAIT};
  39. struct sembuf poser_jeton = { NUM_SEMAPHORE, 1, IPC_NOWAIT};
  40. int id_semaphore = semget(CLE_UNIQUE, 1, IPC_CREAT | 0666);
  41. f = fork();
  42.  
  43. for(int i = 0; i<5 ; i++)
  44. {
  45. while(semctl(id_semaphore, 0 , GETVAL, tester_jeton) < 1)
  46. {
  47. printf("id : %d", f);
  48. }
  49. semop(id_semaphore, &prendre_jeton, 1);
  50. printf("id : %d", f);
  51. semop(id_semaphore, &poser_jeton, 1);
  52. }
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement