Tips (not answers) about the correctness of my sleeping hairdresser and dining philosopher algorithms?

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);    //Guarantees an entering customer can't check the waiting room before the barber.
    P(c);
    P(x);    //A customer being serviced can't leave until barber is done servicing him.
    while( customersWaiting > 0 )  
    {
        V(c);    //Allow a waiting room customer to sit in barber chair.
        V(w);    //Allow another customer to enter the waiting room
        service customer
        V(x);    //Allow customer to leave
        P(w);    //Lock waiting room so barber can check it.
    }   
    //No customers
    V(w);   //Allow next entering customer to check waiting room.
    sleep
    V(c);   //Allows new customer service.
    service customer
    V(x);    //Allow customer to leave.
}

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);    //Guarantees neither barber nor other customers can check waiting room.
if (chair is occupied)  //Could you write this as if(c), or would you create a separate flag?
{    
    if (barber is sleeping)
    {
        wakeup barber
        V(w);    //Now the waiting room can be checked by someone else.
        P(c);    //Sit in barber chair
        P(x);    //Attempt to exit shop
    }
}
else
{
    if (customersWaiting < amountOfChairs)
    {
        customersWaiting++;
        V(w);    //Now the waiting room can be checked by someone else.
        P(c);    //Sit in barber chair, when it available that is.
        P(x);    //Attempt to exit shop
        customersWaiting--;
    }
}
exit shop

, ... , , , , , , , , , . ... , , ( ), , ... , ? ... ? , , .

... , . , , "g" , "" , , c [0..n -1] . , (, ) , ? ( , ) , , , , . :

while(true)
{
    think
    P(g);    //Above all, no one else can try to grab a chopstick at the same time as someone else.
    P(a);    //Decrement the amount of philosophers that may start eating.
    P(c[left chopstick number]);
    take left chopstick
    P(c[right chopstick number]);
    take right chopstick
    V(g);    //Now someone else may attempt to grab a pair.
    eat
    V(c[left chopstick number]);
    replace left chopstick
    V(c[right chopstick number]);
    replace right chopstick
    V(a);

, , , , , - , , , . ?

!

, RedZone

+3
2

- , ( , , , )

  • () .
  • , .
  • , , .
  • ( , ) , , ( ) .
      1  o  2

      o     o

      4  o  3

. , , , , (, ), , ( A, B C, , ( , - )).

+2

.

? ? , , . , . , .

, , , , , - , , , . ?

, . , , . , , , , .

( C ):

all pseudo-code tends to be hybrid C;)

+1
source

All Articles