590 likes | 736 Views
Processes (Διεργασίες). 2.1 Processes - Διεργασίες 2.2 Interprocess communication – Διαδιεργασιακή επικοινωνία 2.3 Classical IPC problems 2.4 Scheduling - Χρονοπρογραμματισμός. Chapter 2. Processes The Process Model.
E N D
Processes (Διεργασίες) 2.1 Processes - Διεργασίες 2.2 Interprocess communication – Διαδιεργασιακή επικοινωνία 2.3 Classical IPC problems 2.4 Scheduling - Χρονοπρογραμματισμός Chapter 2
ProcessesThe 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.
ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant
ProcessesThe 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.
Process Creation Principal events that cause process creation • System initialization • Execution of a process creation system • User request to create a new process • Initiation of a batch job
Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)
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
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
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
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
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.
Implementation of Processes (2) Fields of a process table entry
Implementation of Processes (3) Skeleton of what lowest level of OS does when an interrupt occurs
Interprocess CommunicationRace 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.
Interprocess CommunicationRace Conditions Two processes want to access shared memory at same time
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
Critical Regions (2) Four conditions to provide mutual exclusion • 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
Critical Regions (3) Mutual exclusion using critical regions
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
Mutual Exclusion with Busy Waiting (2)Strict Alternation Proposed solution to critical region problem (a) Process 0. (b) Process 1.
Mutual Exclusion with Busy Waiting (3)Peterson’s solution Peterson's solution for achieving mutual exclusion
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
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 (παραγωγός- καταναλωτής)
Sleep and Wakeup (2) Producer-consumer problem with fatal race condition
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
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!
Monitors (2) Example of a monitor
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
Monitors (4) • Outline of producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots
Monitors (5) Solution to producer-consumer problem in Java (part 1)
Monitors (6) Solution to producer-consumer problem in Java (part 2)
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
Message Passing (2) The producer-consumer problem with N messages
Message Passing (3) • Implementing Message Passing: • buffers • Mailboxes - γραμματοκιβώτιο • Rendezvous – συνάντηση • pipes - σωληνώσεις
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.
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
Dining Philosophers (1) • Philosophers eat/think • Eating needs 2 forks • Pick one fork at a time • How to prevent deadlock
Dining Philosophers (2) A nonsolution to the dining philosophers problem
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.
Dining Philosophers (4) Solution to dining philosophers problem (part 1)
Dining Philosophers (5) Solution to dining philosophers problem (part 2)
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
The Readers and Writers Problem (2) A solution to the readers and writers problem
The Sleeping Barber Problem (2) Solution to sleeping barber problem.
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
SchedulingIntroduction 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
Introduction to Scheduling (2) Scheduling Algorithm Goals
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