Process synchronization

Description

  • Michal Ondrejka

  • GitHub

  • may, 2022

The problem is to simulate the formation of water molecules using two kinds of threads: oxygen and hydrogen. The threads must be synchronized in a way that ensures each molecule is complete before allowing the threads to proceed. In processors there is context switching which means switching between different processes. There are many methods to decide which process should run how long. In this project I faced a difficult challenge where two processes access the same data, but we don't know which will access them first, because the context switching is not being run by the process, rather it is handled by processor controller.
To achieve this I used semaphores. This simple concept uses data structure semaphore, which is represented as a intiger in shared memory. If the semaphore has value zero or less, there already are maximum nuber of processes accessing the shared data. Else the proccess decrement the semaphore and access the data. After it accessed all desired information, the process has to increment the semaphore to ensure more processes will be able to enter this critical area.
In this exact example I was generating water molecules. Oxygen and Hydrogen was created periodicaly, but due to context switching we didn't know the exact time relative to other elements. After the molecules were created, the excess elements were evaluated as not having enough other elements to form a molecule.

Technology