1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void opSem(int sem,int i) {
struct sembuf op;
op.sem_num =0;
op.sem_op = i;
op.sem_flg = 0;
}
int main() {
int semA,semB;
//creation des semaphore
semA = semget(123,1,0666 | IPC_CREAT);
semB = semget(125,1,0666 | IPC_CREAT);
//initiation des semaphores
semctl(semA,0,SETVAL,1);
semctl(semB,0,SETVAL,0);
if(fork()==0) {
while(1) {
//operation down sur semaphore A
opSem(semA,-1);
prinft("A ");
//operation up sur semaphore B
opSem(semB,1);
}
exit(0);
}
if(fork()==0) {
while(1) {
//operation down sur semaphore B
opSem(semB,-1);
prinft("B ");
//operation up sur semaphore A
opSem(semA,1);
}
exit(0);
}
while(waipid(-1,NULL,WNOHANG) > 0) { }
semctl(semA,0,IPC_RMID,0);
semctl(semA,0,IPC_RMID,0);
exit(0); |