-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleeping.c
More file actions
156 lines (135 loc) · 3.48 KB
/
sleeping.c
File metadata and controls
156 lines (135 loc) · 3.48 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
//Sleeping Barber Problem
//---------------------------------------
/* Sooraj K S
S5 CSE
Roll No. 55
GEC Thrissur */
/* Reference : Modern Operating System ,
Andrew S.Tanenbaum */
/* You need to compile the program like 'cc file_name.c -lpthread'
and then run it './a.out' */
#include <stdio.h>
#include<pthread.h>
#include <stdlib.h>
#define seats 6 /*chairs for waiting customers*/
void *customer();
void *barberShop();
void *waiting_Room();
void checkQueue();
pthread_mutex_t queue = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t wait = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t sleepa = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t barberSleep = PTHREAD_COND_INITIALIZER;
pthread_cond_t barberWorking = PTHREAD_COND_INITIALIZER;
int returnTime=5,current=0,urakam =0, asdfgh;
int main(int argc, char *argv[])
{
asdfgh=time(NULL);
srand(asdfgh);
//declare barber thread;
pthread_t barber,customerM,timer_thread;
pthread_attr_t barberAttr, timerAttr;
pthread_attr_t customerMAttr;
//define barber, and cutomerMaker default attributes
pthread_attr_init(&timerAttr);
pthread_attr_init(&barberAttr);
pthread_attr_init(&customerMAttr);
printf("\n");
//create cutomerMaker
pthread_create(&customerM,&customerMAttr,customer,NULL);
//create barber
pthread_create(&barber,&barberAttr,barberShop,NULL);
pthread_join(barber,NULL);
pthread_join(customerM,NULL);
return 0;
}
void *customer()
{
int i=0;
printf("*Customer Maker Created*\n\n");
fflush(stdout);
pthread_t customer[seats+1];
pthread_attr_t customerAttr[seats+1];
while(i<(seats+1)) /*if there are no free chairs, leave*/
{
i++;/*increment count of waiting customer*/
pthread_attr_init(&customerAttr[i]);
while(rand()%2!=1)
{
sleep(1);/*go to sleep*/
}
pthread_create(&customer[i],&customerAttr[i],waiting_Room,NULL);
}
pthread_exit(0);/*shop is full,do not wait*/
}
void *waiting_Room()
{
//take seat
pthread_mutex_lock(&queue);
checkQueue();
sleep(returnTime);
waiting_Room();
}
void *barberShop()
{
int loop=0;
printf("The barber has opened the store.\n");
fflush(stdout);
while(loop==0)
{
if(current==0)
{
printf("\tThe shop is empty, barber is sleeping.\n");
fflush(stdout);
pthread_mutex_lock(&sleepa);
urakam=1;
pthread_cond_wait(&barberSleep,&sleepa);
urakam=0;
pthread_mutex_unlock(&sleepa);
printf("\t\t\t\tBarber wakes up.\n");
fflush(stdout);
}
else
{
printf("\t\t\tBarber begins cutting hair.\n");
fflush(stdout);
sleep((rand()%20)/5);
current--;
printf("\t\t\t\tHair cut complete, customer leaving store.\n");
pthread_cond_signal(&barberWorking);
}
}
pthread_exit(0);
}
void checkQueue()
{
current++;
printf("\tCustomer has arrived in the waiting room.\t\t\t\t\t\t\t%d Customers in store.\n",current);
fflush(stdout);
printf("\t\tCustomer checking chairs.\n");
fflush(stdout);
if(current<seats)
{
if(urakam==1)
{
printf("\t\t\tBarber is sleeping, customer wakes him.\n");
fflush(stdout);
pthread_cond_signal(&barberSleep);
}
printf("\t\tCustomer takes a seat.\n");
fflush(stdout);
pthread_mutex_unlock(&queue);
pthread_mutex_lock(&wait);
pthread_cond_wait(&barberWorking,&wait);
pthread_mutex_unlock(&wait);
return;
}
if(current>=seats)
{
printf("\t\tAll chairs full, leaving store.\n");
fflush(stdout);
current--;
pthread_mutex_unlock(&queue);
return;
}
}