-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem.c
61 lines (44 loc) · 1008 Bytes
/
sem.c
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
/* Routines for hanling mmap data and semaphores */
#include "defs.h"
#include "ext.h"
static int semid[7] = { -1, -1, -1, -1, -1, -1, -1 };
int msem_init (int *sem, int val)
{
*sem = val;
return 1;
}
/* lock a semaphore */
int msem_lock (which, unused)
int *which;
int unused;
{
struct sembuf lock[2];
if (semid[*which] < 0)
{
/* 1000 + which makes for an easy semkey. Easy to change, too */
if ((semid[*which] = semget (1000 + *which, 1, IPC_CREAT | 0664)) < 0)
return -1;
}
lock[0].sem_num = 0;
lock[0].sem_op = 0;
lock[0].sem_flg = 0;
lock[1].sem_num = 0;
lock[1].sem_op = 1; /* inc by 1 */
lock[1].sem_flg = SEM_UNDO;
if (semop (semid[*which], &lock[0], 2) < 0)
return -1;
return 0;
}
/* unlock */
int msem_unlock (which, unused)
int *which;
int unused;
{
struct sembuf unlock;
unlock.sem_num = 0;
unlock.sem_op = -1;
unlock.sem_flg = SEM_UNDO | IPC_NOWAIT;
if (semop (semid[*which], &unlock, 1) < 0)
return -1;
return 0;
}