Consider a University professor who helps students with their programs during office hours. The professor's office is rather small and has room for only one desk with a chair and computer. There are three chairs in the hallway outside the office where students can sit and wait if the professor is currently helping another student. When there are no students who need help during office hours, the professor takes a nap. If a student arrives during office hours and finds the professor sleeping, the student must awaken the professor to ask for help. If a student arrives and finds the professor currently helping another student, the student sits on one of the chairs in the hallway and waits. If no chairs are available, the student will leave and come back at a later time.
From the description of the problem, we will use semaphores (https://en.wikipedia.org/wiki/Semaphore_(programming)) and implement two threads;
1. studentsThd() - which can either get tutored or will leave if there is no free chair
2. professorThd() - a single thread which can either tutor students or go to sleep
In this problem, the professor represents the operating system and the students represent processes. The operating system should allocate the required resources to the processes and, at the same time, avoid deadlock. There are many various solutions to this problem, so be sure to follow the provided guidance.
Implement the Sleeping professor problem in C. The program must compile and execute under Ubuntu 18.04 LTS.
The main() function should perform the following actions:
The professor() function will perform the following actions
// you can have two nested loops in the professor() and checking if all the students have been helped at the end of the outer loop
The students() function should perform the following actions.
// Each thread represents a student so you have to create an array of threads
The professor and student messages should be printed in red and the student messages printed in green.
printf("\033[0;31mI’m a message in red\033[0m\n", 0); // red
printf("\033[0;92mI’m a message in green\033[0m\n", 0); // green
printf("\033[0;94mI’m a message in blue\033[0m\n", 0); // blue
printf("\033[0;93mI’m a message in yellow\033[0m\n", 0); // yellow
printf("\033[0;1mI’m an underlined message\033[0m\n", 0);
Refer to the example execution for examples of the coloring
In order to use threading functions and semaphores in C, you will need the below include files:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < signal.h>
#include < pthread.h>
#include < semaphore.h>
#include < time.h>
#include < stdbool.h>
Use the following parameters
#define STUDENT_COUNT_MIN 2
#define STUDENT_COUNT_MAX 10
#define CHAIR_COUNT 3
#define HELPS_MAX 3
The semaphores, mutexes, and some integers will be declared globally.
Use the following compiler options
gcc -Wall -pedantic -pthread -o sleepingProf sleepingProf.c
Figure: see image.