I work in an operating system. We have an exam in two weeks, and I suspect that there is a lunch philosopher and problems with the hairdresser (semaphore version). Now, if I wanted to, I could just open the tutorial and get the answers to the questions, but I would rather study them myself. I spent some time working on problems, and I think I'm getting closer, but ... I'm not sure. I do not want to be simply told what is wrong with my decisions, but I would like hints. I want to understand myself as much as possible.
So, the sleeping hairdresser ... In the solution that I developed, I have three binary semaphores: “w” for the waiting room, “c” for the hairdresser's chair, and “x” for the exit. The hairdresser can take care of one client at a time when he checks the waiting room for other clients, and if they are not there, he is sleeping, right? This is what I developed for the hairdresser process code (in the form of a C hybrid and pseudocode):
while(true)
{
P(w);
P(c);
P(x);
while( customersWaiting > 0 )
{
V(c);
V(w);
service customer
V(x);
P(w);
}
V(w);
sleep
V(c);
service customer
V(x);
}
I think this is correct, but I'm not sure. I feel that the client that has just arrived should be processed by the code in a while loop (customersWaiting> 0), but I cannot figure out how to organize the semaphores to make this work.
, , , . , , . , , . , , ? , , :
P(w);
if (chair is occupied)
{
if (barber is sleeping)
{
wakeup barber
V(w);
P(c);
P(x);
}
}
else
{
if (customersWaiting < amountOfChairs)
{
customersWaiting++;
V(w);
P(c);
P(x);
customersWaiting--;
}
}
exit shop
, ... , , , , , , , , , . ... , , ( ), , ... , ? ... ? , , .
... , . , , "g" , "" , , c [0..n -1] . , (, ) , ? ( , ) , , , , . :
while(true)
{
think
P(g);
P(a);
P(c[left chopstick number]);
take left chopstick
P(c[right chopstick number]);
take right chopstick
V(g);
eat
V(c[left chopstick number]);
replace left chopstick
V(c[right chopstick number]);
replace right chopstick
V(a);
, , , , , - , , , . ?
!
,
RedZone