+1 (315) 557-6473 

Multithreaded Sleeping Professor Simulation in C

This C program simulates a scenario where a sleeping professor helps students with assignments using multithreading and semaphores. Students arrive randomly, wake the professor, and occupy available chairs. The professor assists students one at a time, each help taking a random duration. Once all students are helped, the professor exits. The code demonstrates synchronization using semaphores and mutex, managing chair states and student help counts. It models a practical scenario involving multiple threads, showcasing effective coordination in a concurrent environment.

Implementation Details of the Sleeping Professor Simulation

This C program intricately models the dynamics of a professor assisting students with their C assignments through a sophisticated multithreaded simulation. Leveraging pthreads and semaphores, the code orchestrates the interaction between students and the professor. Students, represented as threads, initiate their need for help with C assignments after random intervals. The professor, initially dormant, responds to students, managing available chairs and ensuring a fair distribution of assistance. With semaphores and mutexes governing synchronization, the program offers a compelling illustration of concurrent processes and shared resource management, embodying a nuanced academic support scenario where students seek guidance for their programming endeavors.

Block 1: Global Definitions

#include < stdio.h > #include < stdlib.h > #include < pthread.h > #include < unistd.h > #include < semaphore.h > #include < time.h >

The code includes necessary header files for standard input/output, memory allocation, threading, sleep functionality, semaphores, and time functions.

Block 2: Global Constants

#define STUDENT_COUNT_MIN 2 #define STUDENT_COUNT_MAX 10 #define CHAIR_COUNT 3 #define HELPS_MAX 3

These constants define the minimum and maximum number of students, the number of chairs available, and the maximum number of times a professor can help a student.

Block 3: Global Variables and Semaphores

sem_t professor_sem; sem_t chair_sem[CHAIR_COUNT]; pthread_mutex_t chair_count_mutex; int student_count; int *help_counts; int chair_count = 0; int chair_state[CHAIR_COUNT] = {0, 0, 0}; int arrival = 1;
  • professor_sem: Semaphore to wake up the professor.
  • chair_sem[CHAIR_COUNT]: Semaphore array for each chair.
  • chair_count_mutex: Mutex to protect chair count and state.
  • student_count: Number of students.
  • help_counts: Array to store the number of times each student needs help.
  • chair_count: Current number of chairs being used.
  • chair_state[CHAIR_COUNT]: Array representing the state of each chair (0 = free, 1 = used).
  • arrival: Counter used to select students based on arrival order.

Block 4: Professor Thread Function

/* Professor thread */ void *professor(void *arg) { // ... }

This function simulates the behavior of the professor. The professor waits for a student to wake him up, then selects a student from the available chairs, helps the student, and repeats until all students have been helped.

Block 5: Student Thread Function

/* Student thread */ void *students(void *arg) { // ... }

This function simulates the behavior of students. Students do assignments, occasionally need help, and wait for the professor to help them based on available chairs. Students exit the system after receiving help a specified number of times.

Block 6: Main Function

int main() { // ... }
  • Initializes random seed, displays a header, and reads the number of students.
  • Allocates memory for help_counts array and assigns random help counts for each student.
  • Initializes semaphores and creates threads for the professor and students.
  • Waits for all student threads to complete and then waits for the professor thread to complete.
  • Frees allocated memory and destroys semaphores.


In conclusion, this C program encapsulates the complexity of a dynamic educational setting, where students seek guidance on their C assignments from a sleeping professor. Through meticulous multithreading and semaphore usage, the simulation elegantly captures the essence of collaborative learning and concurrent interactions. The code mirrors a real-world academic environment, illustrating the challenges of resource sharing and synchronization in a thread-based system. As the professor awakens and attends to students in a structured manner, the program not only serves as a robust educational tool but also highlights the importance of effective coordination in facilitating student-mentor interactions. It stands as a testament to the intricacies of concurrent programming, portraying a vivid simulation of a professor assisting students on their academic journey.