Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Advanced Operating system

Assignment 3.

Submitted to
Dr. Sajid Hussain
By
Shaneela
2023-KIU-MS5090
The Sleeping Teaching Assistant

A university computer science department has a teaching assistant (TA)


who helps undergraduate students with their programming assignments
during regular office hours. The TA’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 TA is
currently helping another student. When there are no students who need
help during office hours, the TA sits at the desk and takes a nap. If a
student arrives during office hours and finds the TA sleeping, the student
must awaken the TA to ask for help. If a student arrives and finds the TA
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 come back
at a later time. Using POSIX threads, mutex locks, and semaphores,
implement a solution that coordinates the activities of the TA and the
students. Details for this assignment are provided below.

The Students and the TA

Using Pthreads begin by creating n students. Each will run as a separate


thread. The TA will run as a separate thread as well. Student threads will
alternate between programming for a period of time and seeking help from
the TA. If the TA is available, they will obtain help. Otherwise, they will
either sit in a chair in the hallway or, if no chairs are available, will resume
programming and will seek help at a later time. If a student arrives and
notices that the TA is sleeping, the student must notify the TA using a
semaphore. When the TA finishes helping a student, the TA must check to
see if there are students waiting for help in the hallway. If so, the TA must
help each of these students in turn. If no students are present, the TA may
return to napping.

Perhaps the best option for simulating students programming—as well as


the TA providing help to a student—is to have the appropriate threads sleep
for a random period of time.

Solution
This problem described a classic example of a synchronization
problem involving students and a teaching assistant (TA) using POSIX
threads, mutex locks, and semaphores to coordinate their activities.

Entities:

Students: There are 'n' student threads. Each student alternates


between programming and seeking help from the TA.

TA: The TA is a separate thread who helps students when they are
available.

Chairs: There are three chairs in the hallway outside the TA's office.
Students wait on these chairs if the TA is helping another student.

Actions and Synchronization:

Programming: Students spend some time programming before


seeking help. You can simulate this by having each student sleep for a
random period while they program.

Seeking Help: When a student needs help, they check if the TA is


available. If the TA is available, they obtain help; otherwise, they either
sit on a chair in the hallway (if available) or continue programming.
Awakening the TA: If a student arrives and finds the TA sleeping,
they need to notify the TA to wake up. You can use a semaphore to
implement this.

TA Helping Students: The TA helps students one at a time. After


helping a student, the TA checks if there are other students waiting in
the hallway (sitting on chairs). If so, the TA helps each waiting student
in turn.

Napping: If there are no students waiting, the TA can return to


napping.

Synchronization Techniques:

Mutex Locks: use of mutex locks to protect shared resources such as


chairs and to ensure that only one student or the TA accesses them at
a time.

Semaphores: Use semaphores to control the flow of students and the


TA. For example, you can use a semaphore to wake up the TA when a
student arrives and finds the TA sleeping.

Conditional Variables: Conditional variables can be used to make


students wait for the TA to help them if the TA is currently helping
another student.
Random Sleep: Simulate students' programming time by making
them sleep for random periods.

Here's a high-level outline of how to structure a program:

● Create threads for the TA and 'n' students.


● Use mutex locks to protect shared resources like chairs.
● Use semaphores to coordinate waking up the TA and signaling
when the TA is done helping a student.
● Implement logic in student and TA threads to perform the actions
described above based on synchronization primitives.
● Use random sleep to simulate programming time for students.

Code
#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

#include <unistd.h>

#define NUM_STUDENTS 5

#define NUM_CHAIRS 3

pthread_mutex_t mutex;

sem_t student_sem, ta_sem;

int waiting_students = 0;
void *student_thread(void *id) {

int student_id = *(int *)id;

while (1) {

// Simulate programming time

printf("Student %d is programming.\n", student_id);

sleep(rand() % 5);

pthread_mutex_lock(&mutex);

if (waiting_students < NUM_CHAIRS) {

// Student takes a chair in the hallway

waiting_students++;

printf("Student %d is waiting in the hallway.\n", student_id);

pthread_mutex_unlock(&mutex);

sem_wait(&student_sem); // Notify TA

} else {

// No chairs available, student continues programming

pthread_mutex_unlock(&mutex);

printf("Student %d will come back later.\n", student_id);

}
}

void *ta_thread(void *arg) {

while (1) {

pthread_mutex_lock(&mutex);

if (waiting_students > 0) {

// TA helps a student

waiting_students--;

printf("TA is helping a student.\n");

sem_post(&student_sem); // Signal student that TA is available

pthread_mutex_unlock(&mutex);

sleep(rand() % 3); // Simulate TA helping time

} else {

// No students waiting, TA naps

pthread_mutex_unlock(&mutex);

printf("TA is napping.\n");

sem_wait(&ta_sem); // Wait until a student wakes TA up

int main() {
pthread_t students[NUM_STUDENTS], ta;

int student_ids[NUM_STUDENTS];

pthread_mutex_init(&mutex, NULL);

sem_init(&student_sem, 0, 0);

sem_init(&ta_sem, 0, 0);

// Create TA thread

pthread_create(&ta, NULL, ta_thread, NULL);

// Create student threads

for (int i = 0; i < NUM_STUDENTS; i++) {

student_ids[i] = i + 1;

pthread_create(&students[i], NULL, student_thread,


&student_ids[i]);

// Join threads (never reached in this simplified example)

pthread_join(ta, NULL);

for (int i = 0; i < NUM_STUDENTS; i++) {

pthread_join(students[i], NULL);

pthread_mutex_destroy(&mutex);
sem_destroy(&student_sem);

sem_destroy(&ta_sem);

return 0;

Output

You might also like