Concurrency is when multiple processes (or threads) execute at the same time. When multiple physical CPUs are available, the processes may execute in parallel. On a single CPU, concurrency may be achieved by time-sharing.
There is a whole study in the process of parallel programming, where you design applications to run on multiple processors simultaneously to increase performance by using child processes and threads (mostly threads).
A big issue is what happens if multiple threads attempt to access the same section of data.
A critical section is a segment of code that cannot be entered by a process while another process is executing a corresponding segment of the code.
Any solution to the critical section problem must satisfy the following requirements:
- Guarantee mutual exclusion: That is only one process may be executing within the critical section at a time.
- Prevent lockout: A process not attempting to enter the critical section must not prevent other processes from entering the critical section. i.e. you can’t be a bully and prevent others from accessing the section.
- Prevent starvation: A process (or a group of processes) must not be able to repeatedly enter the critical section while other processes are waiting to enter.
- Prevent deadlock: Multiple processes trying to enter the critical section at the same time must not block each other indefinitely. This is a common problem in multi-threaded programs where a thread locks access to a variable, then finds that it itself cannot change the variable because someone else is using it. However, it fails to release the variable so the other process can finish with it.
Note: In the book, sometimes their sample C/C++ code uses 0 (zero) to evaluate to false, which is not uncommon in programming, but we’ve typically tried to use boolean variables and values for clarity.
The producer and consumer problem shown in the book is a good example of a common problem run into by multi-threaded programs. The buffer can be read from, or entered into. However, if the producer writes twice before the consumer can read it, there is lost data.
Process Interactions was originally found on Access 2 Learn
One Comment
Comments are closed.