+1 (315) 557-6473 

Accurate POSIX Programming Project Help in C

This homework shows how to use the POSIX package library in C to solve a sample process-operating system case in real life. It also demonstrates the use of arrays and loops in various situations within the project. The PhD C Homework Helper who offered this POSIX Programming Project Assistance was from ProgrammingHomeworkHelp.
POSIX Programming Project Help in C


Consider a university professor that helps students with some programming tasks at office hours only. He has a small office with room for just one desk carrying one chair and computer. Outside the office is a hallway with three chairs on which students sit while waiting for the professor to sort another student. The professor takes a nap if there aren’t any students that need help during office hours. If a student comes for help during office hours and the professor is taking a nap, s/he must first awaken the professor before asking 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 a student comes and gets that the professor is still helping another student, s/he must sit on the chairs in the hallway and wait; if there aren’t any chairs available, the student must go back and return 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
Here, the professor indicates the operating system, and the students are the processes. The operating system needs to allocate the needed resources to the processes while avoiding any deadlock. There are many various solutions to this problem, so be sure to follow the provided guidance.
Implement the Sleeping professor problem in C.
#define CHAIR_COUNT 3
#define HELPS_MAX 3
pthread_mutex_t chairsMutex = PTHREAD_MUTEX_INITIALIZER;
sem_t studentSem;
sem_t profSem;
sem_t chairsSem[CHAIR_COUNT];
int *studentHelpCounts;
int numStudents = 0;
int usedChairs = 0;
int helpsLeft = 0;
// Professor thread
void *professor(void *args)
    bool awake = false;
    while(helpsLeft > 0)
            printf("\033[0;92mProfessor has been awakened by a student.\033[0m\n");
            awake = true;
        // Signal the student to come in
        printf("\033[0;92mStudent frees chair and enters professors office. Remaining chairs %d\033[0m\n", CHAIR_COUNT - usedChairs);
        // Student comes in and get the help
        printf("\033[0;92mProfessor is helping a student.\033[0m\n");
        usleep(rand() % 1500000);
        // Done helping student
    return (void *)NULL;
// Student thread
void *student(void *args)
    int *studentId = (int *)args;
    while(studentHelpCounts[*studentId] > 0)
        // Work on an assignment
        printf("\033[;93mStudent %d doing assignment.\033[0m\n", (*studentId) + 1);
        usleep(rand() % 2000000);
        // Ask for help
        if(usedChairs >= CHAIR_COUNT)
            printf("\033[0;31mChairs occupied, student %d will return later.\033[0m\n", (*studentId) + 1);
            printf("\033[0;31mStudent %d needs help from the professor.\033[0m\n", (*studentId) + 1);
            // Occupy a chair
            int chairIndex;
            chairIndex = usedChairs;
            // Wait until professor is available to help
            // Vacate the chair
            printf("\033[0;31mStudent %d is getting help from the professor.\033[0m\n", (*studentId) + 1);
            // Wait until professor is done helping
            // Done, stop if no more help needed
    return (void *)NULL;
// Entry point of the program
int main(int argc, char *argv[])
    // Initialize the number of students
        printf("How many students coming to professor's office? ");
        scanf("%d", &numStudents);
        if(numStudents >= STUDENT_COUNT_MIN && numStudents <= STUDENT_COUNT_MAX)
    // Initialize the number of helps each student needs
    studentHelpCounts = (int *)malloc(sizeof(int) * numStudents);
    for(int i = 0; i < numStudents; i++)
        int numHelps = rand() % HELPS_MAX + 1;
        studentHelpCounts[i] = numHelps;
        helpsLeft += numHelps;
    // Initialize the student threads
    pthread_t *studentsThread = (pthread_t *) malloc(sizeof(pthread_t) * numStudents);
    // Initialize the semaphores
    sem_init(&profSem, 0, 0);
    sem_init(&studentSem, 0, 0);
    for(int i = 0; i < CHAIR_COUNT; i++)
        sem_init(&chairsSem[i], 0, 0);
    // Create the professor thread
    pthread_t profThread;
    pthread_create(&profThread, NULL, &professor, NULL);
    // Create the student threads
    for(int i = 0; i < numStudents; i++)
        int *studentId = (int *)malloc(sizeof(int));
        *studentId = i;
        pthread_create(&studentsThread[i], NULL, &student, studentId);
    // Wait for appropriate threads to complete
    for(int i = 0; i < numStudents; i++)
        pthread_join(studentsThread[i], NULL);
    pthread_join(profThread, NULL);
    // Clean up
    return 0;
Related Blogs