Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using c or c++ Seeking Mentor Problem A university computer science lab has a mentor who helps undergraduate students with their programming assignments during regular

Using c or c++

Seeking Mentor Problem

A university computer science lab has a mentor who helps undergraduate students with their programming assignments during regular office hours. The mentor's office has room for only one desk with a chair and computer. There are few chairs in the hallway outside the office where students can sit and wait if the mentor is currently helping another student. When there are no students who need help during office hours, the mentor sits at the desk and takes a nap. If a student arrives during office hours and finds the mentor sleeping, the student must awaken the mentor to ask for help. If a student arrives and finds the mentor 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 mentor and the students. Details for this assignment are provided below.

The Students and the Mentor

Using Pthreads (Section 4.4.1), begin by creating n students. Each will run as a separate thread. The mentor will run as a separate thread as well. Student threads will alternate between programming for a period of time and seeking help from the mentor. If the mentor 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 mentor is sleeping, the student must notify the mentor using a semaphore. When the mentor finishes helping a student, the mentor must check to see if there are students waiting for help in the hallway. If so, the mentor must help each of these students in turn. If no students are present, the mentor may return to napping. For details on how to use pthreads synchronization primitives mutex and semaphore see section 5.9.4 of the book.

image text in transcribed

image text in transcribed

To simulate students programming in students threads, and the mentor providing help to a student in the mentor thread, the appropriate threads should sleep (by invoking sleep()) for a random of time (up to three seconds).

The total number of students, the number of chairs, and the number of times a student seeks the mentor's help are passed as command line parameters as shown below

mentor #students #chairs #help

mentor10 4 5

Once a student thread takes the required number of help from the mentor, it should terminate. Once all the student threads are terminated, the mentor thread be terminated, and finally the main program should be terminated. Print appropriate messages that show the current state of the student/mentor. This will help in both debugging, and validating your program.

Print appropriate messages that show the current state of the student/mentor. This will help in both debugging, and validating your program.

5.9.4 Pthreads Synchronization Although the locking mechanisms used in Solaris are available to user-level threads as well as kernel threads, basically the synchronization methods discussed thus far pertain to synchronization within the kernel. In contrast, the Pthreads API is available f of any particular kernel. This API provides mutex locks, condition variables, and read-write locks for thread synchronization or prog rammers at the user level and is not part Mutex locks represent the fundamental synchronization technique used with Pthreads. A mutex lock is used to protect critical sections of code-that is, a thread acquires the lock before entering a critical section and releases it upon exiting the critical section. Pthreads uses the pthread mutext data type for mutex locks. A mutex is created with the pthread mutex init) function. The first parameter is a pointer to the mutex. By passing NULL as a second parameter, we initialize the mutex to its default attributes. This is illustrated below: #include pthread mutex.t mutex; /create the mutex lock */ pthreadmutex.init(&mutex, NULL); The mutex is acquired and released with the pthread mutex_lock) and pthread mutex unlock() functions. If the mutex lock is unavailable when pthread mutex_lock() is invoked, the calling thread is blocked until the owner invokes pthread mutex unlock ). The following code illustrates protecting a critical section with mutex locks: /*acquire the mutex lock*/ pthread mutex_lock (&mutex); /critical section /release the mutex lock pthread_mutex unlock(&mutex); All mutex functions return a value of 0 with correct operation; if an error occurs, these functions return a nonzero error code. Condition variables and read-write locks behave similarly to the way they are described in Sections 5.8 and 5.7.2, respectively. Many systems that implement Pthreads also provide semaphores, although semaphores are not part of the Pthreads standard and instead belong to the POSIX SEM extension. POSIX specifies two types of semaphores-named and 5.9.4 Pthreads Synchronization Although the locking mechanisms used in Solaris are available to user-level threads as well as kernel threads, basically the synchronization methods discussed thus far pertain to synchronization within the kernel. In contrast, the Pthreads API is available f of any particular kernel. This API provides mutex locks, condition variables, and read-write locks for thread synchronization or prog rammers at the user level and is not part Mutex locks represent the fundamental synchronization technique used with Pthreads. A mutex lock is used to protect critical sections of code-that is, a thread acquires the lock before entering a critical section and releases it upon exiting the critical section. Pthreads uses the pthread mutext data type for mutex locks. A mutex is created with the pthread mutex init) function. The first parameter is a pointer to the mutex. By passing NULL as a second parameter, we initialize the mutex to its default attributes. This is illustrated below: #include pthread mutex.t mutex; /create the mutex lock */ pthreadmutex.init(&mutex, NULL); The mutex is acquired and released with the pthread mutex_lock) and pthread mutex unlock() functions. If the mutex lock is unavailable when pthread mutex_lock() is invoked, the calling thread is blocked until the owner invokes pthread mutex unlock ). The following code illustrates protecting a critical section with mutex locks: /*acquire the mutex lock*/ pthread mutex_lock (&mutex); /critical section /release the mutex lock pthread_mutex unlock(&mutex); All mutex functions return a value of 0 with correct operation; if an error occurs, these functions return a nonzero error code. Condition variables and read-write locks behave similarly to the way they are described in Sections 5.8 and 5.7.2, respectively. Many systems that implement Pthreads also provide semaphores, although semaphores are not part of the Pthreads standard and instead belong to the POSIX SEM extension. POSIX specifies two types of semaphores-named and

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Optimizing Data Collection In Warzones

Authors: Aaget Aamber

1st Edition

B0CQRRFP5F, 979-8869065902

More Books

Students also viewed these Databases questions