Concurrent Processes Lecture 5 Introduction • Modern operating systems can handle more than one process at a time • System scheduler manages processes and their competition for the CPU • Memory manager role is to manage sharing of main memory between active processes • Look at how processes coexist and communicate with each other on modern computer Concurrency pros and cons • Concurrency is good for users – One of the reasons for multiprogramming • Working on the same problem, simultaneous execution of programs, background execution • Concurrency is a “pain in the neck” for the system – Access to shared data structures – Deadlock due to resource contention – Enabling process interaction Concepts: • A process is a program in execution. • A process has state, including resources like memory and open files, as well as the contents of its program counter. • This is the process’s execution context. • A concurrent program has more than one execution context. • This type of program is also referred to as a multi-threaded program. • A parallel program is a concurrent program with more than one thread executing simultaneously. Relationships between processes • Fully indenendant – separate applications running on the one system • Indenpendant but related – example users running their own copy of a data entry program but accessing and updating the one database • Concurrent processes – set of cooperating processes, example C program Resources • Processes compete for CPU, memory, I/O devices, secondary storage • Other resources may include data items in main memory such as message queues, shared data structures. • Resources can be reusable or consumable, reusable examples are CPU, main memory (serial reusable example is printer). Consumable resource is created by one process and consumed by another example is message sent between one process and another. Mutual Exclusive • It is necessary for some resources to remain allocated to a process for as long as process requires. Serial reusable resources require mutual exclusion. Example printer is being used by a process then it must remain allocated to the process until printout is complete. • Mutual exclusion gives rise to another problem which OS must handle – deadlock. Deadlock • Deadlock is where a process is waiting for an event that will never occur. • Example Process P1 has a printer allocated and is attempting to open a file F. Process P2 has already obtained exclusive access to file F and now requests printer. Each process is waiting for a resource held by the other, while holding a resource required by the other. ai W P1 Holding tin g W ai tin g fo r r fo P2 Holding File F Mutual Exclusion First Attempt • Busy Waiting – Process is always checking to see if it can enter the critical section – Process can do nothing productive until it gets permission to enter its critical section Co-routine • Designed to be able to pass execution control back and forth between themselves • Inadequate to support concurrent processing Second Attempt • Each process can examine the other’s status but cannot alter it • When a process wants to enter the critical section is checks the other processes first • If no other process is in the critical section, it sets its status for the critical section • This method does not guarantee mutual exclusion • Each process can check the flags and then proceed to enter the critical section at the same time 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 its 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 • The area of a process which is sensitive to inter-process complications is called the critical region. • To guarantee mutual exclusion only one process is allowed to enter its critical region at a time. • OS need a system to ensure process cannot enter critial region if another process is in it’s. (semaphores) Mutual Exclusion with Busy Waiting • Figure 2-9 Mutual exclusion using critical regions. Critical Regions (1) Four conditions to provide mutual exclusion 1. 2. 3. 4. No two processes simultaneously in critical region No assumptions made about speeds or numbers of CPUs No process running outside its critical region may 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 reciever 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 reciever. Generally a station comprises of 2 people. Semaphores: In Software (contd.) • Dijkstra in 1965 proposed semaphores as a solution to the problems of concurrent processes. • The fundamental principle is: That two or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specified place until it has received a specific signal. • For signaling, special variables called semaphores are used. – Primitive signal (s) is used to transmit a signal – Primitive wait (s) is used to receive a signal File and record locking • Mutual exclusion is required file processing where several users are accessing the same file (databases) • Example airline booking system, one seat left two agents access at same time before file is updated and seat is double booked. • Solution is file locking preventing access to the file while it is being updated Types of file locking • File lock – whole file is locked, prevents any other process accessing any part of that file. • Write lock – if a specific set of data (eg order file) is write locked prevents other process modifying or reading data • Read lock – set of data is read locked other processes may read data but no process can modify it.