Question: WRITE IN C + + . Objective You will learn how to use POSIX threads and their advanced synchronization feature. The PRoblem A university computer

WRITE IN C++.
Objective
You will learn how to use POSIX threads and their advanced synchronization feature.
The PRoblem
A university computer science department has two teaching assistants (TAs) who help undergraduate students with their programming assignments. Their office is rather small and has room for only two undergraduate students at a time. In addition, there are two chairs in the hallway outside the office where students can sit and wait if both TAs are helping other students. If a student arrives and finds both TAs assisting other students, the student sits on one of the two chairs in the hallway and waits. If no chairs are available, the student will come back at a later time.
Your program should track the actions of the students requesting help. It will represent each of them in a separate thread created by your main program. This thread will synchronize with other threads to ensure that the TA office will never contain more than two students and no more than two students will ever wait outside. To achieve that goal, they must use Pthread mutexes and condition variables. Solutions using semaphores will not be accepted.
Your PROGRAM
Your program will read all its input parameters from the-redirected-standard input. Each input line will describe a student seeking help and will contain three quantities representing:
A. A name that will never contain spaces,
B. The number of minutes elapsed since the arrival of the previous student, and
C. The number of minutes the student will spend with a TA.
One possible set of input could be:
\[
\begin{array}{llll}
\text { Alice } & 0 & 10 & \\
\text { Bob }3 & 5 & & \\
\text { Carolina } & 4 & 8\\
\text { Dean }2 & 7 &
\end{array}
\]
Your program should print out a descriptive message including the student's name every time a student:
A. Arrives at the TA office;
B. Leaves without waiting to get help;
C. Starts getting help; and
D. Leaves the TA office after being helped.
At the end, your program should display:
A. The total number of students that got help;
B. The number of students that left without getting help; and
C. The number of students that had to wait.
NON-DETERMINISTIC OUTPUTS
You will notice your program will produce nondeterministic outputs each time two events happen simultaneously, say, when a student arrives just when another leaves. These non-deterministic outputs occur because we do not control how our Pthreads are scheduled. The sole way to guarantee a deterministic output is to come up with input data that generate schedules where each event happens at a different time.
Pthreads
1. Don't forget the Pthread include:
\#include
and the -lpthread library option in your compilation options
2. All variables that will be shared by all threads must be declared outside of any function as in:
static int nStudents;
3. If you want to pass any data to your thread function, you should declare them void as in:
\[
\begin{array}{l}
\text { void *student(void *arg)\{}\\
\quad \text { myData =(struct sData) arg; }\\
\text {\}// student }
\end{array}
\]
You must immediately copy the contents of myData into local variables.
Since some C++ compilers treat the cast of a
void into anything else as a fatal error, you
might have to use the flag -permissive.
To start a thread that will execute the student
function and pass to it an integer value use:
pthread_t tid[MAXTHREADS];
...
pthread_create(&tid[i], NULL,
student, (void *) SData);
To terminate a given thread from inside its
thread function, use:
pthread_exit((void*)0);
Otherwise, the thread will terminate with the
function. Be sure to release any lock your
thread holds before terminating it.
If you ever have to terminate another thread
function, you could be tempted to use:
#include
pthread_kill(pthread_t tid, int sig);
Note that pthread_kill() is a dangerous
system call because its default action is to crash
the target thread even when it is in a critical
section. The safest alternative to kill a thread that
repeatedly executes a loop is through a shared
terminate variable that the target thread
periodically tests. You will not have to use it in
your program.
To wait for the completion of a specific thread
use:
pthread_join(tid, NULL);
Note that the Pthread library has no way to let
you wait for an unspecified thread and do the
equivalent of:
Your main thread will have to keep track of the
thread ID's of all the threads of all the threads
it has created:
pthread_t tid[MAXSTUDENTS];
..
for (i =0; i nStudents; i++)
pthread_join(tid[i], NULL);
Specific CLang CompileR Issue
If your thread function includes a :
pthread_exit((void*)0);
then your thread function must also exit with a :
pthread_exit((void*)0);
Otherwise, you will get a compiler warning and
your program may crash.
Pthread mutexes
To be accessible from all threads, all Pthread
mutexes must be declared outside of any
function:
static pthread_mutex_t alone;
To create a mutex use:
pthread_mutex_init(&alone, NULL);
You
WRITE IN C + + . Objective You will learn how to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!