some-usable-scripts/C_Language/TP_SYS/tp_sema/sema.c

94 lines
1.6 KiB
C
Raw Normal View History

2011-03-10 14:08:58 +01:00
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
/*-----------------------------------------------------------*/
int creat_sem(key_t cle,int val)
/* creation du semaphore cle ayant comme valeur initiale val
retourne un identificateur de semaphore >=0 ou -1 si erreur
*/
{
int semid;
union semun{
int val;
struct semid_ds *buf;
ushort *array;
} arg;
if((semid=semget(cle,1,0666+IPC_CREAT+IPC_EXCL))>= 0)
{
arg.val=val;
if(semctl(semid,0,SETVAL,arg)< 0)
semid = -1;
}
return(semid);
}
/*--------------------------------------------------------------*/
int del_sem(key_t cle)
/* destruction du semaphore cle
retourne:
0 si la destruction du semaphore s est effectuee correctement
-1 si erreur
*/
{
return(semctl((semget(cle,0,0)),0,IPC_RMID,0));
}
/*---------------------------------------------------------------*/
int open_sem(key_t cle)
/* recherche le semaphore cle
retourne l'identificateur du semaphore >=0 ou -1 si erreur
*/
{
return(semget(cle,0,0));
}
/*----------------------------------------------------------------*/
void P(int semid)
{
struct sembuf psembuf;
psembuf.sem_op = -1;
psembuf.sem_flg = 0;
psembuf.sem_num = 0;
semop(semid,&psembuf,1);
}
/*------------------------------------------------------------------*/
void V(int semid)
{
struct sembuf vsembuf;
vsembuf.sem_op = 1;
vsembuf.sem_flg = 0;
vsembuf.sem_num = 0;
semop(semid,&vsembuf,1);
}
/*--------------------------------------------------------------*/