/Teaching/System Level Programming/Assignments/A1

Pull from upstream before solving this task.

Task: Multithreading

This exercise should teach you what multi threading and concurrency programming is for and what side effects it has.

Please read the whole description of this assignment carefully beforehand, you will need all of the information and it will save you a lot of time.

Main Idea

Since many of us are not just software developers but also serious gamers, we decided to show you the aspects of multi threading with a simple ASCII game. You can see if you have done something right by observing that the game is getting more and more playable. At the beginning you will only see a plain game map without any content.

The game might be seen as a heavily simplified “Achtung die Kurve” and “Snake” crossover. The goal is to control the users snake in order to avoid collisions with enemy snakes and collect as much chests as possible.

The game consists of 3 different entities:

  • Enemy Snakes: The enemies are spawning at a random position and will kill your snake in case you hit them.(represented by a ‘S’ on red background)
  • User Snake: For collecting chests and avoiding enemies the user’s snake can be moved by the arrow keys. (represented by a ‘S’ on blue background). The snake shall be killed when ‘q’ is pressed, when it hits an enemy snake or when it hits the boarder.
    (Lifepoints are set to 0)
  • Chests: In order to get points you may collect some chests (represented by ‘#’), ones one chest is collected the next chest shall spawn immediately. Each collected chest gives 2000 points.


You’ll have to install ncurses on your system. In Debian/Ubuntu the following line will do the job:

sudo apt-get install libncurses5-dev libncursesw5-dev

In case ncursesw5 is not working or not available for your system you may also use ncursesw6. If that’s the case you may replace the line

NCURSES = $(shell ncursesw5-config --cflags --libs)

in the Makefile with the line:

NCURSES = $(shell ncursesw6-config --cflags --libs)

Do NOT make any other changes in the Makefile!

Implementation details

When you open the folder of this task, you will notice four files:

  • Makefile: Use this file to compile and run the program or clean up the folder with it.
  • snake.h: This file contains relevant includes, typedefs and predefined values, you can change them as you wish but be careful, you must not push this file, therefor all changes are irrelevant for us.
  • helpers.c: This file contains functions, which make your implementation run. As with your header file, you can change everything in there, but as the header file, you must not submit this file as well, so your changes are irrelevant to the testing system.
  • snake.c: This is the only file that will be checked and used by the test system. Please follow the TODOs and ONLY change and add code between TODO BEGIN and TODO END!

You must not create ANY global variables, delete existing code (comments can be changed and added, of course) or rearrange existing code! Be careful, do not delete any needed functions declared in the header file.

For using POSIX-Threads use -pthread and -lncurses (for displaying your board) as compiler flags. Because of the function usleep(), which is not included in the std library, use the -std=gnu11 flag instead of the std=c11 one!!!

What to do before you start?

  • Pull from upstream!
  • Carefully look at the TODOs in the snake.c file.
  • Look at the Manpage and what those parameters of the needed functions are for and how they are used. (pthread_create( ... ), pthread_cancel( ... ), pthread_join( ... ), etc [https://man7.org/linux/man-pages/man7/pthreads.7.html])
  • Only begin, if you understand the basic concept, what a thread is and what it does. Bruteforcing will lead to a serious amount of wasted time.
    • Try to understand the different functions in the snake.c file and their connections
    • I would recommend to start with the user_snake spawning task in the main() function!
    • Hint: Make sure, that you reuse as many variables as possible.

I prepared some data structures to save some time. You can find them in the header file. There is a predefined structure for the parameters you have to pass. USE IT!

You’ll encounter some race conditions here and then (especially at the collision detection). That’s perfectly fine. How this is preventable will be discussed in the Thread Synchronization assignment. If you want some further information, take a look at this page: https://en.wikipedia.org/wiki/Race_Condition

Helpful resources

You find all the needed information on the manual pages. You can access them by either typing man into the terminal or by reading them on various websites. For example the manual for pthread_create( ... ) may be accessed by the command man pthread_create . For this exercise you might find the following links useful.


In case you prefer a german source:


Modify snake.c in your git repository. You can find this file in directory A1. Tag the submission with A1 and push it to the server. Do not add any additional files to the folder!

Assignment Tutor

If you have any questions regarding this assignment, try discord first, and os@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 or look on the 3rd floor in the “Lernzentrum”, you have good chances to meet me there during the week (of course only if the current COVID situation allows it).

Mario Bischof, mario.bischof@student.tugraz.at