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

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

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 (Διεργασίες)

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

1

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

Chapter 2

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

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

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.

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

3

ProcessesThe Process Model

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

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

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.

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

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

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

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)

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

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

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

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

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

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

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

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

Page 11: 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.

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

12

Implementation of Processes (2)

Fields of a process table entry

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

13

Implementation of Processes (3)

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

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

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.

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

15

Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time

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

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

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

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

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

18

Critical Regions (3)

Mutual exclusion using critical regions

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

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

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

20

Mutual Exclusion with Busy Waiting (2)Strict Alternation

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

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

21

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

Peterson's solution for achieving mutual exclusion

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

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).

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

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

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

24

Sleep and Wakeup (2)

Producer-consumer problem with fatal race condition

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

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

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

26

Semaphores(2)

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

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!

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

28

Monitors (2)

Example of a monitor

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

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

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

30

Monitors (4)

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

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

31

Monitors (5)

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

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

32

Monitors (6)

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

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

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

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

34

Message Passing (2)

The producer-consumer problem with N messages

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

35

Message Passing (3)

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

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

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.

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

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

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

38

Dining Philosophers (1)

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

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

39

Dining Philosophers (2)

A nonsolution to the dining philosophers problem

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

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.

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

41

Dining Philosophers (4)

Solution to dining philosophers problem (part 1)

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

42

Dining Philosophers (5)

Solution to dining philosophers problem (part 2)

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

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

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

44

The Readers and Writers Problem (2)

A solution to the readers and writers problem

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

45

The Sleeping Barber Problem (1)

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

46

The Sleeping Barber Problem (2)

Solution to sleeping barber problem.

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

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

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

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

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

49

Introduction to Scheduling (2)

Scheduling Algorithm Goals

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

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

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

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)

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

52

Priority Scheduling (2)

A scheduling algorithm with four priority classes

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

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

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

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

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

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

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

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

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

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?

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

58

Three Level Scheduling

Three level scheduling

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

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