Introduction:

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.

Project:

Implement the Sleeping professor problem in C. The program must compile and execute under Ubuntu 18.04 LTS.

Specifications

The main() function should perform the following actions:

  • Display header
  • Read (scanf) and validate student count
    • Must be between STUDENT_COUNT_MIN and STUDENT_COUNT_MAX (inclusive)
    • Refer to example execution
  • Student Help Counts
    • Create and populate an array for the number of times each student will need help.
      • initialize each student help count (i.e., (rand()%HELPS_MAX + 1);)
      • the array must be dynamically allocated (i.e., use malloc()).
  • Threads array (students)
    • An array for student threads must be used (one per student)
      • Thread array must be dynamically allocated (i.e., use malloc()).
  • Initialize semaphores
    • The student semaphore initialized to 0
    • The professor semaphore initialized to 0
    • The chairs semaphore array all initialized to 0
  • Create one professor thread
  • Create student count student threads
  • Wait for the appropriate threads to complete.

The professor() function will perform the following actions

  • Loop forever
    • Wait until awakened by a student
    • After awakened, display message
    • Once awakened, loop to help all waiting students
      • if chair count is 0, exit help students loop
      • decrement chair count (must use mutex)
      • student vacates chair (applicable chair semaphore)
        • display message (see example)
      • professor helps the student (random amount of time)
        • display message (see example)
        • random amount of time: usleep(rand() % 1500000);
      • signal that next student can enter
    • If all students have been helped (all helps counts 0).
      • exit thread

// 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.

  • Loop forever
    • Display message that student is working on the assignment (see example)
    • Student works on the assignment for a random amount of time
      • Random amount of time: usleep(rand() % 2000000);
    • Student decides to ask for help (display message)
    • Get chair count (must use mutex)
    • If no chairs available
      • Display message, continue working on the assignment
    • If a chair is available
      • If all chairs available
        • awaken the sleeping professor
    • Occupy a chair
      • update chair count (must use mutex)
      • wait until professor is available to help (applicable chair semaphore)
      • when professor helps, display message
      • wait until professor is done helping
      • decrement the help count for that student
      • if that students help count is 0
        • exit thread

// 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

Include Files

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.

Compilation Options

Use the following compiler options

gcc -Wall -pedantic -pthread -o sleepingProf sleepingProf.c

Example Execution

Figure: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.