-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsem.h
More file actions
79 lines (68 loc) · 1.44 KB
/
psem.h
File metadata and controls
79 lines (68 loc) · 1.44 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Program: psem.h
* Purpose: Definition of the semun union and process semaphore operations.
*/
#include <unistd.h>
#include <sys/sem.h>
union semun {
int val; /* Value for SETVAL */
struct semid_ds *sem; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};
typedef struct psem {
int id, val;
unsigned key;
} psem_t;
int psem_init(psem_t *sem, unsigned key, int ival)
{
union semun argu;
sem->id = semget(key, 1, 0666|IPC_CREAT);
if(sem->id == -1)
return 0;
sem->key = key;
if(ival < 0)
return -1;
argu.val = ival;
if(semctl(sem->id, 0, SETVAL, argu) == -1)
return 0;
sem->val = ival;
return 1;
}
int psem_wait(psem_t *sem)
{
struct sembuf operation;
operation.sem_num = 0;
operation.sem_op = -1;
operation.sem_flg = SEM_UNDO;
if(semop(sem->id, &operation, 1) == -1)
return 0;
return 1;
}
int psem_post(psem_t *sem)
{
struct sembuf operation;
operation.sem_num = 0;
operation.sem_op = 1;
operation.sem_flg = SEM_UNDO;
if(semop(sem->id, &operation, 1) == -1)
return 0;
return 1;
}
int psem_holdon(psem_t *sem)
{
struct sembuf operation;
operation.sem_num = 0;
operation.sem_op = 0;
operation.sem_flg = 0;
if(semop(sem->id, &operation, 1) == -1)
return 0;
return 1;
}
int psem_destroy(psem_t *sem)
{
union semun no_argu;
if(semctl(sem->id, 0, IPC_RMID, no_argu) == -1)
return 0;
return 1;
}