Chapter 2 Processes (Διεργασίες) 2.1 Processes - Διεργασίες 2.2 Interprocess communication – Διαδιεργασιακή επικοινωνία 2.3 Classical IPC problems 2.4 Scheduling - Χρονοπρογραμματισμός 1 Processes The Process Model • Pseudo-parallelism: rapid switching back and forth of the CPU between programs • A process is an executing program + values of the PC+registers+variables. • Think the processes run in parallel – each on a separate CPU. • Independent sequential processes. 2 Processes The Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant 3 Processes The Process Model • Processes must not be programmed with built-in assumptions about timing. • E.g. an idle loop to wait for I/O reading. • Processes are NOT programs. • E.g. someone preparing a birthday cake following a recipe (=the program). The whole activity is the process. 4 Process Creation Principal events that cause process creation 1. System initialization • Execution of a process creation system 1. User request to create a new process 2. Initiation of a batch job 5 Process Termination Conditions which terminate processes 1. Normal exit (voluntary) 2. Error exit (voluntary) 3. Fatal error (involuntary) 4. Killed by another process (involuntary) 6 Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy – UNIX calls this a "process group" • Windows has no concept of process hierarchy – all processes are created equal 7 Process States - Καταστάσεις (1) • Process often need to interact with other processes. • E.g. cat chapter1 chapter2 chapter3 | grep tree • Three states: runnable, blocked, running (έτοιμη ή εκτελέσιμη, υπό αναστολή, εκτελούμενη) • Blocked != Runnable 8 Process States (2) • Possible process states – running (using the CPU now) – blocked (unable to run until an event happens) – ready (runnable; temporarily stopped) • Transitions between states shown 9 Process States (3) • Lowest layer of process-structured OS – handles interrupts, scheduling, i.e. scheduler does more than process scheduling • Above that layer are sequential processes 10 Implementation of Processes (1) • To implement the process model the OS maintains the process table (πίνακας διεργασιών). There is one entry for each process, keeping the process state. 11 Implementation of Processes (2) Fields of a process table entry 12 Implementation of Processes (3) Skeleton of what lowest level of OS does when an interrupt occurs 13 Interprocess Communication Race Conditions (συνθήκες ανταγωνισμού) • Processes frequently communicate with other processes (IPC). • Processes share common storage (read/write). • Situations where two or more processes are reading or writing some shared data and the final result depends on who runs when are called race conditions. • E.g. printing and spooler directories. 14 Interprocess Communication Race Conditions Two processes want to access shared memory at same time 15 Critical Regions (1) – Κρίσιμα τμήματα • Prohibit more than one process to read/write • What is needed is mutual exclusion (αμοιβαίος αποκλεισμός) • Primitive operations to achieve mutual exclusion – a design choice for an OS • The part of the program that accesses shared memory is called critical region 16 Critical Regions (2) 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 17 Critical Regions (3) Mutual exclusion using critical regions 18 Mutual Exclusion with Busy Waiting (1) Αμοιβαίος αποκλεισμός με ενεργό αναμονή • Disabling interrupts (απενεργοποίηση διακοπών): a process disables all the interrupts when entering the critical region and enabling them before exit • Lock variables (μεταβλητές κλειδώματος): A software solution. Unfortunately does not work. • Strict alternation (αυστηρή εναλλαγή) • Peterson’s solution • TSL instruction 19 Mutual Exclusion with Busy Waiting (2) Strict Alternation Proposed solution to critical region problem (a) Process 0. (b) Process 1. 20 Mutual Exclusion with Busy Waiting (3) Peterson’s solution Peterson's solution for achieving mutual exclusion 21 Mutual Exclusion with Busy Waiting (4) TSL instruction= test and set lock – indivisible. Use of a shared variable, flag (could be 0 or 1). Entering and leaving a critical region using the TSL instruction 22 Sleep and Wakeup (1) – Απενεργοποίηση και αφύπνιση • Previous solutions require busy waiting: – the waiting process sits in a tight loop – wastes CPU time – unexpected effects • A process may block instead of waiting • SLEEP and WAKEUP(pid) system calls • Example: producer and consumer (παραγωγός- καταναλωτής) 23 Sleep and Wakeup (2) Producer-consumer problem with fatal race condition24 Semaphores(1) - Σημαφόροι • A semaphore can have the value 0 or > 0 • Two operations, DOWN and UP • DOWN: if semaphore > 0 then semaphore-otherwise it sleeps (atomic action-all in one) • UP: increments the semaphore and waking up a process – again indivisible • DOWN and UP should be atomic • The producer-consumer problem using binary semaphores 25 Semaphores(2) 26 Monitors (1) - Παρακολουθητές • Semaphores are good but there are deadlocks. • A higher level of synchronization principle • A monitor is a collection of procedures, variables and data structures grouped together in a module or package. • Processes can call procedures of the monitor but can not access internal data structures. • !Only one process can be active in a monitor! 27 Monitors (2) Example of a monitor 28 Monitors (3) • Monitors are programming constructs => compilers job to implement (binary semaphores) • Monitors achieve mutual exclusion but processes must block when they can not proceed. • Condition variables (μεταβλητών συνθήκης) + WAIT + SIGNAL • Ensure: no two active processes in the monitor. • WAIT and SIGNAL are similar to SLEEP and WAKEUP, but without the known bug. • Problem: it is a programming language concept 29 Monitors (4) • Outline of producer-consumer problem with monitors – only one monitor procedure active at one time – buffer has N slots 30 Monitors (5) Solution to producer-consumer problem in Java (part 1) 31 Monitors (6) Solution to producer-consumer problem in Java (part 2) 32 Message Passing (1) – Μεταβίβαση μηνύματος • Uses 2 primitives: SEND, RECEIVE – SEND (destination, &message) – RECEIVE(source, &message) • These are system calls, like semaphores • Many interesting problems and design issues – lost messages – naming processes – authentication 33 Message Passing (2) The producer-consumer problem with N messages 34 Message Passing (3) • Implementing Message Passing: – – – – buffers Mailboxes - γραμματοκιβώτιο Rendezvous – συνάντηση pipes - σωληνώσεις 35 Equivalence of Primitives (1) • Using semaphores to implement monitors – Associated with each monitor is a binary semaphore, mutex, initially 1, to control entry to the monitor and an additional semaphore, initially 0, per condition variable • Using semaphores to implement monitors – Associated with each process is a semaphore, initially 0, on which it will block when a SEND or RECEIVE must wait for completion. 36 Equivalence of Primitives (2) • Using monitors to implement semaphores • Using monitors to implement message passing • Using message passing to implement semaphores • Using message passing to implement monitors 37 Dining Philosophers (1) • • • • Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock 38 Dining Philosophers (2) A nonsolution to the dining philosophers problem 39 Dining Philosophers (3) • Modifications: – after taking the left fork, the program checks to see if the right fork is available. If not, put down the left fork and wait for some time. Starvation. – waiting random time – critical applications. – protect the five statements following the call to think by a binary semaphore. Performance problem. 40 Dining Philosophers (4) Solution to dining philosophers problem (part 1) 41 Dining Philosophers (5) Solution to dining philosophers problem (part 2) 42 The Readers and Writers Problem (1) • • • • Big database system (e.g. airline reservation) Processes compete to write and read Many readers concurrently Only one writer at any time – no readers and writers 43 The Readers and Writers Problem (2) A solution to the readers and writers problem 44 The Sleeping Barber Problem (1) 45 The Sleeping Barber Problem (2) Solution to sleeping barber problem. 46 Scheduling- χρονοπρογραμματισμός Introduction to Scheduling (1) • Bursts of CPU usage alternate with periods of I/O wait – a CPU-bound process – an I/O bound process 47 Scheduling Introduction to Scheduling (1) • Schedulers, scheduling algorithms • Goals: fairness, efficiency, response time, turnaround, throughput (δικαιοσύνη, αποδοτικότητα, χρόνος απόκρισης, κύκλος διεκπεραίωσης, ρυθμός απόδοσης) • Processes are unique and unpredicatable • Preemptive scheduling vs. run to completion • Interactive systems vs. batch systems 48 Introduction to Scheduling (2) Scheduling Algorithm Goals 49 Round Robin Scheduling –Εκ περιτροπής • One of the oldest, fairest, most widely used algorithms • Each process is given a time interval, called quantum • Round Robin Scheduling – list of runnable processes – list of runnable processes after B uses up its quantum • Length of quantum could be too short or too long • Process switch or context switch 50 Priority Scheduling (1) - Προτεραιότητες • Round robin assumes all processes equal • May have different groups with priorities • Idea: Each process is assigned a priority and the runnable process with the highest priority is allowed to run • The scheduler may decrease the priority of each process at each clock tick (Unix: nice) • Priorities can be assigned dynamically (I/O) 51 Priority Scheduling (2) A scheduling algorithm with four priority classes 52 Multiple Queues – Πολλαπλές ουρές • Set up priority classes • Processes in the highest class run for one quantum. In the next highest class run for two quanta, four quanta, etc. • Minimize swaps 53 Shortest Job First • Mainly batch systems, running times are known • An example of shortest job first scheduling: 14m 11m • 4 jobs with running time: a,b,c,d. Completion time is: 4a+3b+2c+d. • Minimum average response time. Can it be used for interactive processes? 54 Policy-Driven Scheduling – Εγγυημένος χρονοπρογραμματισμός • Make promises to user and live up to them • Example: if there are n users in the system you will get 1/n CPU power • Keep track of how much CPU time a user has had for all his processes since login • A similar idea can be applied to real-time systems where there are deadlines 55 Scheduling in Real-Time Systems Schedulable real-time system • Given – m periodic events – event i occurs within period Pi and requires Ci seconds • Then the load can only be handled if m Ci 1 i 1 Pi 56 Two Level Scheduling • Some processes may have to be kept in disk. • Major implications – Algorithms must change • Two level-scheduler: – Some subset of runnable processes into memory. Lower-level scheduler picks from these. – Periodically a higher-level scheduler removes processes from main memory to disk and vice versa • How long has it been since the process was swapped in/out • How much CPU the has the process had recently? • How big is the process? What is its priority? 57 Three Level Scheduling Three level scheduling 58 Policy versus Mechanism • Separate what is allowed to be done with how it is done – a process knows which of its children threads are important and need priority • Scheduling algorithm parameterized – mechanism in the kernel • Parameters filled in by user processes – policy set by user process 59