Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rdemeter
GitHub Repository: rdemeter/so
Path: blob/master/lab5/server_sem.c
224 views
1
#include <stdio.h>
2
#include <fcntl.h> /* For O_* constants */
3
#include <sys/stat.h> /* For mode constants */
4
#include <semaphore.h>
5
6
#define SEM_NAME "/my_semaphore"
7
8
int main(void)
9
{
10
sem_t *my_sem;
11
int rc, pvalue;
12
13
/* create semaphore with initial value of 1 */
14
my_sem = sem_open(SEM_NAME, O_CREAT, 0644, 1);
15
if(my_sem == SEM_FAILED) {
16
printf("sem_open failed\n");
17
return 0;
18
}
19
20
printf("Asteapta la semafor!\n");
21
22
/* get the semaphore */
23
sem_wait(my_sem);
24
25
/* do important stuff protected by the semaphore */
26
rc = sem_getvalue(my_sem, &pvalue);
27
printf("sem is %d\n", pvalue);
28
29
getchar();
30
31
/* release the lock */
32
sem_post(my_sem);
33
34
rc = sem_close(my_sem);
35
if(rc == -1)
36
printf("sem_close failed\n");
37
38
rc = sem_unlink(SEM_NAME);
39
if(rc == -1)
40
printf("sem_unlink failed\n");
41
42
return 0;
43
}
44
45