Concurrency in operating systems allows multiple threads or processes to be managed simultaneously for efficient resource use. Processes run independently, whereas threads, lighter and quicker, operate within processes. Synchronization mechanisms like locks prevent access conflicts to shared resources. The operating system’s scheduler enables rapid context switching, creating the illusion of parallelism even on single-core machines.
Challenges include: deadlocks, where processes wait indefinitely for resources starvation, where some never get enough resources.
Whether through preemptive or cooperative multitasking, managing concurrency is essential for leveraging multi-core processors and ensuring responsive, robust system performance.
Producer / Consumer Problem
The producer generates data to be written in the buffer. The consumer reads data from the buffer and does something with it. However the buffer is bounded so it has limited places to hold data.
If the rate at which the producer works is different than the rate the consumer works then we have a problem.
Producers will have to suspend work until the buffer has empty spaces. Therefore we need access control for the buffer
Consumers might need to suspend work if the buffer is empty and cannot progress until the producer has written something to the buffer.
Critical Region
Area of code changing a common resource Only one process at a time can be in a critical region (CR) Controlling critical regions can be done through Hardware locks: (actual wires controlling read/write to memory/registers) Software locks: special synchronisation messages in the OS Modern OS offers an ATOMIC operation, that is, a part of a process that cannot be interrupted until it completes all its commands Context switch is an atomic operation
Deadlocks
- Process holding CR (e.g. lady in phone booth)
- Other processes blocking to get CR (e.g. queue)
Process holding CR blocking waiting for something else (e.g. first person in queue blocks the phone booth door and lady cannot get out)
Infinite loop of blocking and holding
Solutions to Deadlocks:
There are algorithms for this, e.g. Banker’s Algorithm Mostly it succeeds If it fails it can
- Try to recover
- Terminate the processes causing the deadlock
- Let the program hang