Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/sem.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <semaphore.h>
- #define CLE_UNIQUE 1234
- #define NUM_SEMAPHORE 0
- /*struct sembuf
- {
- unsigned short sem_num;
- short sem_op;
- short sem_flag;
- };*/
- union semun
- {
- int val;
- struct semid_ds * buf;
- ushort * array;
- };
- int main(int argc, char* argv[])
- {
- int f = 0;
- union semun init_jeton, tester_jeton;
- init_jeton.val = 1;
- tester_jeton.val = 0;
- struct sembuf prendre_jeton = { NUM_SEMAPHORE, -1, IPC_NOWAIT};
- struct sembuf poser_jeton = { NUM_SEMAPHORE, 1, IPC_NOWAIT};
- int id_semaphore = semget(CLE_UNIQUE, 1, IPC_CREAT | 0666);
- f = fork();
- for(int i = 0; i<5 ; i++)
- {
- while(semctl(id_semaphore, 0 , GETVAL, tester_jeton) < 1)
- {
- printf("id : %d", f);
- }
- semop(id_semaphore, &prendre_jeton, 1);
- printf("id : %d", f);
- semop(id_semaphore, &poser_jeton, 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement