Semaphore Basic Operations

204 Views Asked by At

First off, please excuse me if this is not the correct place for my question.

I am studying for an exam, and this is the sample question that was given:

Problem given in a previous exam:

Students decide to meet and go watch a movie. Students decide to meet and go watch a movie. decide to meet and go watch a movie. After they gather together in groups of group_size, they go buy tickets. Each student student student buys his own ticket (becomes a customer). There are two ticket-booths (each booth has one clerk). If the clerk is available it will serve the next customer on line (one customer at a time). There is only one line for both booths.

Using semaphores and operations on semaphores, synchronize the two thread types: Student and Clerk. There are numStundents (default 9) and numClerks (default 2). Consider that there are no customers on line at the booth in the early morning. From the clerk’s point of view (it is up to you) you can consider that both clerks were already at work or that neither of them has arrived yet (when the first customer showed up). There are more customers than numStudents. Give the type, initial value of each semaphore used, and shortly explain its use. Don’t use Boolean variables if their use can be replaced by semaphores. Roughly before the synchronization, a possible execution pseudo-code for the threads might be:

   Student ( ) { 
    arrive to meeting place // napping
    group together
    go buy ticket

    buy ticket //napping 
    watch movie // napping 
   } 

   Clerk ( ) {
    get to workplace // napping
    while(true) {
     serve customer // if ∃ a customer
    }
   }

If anyone would help me solve this question, I would really appreciate it.

1

There are 1 best solutions below

0
On

As providing actual code would not really serve you, here are some few tips:

  • You need to maintain a queue of waiting students, which will have to be locked each time a clerk decide to accept a student, and freed when the clerk is done selling tickets to the student(s).

  • You need to make use of wait() to put clerks and students in "sleep mode", while your semaphore is = 0

  • Think about the scenario where you can reach a deadlock (clerk never releasing the lock/ decrementing semaphore, etc)