Processes (Διεργασίες)

Post on 03-Jan-2016

34 views 0 download

description

Processes (Διεργασίες). 2.1 Processes - Διεργασίες 2.2 Interprocess communication – Διαδιεργασιακή επικοινωνία 2.3 Classical IPC problems 2.4 Scheduling - Χρονοπρογραμματισμός. Chapter 2. Processes The Process Model. - PowerPoint PPT Presentation

Transcript of Processes (Διεργασίες)

1

Processes (Διεργασίες)

Chapter 2

2.1 Processes - Διεργασίες2.2 Interprocess communication – Διαδιεργασιακή επικοινωνία2.3 Classical IPC problems2.4 Scheduling - Χρονοπρογραμματισμός

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.

3

ProcessesThe Process Model

• Multiprogramming of four programs• Conceptual model of 4 independent, sequential processes• Only one program active at any instant

4

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.

5

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

6

Process Termination

Conditions which terminate processes

1. Normal exit (voluntary)

2. Error exit (voluntary)

3. Fatal error (involuntary)

4. Killed by another process (involuntary)

7

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

8

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

9

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

10

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

11

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.

12

Implementation of Processes (2)

Fields of a process table entry

13

Implementation of Processes (3)

Skeleton of what lowest level of OS does when an interrupt occurs

14

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.

15

Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time

16

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

17

Critical Regions (2)

Four conditions to provide mutual exclusion1. No two processes simultaneously in critical region

2. No assumptions made about speeds or numbers of CPUs

3. No process running outside its critical region may block another process

4. No process must wait forever to enter its critical region

18

Critical Regions (3)

Mutual exclusion using critical regions

19

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

20

Mutual Exclusion with Busy Waiting (2)Strict Alternation

Proposed solution to critical region problem(a) Process 0. (b) Process 1.

21

Mutual Exclusion with Busy Waiting (3)Peterson’s solution

Peterson's solution for achieving mutual exclusion

22

Mutual Exclusion with Busy Waiting (4)

Entering and leaving a critical region using the

TSL instruction

TSL instruction= test and set lock – indivisible.Use of a shared variable, flag (could be 0 or 1).

23

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 (παραγωγός- καταναλωτής)

24

Sleep and Wakeup (2)

Producer-consumer problem with fatal race condition

25

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

26

Semaphores(2)

27

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!

28

Monitors (2)

Example of a monitor

29

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

30

Monitors (4)

• Outline of producer-consumer problem with monitors– only one monitor procedure active at one time– buffer has N slots

31

Monitors (5)

Solution to producer-consumer problem in Java (part 1)

32

Monitors (6)

Solution to producer-consumer problem in Java (part 2)

33

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

34

Message Passing (2)

The producer-consumer problem with N messages

35

Message Passing (3)

• Implementing Message Passing:– buffers– Mailboxes - γραμματοκιβώτιο– Rendezvous – συνάντηση – pipes - σωληνώσεις

36

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.

37

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

38

Dining Philosophers (1)

• Philosophers eat/think• Eating needs 2 forks• Pick one fork at a time • How to prevent deadlock

39

Dining Philosophers (2)

A nonsolution to the dining philosophers problem

40

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.

41

Dining Philosophers (4)

Solution to dining philosophers problem (part 1)

42

Dining Philosophers (5)

Solution to dining philosophers problem (part 2)

43

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

44

The Readers and Writers Problem (2)

A solution to the readers and writers problem

45

The Sleeping Barber Problem (1)

46

The Sleeping Barber Problem (2)

Solution to sleeping barber problem.

47

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

48

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

49

Introduction to Scheduling (2)

Scheduling Algorithm Goals

50

Round Robin Scheduling –Εκ περιτροπής

• 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

• One of the oldest, fairest, most widely used algorithms• Each process is given a time interval, called quantum

51

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)

52

Priority Scheduling (2)

A scheduling algorithm with four priority classes

53

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

54

Shortest Job First

• Mainly batch systems, running times are known

• An example of shortest job first scheduling:

• 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?

14m11m

55

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

56

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

1

1m

i

i i

C

P

57

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?

58

Three Level Scheduling

Three level scheduling

59

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