This program is Implement a robust variation of the Barber Shop problem using the UNIX fork command and mutual exclusion techniques. There are 3 categories of people needing haircuts - Officers, NonComs, and Grunts. The program uses a producer and consumer paradigm with shared circular buffer.
The producer is a process that periodically attempts to add a new person, long hair, to the buffer of chairs in the barber shop. The producer periodically uses the sleep call with a sleep time determined by a command line input parameter between 1 and 5 seconds (default 3). If there are no open seats, then the sergeant waits until there is an empty chair.
The consumer process is the barber. Haircuts have an individual time between 1 and 5 seconds, determined by the input but priority for service is instituted, officers get priority over anyone else, followed by NonComs, followed by grunts.
The producer and consumer share a buffer of 20 chairs, but there are 3 FIFO lists within the 20 chairs. At any time there may be only 20 people in the Barber Shop chairs with a random distribution of Officers, NonComs, and Grunts, and 1 person getting a hair cut. When there is only one type of person in the Barber Shop, then the shop queue acts as a circular buffer.
There is another process, the Lieutenant, who monitors the overall Barber Shop. He reports the status of barber shop every 2 seconds.
The program creates 3 processes as described above. The program will firstly fork off the sergeant process and the barber process. After it forks these 2 processes off, it will become a lieutenant.
2 semaphores are used to fork command and mutual exclusion techniques. One is used to control shared memory, which is 20 chairs in the barber shop and another is used to control when the lieutenant process is done.
There are 20 shared memory space, which is chairs in the barber shop. Each chair has the information that the chair is occupied, which person is in the chair, priority of haircut, and the time to have haircut.
The lieutenant process will report the status of buffer until the barber process is done to cut hair.
The barber process will cut hair people in the waiting room. This process will find the highest priority person (job), which is highest group number and highest priority job in the group, in the buffer and this person will get haircut. The barber process will also remove the person who will get haircut and will change the people¡¯s haircut order in the buffer.
The sergeant process will get input from standard input (cin) until the end of the data.
The program has 20 chairs and a lot of people want to have haircut. As same as the program, the real system has limited, expensive resources, and people want to share systems at the same time. As a result, it is very important that the operating system can manipulate shared memory and priorities of jobs. The operating system should make sure that the process will not overwrite shared memory and schedule well jobs in the queue.