-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsemaphore.c
More file actions
187 lines (163 loc) · 4.57 KB
/
semaphore.c
File metadata and controls
187 lines (163 loc) · 4.57 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Groovit Copyright (C) 1998 Jean-Daniel PAUGET
* making accurate and groovy sound/noise
* groovit@disjunkt.com - http://groovit.disjunkt.com/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* you can also try the web at http://www.gnu.org/
*
* $Log: semaphore.c,v $
* Revision 1.2 2003/03/19 16:45:47 jd
* major changes everywhere while retrieving source history up to this almost final release
*
* Revision 1.2.0.24.0.2 1999/10/19 19:29:45 jd
* *** empty log message ***
*
* Revision 1.2.0.24.0.1 1999/10/11 18:36:29 jd
* *** empty log message ***
*
* Revision 1.2.0.24 1999/10/11 15:30:51 jd
* second pre-tel-aviv revision
*
* Revision 1.2.0.23.0.1 1999/09/15 10:35:48 jd
* *** empty log message ***
*
* Revision 1.2.0.23 1999/09/15 10:20:39 jd
* second pre tel-aviv public revision, for testing
*
* Revision 1.2.0.22.0.1 1999/08/25 09:17:49 jd
* Revision 1.1 1999/08/25 09:14:10 jd
* Initial revision
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including <sys/sem.h> */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int *array; /* array for GETALL, SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
int initinewsema ()
{ int semid;
union semun u;
if ((semid = semget (IPC_PRIVATE, 1, 0600)) == -1)
{
#ifdef DEBUGSEM
fprintf (stderr, "creation de semaphore merdu errno: %d\n", errno);
#endif
return (-1);
}
printf ("semaphore id: %d\n", semid);
u.val = 0;
if (semctl (semid, 0, SETVAL, u) < 0)
{ fprintf (stderr, "semaphore initialisation merdue: errno %d\n", errno);
fprintf (stderr, "continuons quand meme\n");
}
return semid;
}
int deletesema (int semid)
{
union semun u;
if (semctl (semid, 0, IPC_RMID, u) < 0)
{ fprintf (stderr, "erreur a la destruction du semaphore errno: %d\n", errno);
return (-1);
}
return 0;
}
int testlocksema (int semid)
{
struct sembuf soperations [3];
soperations[0].sem_num = 0;
soperations[0].sem_op = 0; /* on teste */
soperations[0].sem_flg = IPC_NOWAIT;
soperations[1].sem_num = 0;
soperations[1].sem_op = 1; /* on incremente */
soperations[1].sem_flg = IPC_NOWAIT;
if ((semop (semid , soperations, 2) < 0) && (errno==EAGAIN))
{
#ifdef DEBUGSEM
fprintf (stderr, "semaphore locke mais on continue\n");
#endif
return 0;
}
else
{
#ifdef DEBUGSEM
fprintf (stderr, "semaphore libre, donc j'ai du le locker...\n");
#endif
return 1;
}
}
int unlocksema (int semid)
{
struct sembuf soperations [3];
soperations[0].sem_num = 0;
soperations[0].sem_op = -1; /* on decremente */
soperations[0].sem_flg = IPC_NOWAIT;
if (semop (semid , soperations, 1) < 0)
{
// #ifdef DEBUGSEM
fprintf (stderr, "semaphore erreur a la decrementation ?? errno: %d\n", errno);
// #endif
return -1;
}
else
{
#ifdef DEBUGSEM
fprintf (stderr, "semaphore decremente\n");
#endif
return 0;
}
}
/*
int testsemaconcept (void)
{
int semid;
if ((semid = initinewsema ()) == -1) return (-1);
testlocksema (semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
testlocksema (semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
unlocksema(semid);
if (testlocksema (semid)) unlocksema(semid);
return (deletesema (semid));
}
int main (void)
{
int i;
for (i=0 ; 1 ; i++)
testsemaconcept ();
return 0;
}
*/