0% found this document useful (0 votes)
29 views2 pages

EECS3321.3 Assignment 2. Using Threads. Due Date: Tue. July. 5 11:59midnight

This document provides instructions for Assignment 2 on using threads in EECS3321.3. Students are asked to write a program that simulates a client-server system using probabilistic methods and threads. The program should track job statistics over multiple time ticks as client and server threads run concurrently. POSIX threads, mutexes, and condition variables must be used to synchronize access to shared resources like job queues and statistics. Command line arguments control simulation parameters.

Uploaded by

NABEL WAMBULWA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

EECS3321.3 Assignment 2. Using Threads. Due Date: Tue. July. 5 11:59midnight

This document provides instructions for Assignment 2 on using threads in EECS3321.3. Students are asked to write a program that simulates a client-server system using probabilistic methods and threads. The program should track job statistics over multiple time ticks as client and server threads run concurrently. POSIX threads, mutexes, and condition variables must be used to synchronize access to shared resources like job queues and statistics. Command line arguments control simulation parameters.

Uploaded by

NABEL WAMBULWA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EECS3321.

3
Assignment 2.

Using Threads.

Due Date: Tue. July. 5 11:59midnight.


1. POSIX threads
Try to answer all the following questions before you start programming. Most important
try to find the manual page that gives the answer rather than just guessing or googling it.
(1) What is the naming conventions for pthread function calls?
(2) Enumerate the synchronization mechanisms for POSIX threads.
(3) Why is it a good programming practice to call pthread_cond_wait from
within a loop?
(4) Which thread executes after a thread is woken up from a condition variable.
(5) What happens if a thread blocks on a condition variable while holding two mu-
texes.
(6) What is a detacched thread.

2. Probabilistic Simulation of a Client-Server system


Write a program that does a probabilistic simulation of a client server system. The
clients generate job packages at random times and place them on the global queue. Every
package takes a random amount of time to execute and if the servers are busy the package
stays on the queue. At every time interval a the client and the server are woken up to sim-
ulate work done during this interval.
The servers need one parameter, the completion rate µ (mu), also called death rate,
of the currently executing process. At every activation of the server, if there is a job exe-
cuting, the server generates a random number between zero and one and compares it to µ.
If the random number is smaller than µ the job execution is terminated. If there is no job
executing, the server gets a job from the global queue if available. The state of the server
can be either busy or not-busy.
The clients need one parameter as well, the birth rate λ (lambda) of the jobs. Again,
at every activation (if it is not waiting for a previous job to complete) the client generates
a random number between zero and one and compares it to λ . If the random number is
smaller than λ a new job is generated and placed on the global queue.
The program has to have a simple queue (really simple, it just keeps track of the
number of items in it and nothing else since all the jobs are really the same). It also needs
to keep a few statistics (like number of jobs generated, total number of jobs in the queue,
number of clock ticks so far, etc) from which to compute useful statistics.

1
The program prints the following job statistics, one per line, properly annotated:
(1) Average waiting time (AWT)
(2) Average execution time (AXT)
(3) Average turnaround time (ATA)
(4) Average queue length (AQL)
(5) Average interarrival time. (AIA)
The command line options to the program are the --lambda [0.005], --mu [0.01],
--servers[2], --clients[2], --ticks[1000].
Your program will use pthreads. Every server, and every client will be separate
threads. In addition there is one thread for the clock and this can be the original thread. At
every tick of the clock thread, all other threads get activated, do whatever they need to do
and then wait for the next tick. Whenever the clock sees that all the threads have com-
pleted their work for the current tick, it emits the next tick. The program runs for the
number of ticks specified.

3. Structure and tips


You will need several mutex variables for this program. One for protecting the up-
date of the statistics update, one for protecting access to global variables like number of
servers or clients blocked that will also accompany the condition variables on which
clients, servers and the clock are blocked and one for the queue. You will need one condi-
tion variable for the clock thread to block on while the client and server threads are work-
ing and one condition variable for the client and server threads to block on waiting for the
next clock tick.
Your main program goes through the following steps. Parses the command line argu-
ments, initializes the condition variables and mutexes, allocates space for the
thread_arg data structures (used for passing arguments to the threads) and fills them
with the appropriate values and creates the threads. It then calls the clk function, essen-
tialy transforming itself to the clock thread.
The server thread can be in two states: busy and not-busy. If it is busy, after updating
the statistics, it uses the random number generator to decide if it will switch to not-busy.
Then it blocks to wait for the next tick.
The client uses the random number generator to decide if it creates a new job. The
client cannnot create a job before the previous job has been serviced. It updates the statis-
tics and then blocks waiting for the next clock tick.
The clock thread (the original thread) waits for the clients and servers to block and
then increments the clock tick, updates the statistics and then blocks again.
Both clients and servers before blocking they check if they are the last one to block.
If they are the last one blocking they wake up the clock.

You might also like