-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphores.c
204 lines (172 loc) · 4.82 KB
/
semaphores.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
//create user defined semun for initializing the semaphores
void *Thread1(void *arg)
{
int semid;
semid = (int)arg;
//in order to perform the operations on semaphore
// first need to define the sembuf object
struct sembuf op1,op2;
//operation for 0th semaphore
op1.sem_num = 0; //signifies 0th semaphore
op1.sem_op = -1; //reduce the semaphore count to lock
op1.sem_flg = 0; //wait till we get lock on semaphore
//operation for 1th semaphore
op2.sem_num = 1; //signifies 0th semaphore
op2.sem_op = -1; //reduce the semaphore count to lock
op2.sem_flg = 0; //wait till we get lock on semaphore
//locking the 0th semaphore
if (semop(semid,&op1,1) == -1)
{
perror("Thread1:semop failure Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread1:Successfully locked 0th semaphore\n");
//lock the 1th semaphore
if (semop(semid,&op2,1) == -1)
{
perror("Thread1:semop failure Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread1:Successfully locked 1th semaphore\n");
//release the 0th semaphore
op1.sem_num = 0; //signifies 0th semaphore
op1.sem_op = 1; //reduce the semaphore count to lock
op1.sem_flg = 0; //wait till we get lock on semaphore
if (semop(semid,&op1,1) == -1)
{
perror("Thread1:semop failure Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread1:Successfully unlocked 0th semaphore\n");
//release the 1th semaphore
op2.sem_num = 1; //signifies 0th semaphore
op2.sem_op = 1; //reduce the semaphore count to lock
op2.sem_flg = 0; //wait till we get lock on semaphore
if (semop(semid,&op2,1) == -1)
{
perror("Thread1:semop failure Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread1:Successfully unlocked 1th semaphore\n");
}
void *Thread2(void *arg)
{
int semid;
semid = (int)arg;
//in order to perform the operations on semaphore
// first need to define the sembuf object
struct sembuf op1,op2;
//operation for 0th semaphore
op1.sem_num = 0; //signifies 0th semaphore
op1.sem_op = -1; //reduce the semaphore count to lock
op1.sem_flg = 0; //wait till we get lock on semaphore
//operation for 1th semaphore
op2.sem_num = 1; //signifies 0th semaphore
op2.sem_op = -1; //reduce the semaphore count to lock
op2.sem_flg = 0; //wait till we get lock on semaphore
//lock the 0th semaphore
if (semop(semid,&op1,1) == -1)
{
perror("Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread2:Successfully locked 0th semaphore\n");
//lock the 1th semaphore
if (semop(semid,&op2,1) == -1)
{
perror("Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread2:Successfully locked 1th semaphore\n");
//release 0th semaphore
op1.sem_num = 0; //signifies 0th semaphore
op1.sem_op = 1; //reduce the semaphore count to lock
op1.sem_flg = 0; //wait till we get lock on semaphore
if (semop(semid,&op1,1) == -1)
{
perror("Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread2:Successfully unlocked 0th semaphore\n");
//release the 1th semaphore
op2.sem_num = 1; //signifies 0th semaphore
op2.sem_op = 1; //reduce the semaphore count to lock
op2.sem_flg = 0; //wait till we get lock on semaphore
if (semop(semid,&op2,1) == -1)
{
perror("Reason:");
exit(-1);
}
else
fprintf(stderr,"Thread2:Successfully unlocked 1th semaphore\n");
}
int main()
{
pthread_t tid1,tid2;
int semid;
//create user defined semun for initializing the semaphores
typedef union semun
{
int val;
struct semid_ds *buf;
ushort * array;
}semun_t;
semun_t arg;
semun_t arg1;
//creating semaphore object with two semaphore in a set
//viz 0th & 1th semaphore
semid = semget(IPC_PRIVATE,2,0666|IPC_CREAT);
if(semid<0)
{
perror("semget failed Reason:");
exit(-1);
}
//initialize 0th semaphore in the set to value 1
arg.val = 1;
if ( semctl(semid,0,SETVAL,arg)<0 )
{
perror("semctl failure Reason:");
exit(-1);
}
//initialize 1th semaphore in the set to value 1
arg1.val = 1;
if( semctl(semid,1,SETVAL,arg1)<0 )
{
perror("semctl failure Reason: ");
exit(-1);
}
//create two threads to work on these semaphores
if(pthread_create(&tid1, NULL,Thread1, semid))
{
printf("\n ERROR creating thread 1");
exit(1);
}
if(pthread_create(&tid2, NULL,Thread2, semid) )
{
printf("\n ERROR creating thread 2");
exit(1);
}
//waiting on these threads to complete
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
//once done clear the semaphore set
if (semctl(semid, 1, IPC_RMID ) == -1 )
{
perror("semctl failure while clearing Reason:");
exit(-1);
}
//exit the main threads
pthread_exit(NULL);
return 0;
}