forked from sudikrt/assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemphore.c
37 lines (34 loc) · 907 Bytes
/
semphore.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
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <error.h>
#include <sys/types.h>
#include <semaphore.h>
#include <stdlib.h>
#include <string.h>
sem_t sem;
int count = 0;
void* fun (void* dat)
{
int c = *((int *) dat);
printf ("%d variable is waiting\n", c);
sem_wait (&sem);
printf ("%d is entered\n", c);
printf ("%d thread is with counter value %d\n", c, count);
count++;
printf ("%d thread is with new counter value %d\n", c, count);
printf ("Exiting\n");
sem_post (&sem);
}
int main ()
{
pthread_t id1, id2;
int a = 1, b = 2;
sem_init(&sem, 0, 1);
pthread_create (&id1, NULL, &fun, (void *) &a);
pthread_create (&id2, NULL, &fun, (void *) &b);
pthread_join (id1, NULL);
pthread_join (id2, NULL);
sem_destroy (&sem);
return 0;
}