/Teaching/System Level Programming/Assignments/A2

Pull from upstream before solving this task.

Task: Thread Synchronization

In the upstream repository, you will find a multi-threaded program without any synchronization. Now your task is to add correct and useful synchronization and make the program run flawlessly.
If you don’t know where and how to start, have a look at section help below.

Main Idea

This time, we provide a framework for a Hospital Simulator.

In the morning a lot of ill people are waiting for the hospital to open.

First, they must go to the receptionist’s desk to fill out their registration form. There is only a limited number of receptionist desks – so you need to make sure that only one patient registers at one receptionist desk at a time.

After the patient has filled out their registration form, they can move to the waiting room to wait for their treatment. As soon as a doctor is available, they will take care of the patient.

As soon as the doctor treated the patient, they will let the patient know that they can go home. (Make sure that the patient is notified as soon as the treatment is finished).


  • Identify all actors and resources in the programs.
  • Implement the missing parts of the programs, fulfilling the above requirements.
  • You need to ensure that the resources are acquired in a safe way.
  • Think about potential data races and deadlock scenarios.
  • Use synchronization primitives correctly to make access to all shared resources thread-safe.
  • All shared resources must be accessed by only one thread at a time!
  • As with all other assignments, you can still lose points during the interviews, if you cannot explain your solution in a satisfactory way or any cheating is detected.

Test your programs multiple times and with multiple different parameters to find possible threading errors. Threading errors tend to crash programs/produce incorrect output very irregularly and are sometimes hard to reproduce. Think about your locking structure and check that all shared variables are locked. Also, check if all the locks are acquired and released in the correct order.


This assignment uses CMake as a build system. You should research the CMake build process online and get familiar with it ( CMake is also used in the operating systems course). However, the essential build steps are as follows:

in-source building:

cd A2/
cmake .

out-of-source building:

cd A2/
mkdir build && cd build
cmake ..

The executables are then located in <build-dir>/<name>/<name>.


First of all, the idea of this course is that you should learn to look up technical concepts and implement practical tasks by yourselves. However, here is some help in case you get lost:

Most important resource: Manual pages for the pthreads (POSIXthreads) functions

man pthreads
man pthread_mutex_lock
man pthread_cond_wait
man < ... >

Some important concepts

Always look up the function you use in their man pages. They will tell you how each function behaves in much more detail.

Assignment Tutor

If you have any questions regarding this assignment, try discord first, and bs-helpline@iaik.tugraz.at second. If you have a more direct question regarding your specific solution, you can also ask the tutor who organizes this assignment:

Forian Hirner, florian.hirner@student.tugraz.at