/* Pseudocode of the barbershop problem * William Stallings, "Operating Systems Internals and Design Principles," * 6/e, Prentice Hall, 2009, p. 763 */ semaphore max_capacity = 20; semaphore sofa = 4, barber_chair = 3, coord = 3; semaphore cust_ready = 0, leave_b_chair = 0, payment= 0, receipt = 0; semaphore finished[50] = {0}; semaphore mutex1 = 1, mutex2 = 1; int count; void customer() { int cust_nr; wait(max_capacity); enter_shop(); wait(mutex1); cust_nr = count; count++; signal(mutex1); wait(sofa); sit_on_sofa(); wait(barber_chair); get_up_from_sofa(); signal(sofa); sit_in_barber_chair(); wait(mutex2); enqueue1(cust_nr) signal(mutex2); signal(cust_ready); wait(finished[cust_nr]); leave_barber_chair(); signal(leave_b_chair); pay(); signal(payment); wait(receipt); exit_shop(); signal(max_capacity); } void barber() { int b_cust; while (true) { wait(cust_ready); wait(mutex2); dequeue1(b_cust); signal(mutex2); wait(coord); cut_hair(); signal(coord); signal(finished[b_cust]); wait(leave_b_chair); signal(barber_chair); } } void cashier() { while (true) { wait(payment); wait(coord); accept_pay(); signal(coord); signal(receipt); } }