/Teaching/System Level Programming/Assignments/A4


Pull from upstream before solving this task.


Task: Interprocess Communication (IPC)

This exercise should teach you what interprocess communication is for and how you can realize it.

Main Idea

Everybody of us has used interprocess communication already. Mostly unintentionally at this point in your studies. This is why we wanted to take this specific topic into this semester’s course.

To understand the concept of IPC, some major concepts must be learned and understood beforehand.

  • Virtual Memory
  • Process vs. Thread
  • Shared Resources
  • Locking

Some of those terms are already familiar to you, others not. I will not entirely go into details for this assignment, but it’s always useful looking up information based on those keywords.

Implementation details


You MUST NOT change predefined function signatures, sequences of checks or similar. Exploits will automatically result in deductions! Do not remove or add any usleep or assert statements!


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

  • Makefile: Use this file to compile the program or clean up the folder with it.
  • Attendant.h\c: The cleaning personal, Attendant_run will be executed in a child process. TODO make cloakroom accessible in child processes.
  • Basin.h\c: Functionalities for the Basin. TODO make Basin accessible in child processes.
  • Cloakroom.h\c: Main functions for the cloakroom.
  • Customer.h\c: The customers, Customer_run will be executed in a child process. Every customer has to enter the toilet use it, aquire a basin and a soap, wash their hands and return them again. TODO make cloakroom accessible in child processes.
  • Process.h\c: Starts the child processes, no work in here.
  • Soap.h\c: Functionalities for the Soap. TODO make Soap accessible in child processes.
  • Toilet.h\c: Functionalities for the Toilet. TODO make Toilet accessible in child processes.
  • vector.h\c: A simple vector implementation, no work in here.
  • main.c: Parses parameters and inits the cloakroom, TODO make cloakroom accessible in child processes

One TODO is of course to make the program thread safe – but you have to figure out where and how to do that exactly.

IPC in a (way too oversimplified) nutshell

Those simplified explanations should not replace your attendance and attention in the lecture nor serve you the detailed solution to this assignment. It should help you to understand the central concept briefly to make research easier.

Virtual Memory Virtual memory is part of the concept of modern operating systems. As you may suspect, there is a difference between physical and virtual memory. The physical memory provided by, e.g. (SO-)DIMMs has to be managed by the operating system that uses virtual addresses for locating data. So there has to be a sort of translation between the physical and virtual memory addresses. If you are interested in this topic, you can look up this and related articles [1].

Process vs. Threads As many of you already know from previous assignments, processes are treated differently in contrast to threads – well, kind of. As you discovered, a thread can operate on the whole memory the program has mapped. This means, e.g., the heap-allocated by one thread, can be used with any other thread the program launches. It is possible since they share the same virtual address space, as they are all used in one program launched before. So we can deduce that each process has its own virtual memory space. Those virtual memory spaces are strictly separated from each other. Thus, sharing resources between two processes are realized differently to sharing resources in a process. So we have to share memory between two processes. This memory is surprisingly called shared-memory, and it is a central part of IPC [2].

Shared resources To share resources between processes there are a few different possibilities. We could use files as they exist in both processes but this can be slow. It can be improved by using mmap( ... ) on the file which map the file into our virtual memory so it can be used like a pointer. Another possibility is a file created with shm_open( ... ) which also can be used with mmap. The file approach is needed when the processes are not related. In our case the processes are related as the main process is forking over and over again to create children. In this case we can use MAP_ANONYMOUS to completely avoid the overhead of files. This will create a virtual memory region without any underlying file and this region can be shared with child processes. This is the way to go for this assignment.

Locking When we use IPC, mostly, some locking mechanisms have to be used. In our case, semaphores and mutex are the way to go [3].


Holding a lock while going to sleep will lead to point deductions.

Some further hints

What to do before you start?

  • Pull from upstream!
  • Try to understand the program structure.
  • Look at the Manpage and what those parameters of the needed functions are for and how they are used.
  • Only begin, if you understand the basic concept of processes, virtual memory, shared resources as well as mapping them. Bruteforcing will lead to a severe amount of wasted time.
  • I would recommend completing all tasks in the main() and in the new_ functions first!
  • vector.h/c and Process.h/c don’t require any changes
  • You are allowed to add code everywhere in the program and extends structs as you like. But don’t remove or move around existing code ever (this would lead to point deductions).

Submission

Modify the files in your git repository. You can find this file in directory A4. Tag the submission with A4 and push it to the server.

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.

Alina Steinberger <alina.steinberger@student.tugraz.at>