PROBLEM #1 line 87-93 -------------- sem_wait(&mutexfilmfare); if (freefilmfares <= 0) { printf("Customer %d reads S+G OS\n",CustomerId); } freefilmfares--; sem_post(&mutexfilmfare); sem_wait(&filmfare); Consider the situation where a customer is interrupted (timer/scheduler) after sem_post(&mutexfilmfare). Another customer who started reading S+G later may get hold of a filmfare by doing a sem_wait(&filmfare) ahead of the first one. Unfair Q jumping! This bug may be eliminated by changing the code as follows (albeit at a slight loss of concurrency!): sem_wait(&mutexfilmfare); if (freefilmfares <= 0) { printf("Customer %d reads S+G OS\n",CustomerId); } freefilmfares--; sem_wait(&filmfare); sem_post(&mutexfilmfare); PROBLEM #2 line 133-138 ----------------- sem_wait(&mutex3); // enqueue(Q1,(CustomerId,i)); write_pipe(pipe1[1],CustomerId); write_pipe(pipe1[1],i); sem_post(&mutex3); sem_post(&cust_ready); Consider the situation where a customer writes (CustomerId,i) to the Q/pipe, releases mutex3 and is interrupted. Now, another cutomer (or cashcounter) may write his (its) stuff in to the pipe and sem_post(&cust_ready). Because of the Q discipline in the pipe the barber will still pick up the guy ahead, but on a signal from a wrong guy. This is clearly a race condition and is undesirable - if not anything else it is not elegant! Change to: sem_wait(&mutex3); // enqueue(Q1,(CustomerId,i)); write_pipe(pipe1[1],CustomerId); write_pipe(pipe1[1],i); sem_post(&cust_ready); sem_post(&mutex3); PROBLEM #3 line 75-80 ------------- sem_wait(&mutex2); sem_wait(&mutexfilmfare); if ((freechairs == 0) || (freefilmfares < totalfilmfares)) { sem_post(&mutexfilmfare); sem_post(&mutex2); line 100-103 ----------------- sem_wait(&mutexfilmfare); freefilmfares++; sem_post(&mutexfilmfare); line 106 ----------- else { sem_post(&mutexfilmfare);sem_post(&mutex2);sem_wait(&barberchair); } If a customer who got his hair cut gets up from the chair, he will increase the number of freechairs by one and the barber will signal the semaphore barberchair. Now, for the sake of fairness, the customer who has been reading filmfare for the longest time should get the hold of a barber chair. But, suppose there was only one customer reading filmfare and after releasing the filmfare he gets an interrupt from the scheduler. A new customer entering will see a chair free and no customer reading filmfare hence he will occupy the chair before the customer who was waiting. The customer who is done with FF will miss his turn for hair-cut and the new guy will miss the juicy gossips - bad for both. So the statements of releasing filmfares and occupying barber chairs should be made atomic. i.e. lines 101-104 should be changed as following sem_wait(&mutexfilmfare); freefilmfares++; occupychair(); // function to occupy chair sem_post(&mutexfilmfare); & line 106 as else {sem_wait(&barberchair); occupychair(); sem_post(&mutexfilmfare);sem_post(&mutex2); } where, occupychair() { /* barberchairs, here we come to select */ /*choose a free chair. show preference in order - simplest. perhaps the first chair has a lurid interesting poster ahead on the wall and we wish to be as close as possible? chair[i] = -1 means it is unoccupied */ i = 0; while ((chair[i] != -1) && (i < totalchairs)) i++; if (i == totalchairs) { printf("this is not possible! we are here because at least 1 chair empty!"); printf("which idiot sem_postled barberchair? psychoanalysis suggested!"); exit(); } else { /* occupy chair i */ /* the secure selection ensures we aren't sitting on somebody's lap*/ chair[i] = CustomerId; freechairs--; printf("Customer %d occupies chair %d. freechairs = %d\n",CustomerId,i,freechairs); } } and the part of original code from line 108 - 128 should be removed.