Process Concurrency Notes
Process Concurrency
Introduction
- Modern operating systems handle multiple processes concurrently.
- The system scheduler manages processes and their competition for the CPU.
- The memory manager handles sharing main memory between active processes.
- We will examine how processes coexist and communicate in modern computers.
Concurrency: Pros and Cons
- Pros:
- Good for users, enabling multiprogramming.
- Supports simultaneous execution.
- Allows background execution.
- Cons:
- Complex for the system due to:
- Access to shared data structures.
- Deadlock from resource contention.
- Necessitates process interaction management.
Concepts
- A process is a program in execution with its own state, including resources (memory, open files) and program counter.
- The process's state is its execution context.
- A concurrent program has multiple execution contexts (multi-threaded program).
- A parallel program is a concurrent program where multiple threads execute simultaneously.
Relationships Between Processes
- Fully independent: Separate applications running on a system.
- Independent but related: Multiple users running the same application but accessing/updating a single database.
- Concurrent processes: Cooperating processes (e.g., a C program).
Resources
- Processes compete for resources like CPU, memory, I/O devices, and secondary storage.
- Other resources include data items in main memory, message queues, and shared data structures.
- Resources can be:
- Reusable: CPU, main memory, printer (serially reusable).
- Consumable: Created by one process and consumed by another (e.g., messages).
Mutual Exclusion
- It is necessary for some resources to remain allocated to a process for as long as the process requires it.
- Serial reusable resources require mutual exclusion.
- Example: A printer must remain allocated to a process until printing is complete.
- Mutual exclusion can lead to deadlock, which the OS must handle.
Deadlock
- Deadlock occurs when a process waits for an event that will never occur.
- Example:
- Process P1 has a printer and wants to open file F.
- Process P2 has file F and wants the printer.
- Each process is waiting for a resource held by the other.
Mutual Exclusion First Attempt: Busy Waiting
- A process continuously checks to see if it can enter the critical section.
- The process cannot do anything productive while waiting for permission.
Co-routine
- Designed to pass execution control back and forth between themselves.
- Inadequate to support concurrent processing.
Second Attempt
- Each process checks the other's status but cannot alter it.
- A process checks if another process is in the critical section before entering.
- If no other process is in the critical section, it sets its own status.
- This method does not guarantee mutual exclusion because multiple processes could check the flag and enter concurrently.
Third Attempt
- Set flag to enter critical section before check other processes
- If another process is in the critical section when the flag is set, the process is blocked until the other process releases the critical section
- Deadlock is possible when two process set their flags to enter the critical section. Now each process must wait for the other process to release the critical section
Fourth Attempt
- A process sets its flag to indicate its desire to enter critical section but is prepared to reset the flag
- Other processes are checked. If they are in the critical region, the flag is reset and later set to indicate desire to enter the critical region. This is repeated until the process can enter the critical region
Fourth Attempt (contd.)
- It is possible for each process to set their flag, check other processes, and reset their flags. This scenario will not last very long so it is not deadlock. It is undesirable
Correct Solution
- Each process gets a turn at the critical section
- If a process wants the critical section, it sets its flag and may have to wait for its turn
Critical Region
- Critical region is the code section sensitive to inter-process complications.
- Mutual exclusion is guaranteed by allowing only one process in its critical region at a time.
- The OS must ensure that a process cannot enter a critical region if another process is already in it (using semaphores).
- No two processes simultaneously in the critical region.
- No assumptions about speeds or number of CPUs.
- No process running outside its critical region can block another process.
- No process must wait forever to enter its critical region.
Semaphores: What are they?
- Semaphore is a system of communication using flags.
- There are 2 stations a sender and receiver and they must be able to clearly see the other station.
- For long distance communication, there could be a number of repeater stations between the sender and receiver. Generally a station comprises of 2 people
Semaphores: In Software (contd.)
- Dijkstra proposed semaphores in 1965 to solve problems of concurrent processes.
- Fundamental principle: Processes cooperate via simple signals, forcing a process to stop until a specific signal is received.
- Special variables called semaphores are used for signaling.
signal(s): Transmits a signal.wait(s): Waits to receive a signal.
File and Record Locking
- Mutual exclusion is needed in file processing where multiple users access the same file (e.g., databases).
- Example: Airline booking system where two agents access the last seat simultaneously before the file is updated, leading to double booking.
- Solution: File locking to prevent access during updates.
Types of File Locking
- File lock: The entire file is locked, preventing any other process from accessing it.
- Write lock: A specific set of data is locked preventing other processes from modifying or reading data.
- Read lock: Processes may read data but no process can modify it.