43
1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

Embed Size (px)

Citation preview

Page 1: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

1

Processes

Chapter 2

2.1 Processes2.3 Interprocess communication2.4 Classical IPC problems2.5 Scheduling

Page 2: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

2

ProcessesThe Process Model

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

Page 3: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

3

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 4: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

4

Process example 1/* ------------------------------------------------------------------------------ main ---- example of creating processes ---------------------------------------------------------------------------------*/main ( ) {

int prA( ), prB( );

resume( create( prA, INITSTK, INITPRIO, “proc 1”, 0) );resume( create( prB, INITSTK, INITPRIO, “proc 2”, 0) );

}

/*-------------------------------------------------------------------------------- prA ---- prints ‘A’ for ever----------------------------------------------------------------------------------*/prA( ) { while ( 1 ) putc( CONSOLE, ‘A’ );}

/*-------------------------------------------------------------------------------- prB ---- prints ‘B’ for ever----------------------------------------------------------------------------------*/prB( ) { while ( 1 ) putc( CONSOLE, ‘B’ );}

pointer to the code

pointer to the stack

number of parameterspriority

Page 5: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

5

Process example 2

/* ------------------------------------------------------------------------------ main ---- creating different processes with the same code ---------------------------------------------------------------------------------*/main ( ) {

int prntr( );

resume( create( prntr, INITSTK, INITPRIO, “proc 1”, 1, ‘A’) );resume( create( prntr, INITSTK, INITPRIO, “proc 2”, 1, ‘B’) );

}

/*-------------------------------------------------------------------------------- prntr ---- prints a character for ever----------------------------------------------------------------------------------*/prntr( char ch ) { while ( 1 ) putc( CONSOLE, ch );}

Page 6: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

6

Process example 3/* ------------------------------------------------------------------------------ main ---- example of re-entrant procedures ---------------------------------------------------------------------------------*/void prnt (char ch);main ( ) {

int proc( );resume( create( proc, INITSTK, INITPRIO, “proc 1”, 0) );resume( create( proc, INITSTK, INITPRIO, “proc 2”, 0) );

}/*-------------------------------------------------------------------------------- pr ---- process----------------------------------------------------------------------------------*/proc( ) { int i; for (i=1; i<=10; i++) prnt (i); }/*-------------------------------------------------------------------------------- prnt ---- rentrant procedure----------------------------------------------------------------------------------*/void prnt (int i) { printf ( “number is %d\n” , i); }

Page 7: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

7

Process States

• Possible process states– running– blocked– ready

• Transitions between states shown

Page 8: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

8

Interprocess CommunicationRace Conditions

Two processes want to access shared memory at same time

process 0

process 1

Page 9: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

9

Critical Regions (0)

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 10: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

10

Critical Regions (1)

Mutual exclusion using critical regions

Page 11: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

11

Mutual Exclusion with Busy Waiting

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

Page 12: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

Dekker’s algorithm for two processes 1965no define FALSE 0no define TRUE 1no define N 2

int TURN;int NEED[N];

void mutex_begin (int process) { int me=process; int other=0-process;

NEED[me] = TRUE; while (NEED[other]) { if (TURN != me) { NEED[me] = FALSE; while (TURN != me) ; NEED[me] = TRUE } }}critical_region ()void mutex_end (int process) { int me=process; int other=0-process;

NEED[me] = FALSE; TURN = other;}

0

TURN

P1 out

NEED[0]

P0 in

P1 in

P1 exit

P0 enter

P1 enter

P0 out

Criticalregion

P0 exit

P0 enter

P1 enter

1

0

1

1

0

1

0

NEED[1]

Page 13: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

13

Peterson's solution 1981

Page 14: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

14

TSL (Test and Set Lock) Dijkstra 1968

Page 15: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

15

Producer Consumer example

int n = 0;main ( ) {

int prod( ), cons( );

resume( create( cons, INITSTK, INITPRIO, “cons”, 0) );resume( create( prod, INITSTK, INITPRIO, “prod”, 0) );

}/*-------------------------------------------------------------------------------- producer ---- increments n 1000 times and exit----------------------------------------------------------------------------------*/prod( ) { int i; for (i=1; i<=1000; i++) n++;}/*-------------------------------------------------------------------------------- consumer ---- prints n 1000 times and exit----------------------------------------------------------------------------------*/cons( ) { int i; for (i=1; i<=1000; i++) printf (“n is %d\n”, n);}

Page 16: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

16

Process Synchronization – Semaphores Dijkstra 1965int n = 0;main ( ) {

int prod( ), cons( );int produced, consumed;

consumed = screate (0);produced = screate (1);resume( create( cons, INITSTK, INITPRIO, “cons”, 2, produced, consumed) );resume( create( prod, INITSTK, INITPRIO, “prod”, 2, produced, consumed) );

}/*------------------------------------------------------------------ producer ---- increments n 1000 times and exit-------------------------------------------------------------------*/prod (produced, consumed) { int produced, consumed; int i; for (i=1; i<=1000; i++) { wait (consumed); /* indivisible: decrement consumed and if <0 waits */ n++; signal (produced); /*indivisible: increment produced and wakes up waiting process*/ }}/*----------------------------------------------------------------- consumer ---- prints n 1000 times and exit------------------------------------------------------------------*/cons (produced, consumed) { int produced, consumed; int i; for (i=1; i<=1000; i++) { wait (produced); printf (“n is %d\n, n); signal (consumed); }}

Page 17: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

17

Producer-consumer generalization: counting semaphores

1

7

6

5

4 3

2

0

producer consumer

pointer i

capacity 8

Page 18: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

18

Counting Semaphoresint i = 0, j = 0; n = 8, buffer [n]; main ( ) {

int prod( ), cons( );int mutex, producer, consumer;mutex = screate (1);producer = screate (0); consumer = screate (n);resume( create( cons, INITSTK, INITPRIO, “cons”, 2, mutex, producer, consumer) );resume( create( prod, INITSTK, INITPRIO, “prod”, 2, mutex, producer, consumer) );

}/*------------------------------------------------------------------ prod ---- inserts item in a buffer-------------------------------------------------------------------*/prod (mutex, producer, consumer) { wait (consumer); wait (mutex); buffer [i++ mod n] = item; signal (mutex); signal (producer);}/*----------------------------------------------------------------- consumer ---- removes item from the buffer ------------------------------------------------------------------*/cons (mutex, producer, consumer) { wait (producer); wait (mutex); item = buffer [j++ mod n]; signal (mutex); signal (consumer);}

Page 19: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

19

Monitors: Hoare 1974, Hansen 1975

Page 20: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

20

Producer-consumer problem with monitors

– only one monitor procedure active at one time– buffer has N slots

Page 21: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

21

Producer-consumer monitor elaboratedmonitor ProducerConsumer {

int i = 0; /* inserting pointer */ int j = 0; /* removing pointer */ int n = 8; /* buffer capacity */ int buffer [n]; condition full, empty; /* queues */

void insert (int item) { if ( ((j - i) mod n) == 1) wait (full) ; buffer [i++ mod n ] = item; if (((j - i) mod n) == 2) signal (empty); } int remove ( ) { int item; if ( i == j ) wait (empty) ; item = buffer [ j++ mod n ] ; if (((j – i) mod n) == 2) signal (full); return item; }

}

main ( ) {

resume( create( consumer, INITSTK, INITPRIO, “cons”, 0));resume( create( producer, INITSTK, INITPRIO, “prod”, 0));

}/*------------------------------------------------------------------ producer ---- inserts items-------------------------------------------------------------------*/producer ( ) { while ( 1) { item = produce_item ( );

ProducerConsumer.insert (item); }}/*----------------------------------------------------------------- consumer ---- removes items------------------------------------------------------------------*/consumer ( ) { while ( 1) {

item = ProducerConsumer.remove ( );consume_item(item);

}}

Page 22: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

22

Process states revisited – kernel as a monitor

running

CPU

ready

sem1

sem2

I/O blocked

create

resume

wait (sem2)

wait (sem1)signal (sem1)

signal (sem2)

suspend

kill

kernel

condition

Page 23: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

23

Producer-consumer problem in Java

Page 24: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

24

Producer-consumer problem in Java (cont)

Page 25: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

25

Message Passing

Page 26: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

26

Barriers

• Use of a barrier– processes approaching a barrier– all processes but one blocked at barrier– last process arrives, all are let through

Page 27: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

27

Dining Philosophers problem

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

Page 28: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

28

Dining Philosophers problem

Wrong solution

Page 29: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

29

Monitor solution to Dining Philosophers

monitor forks { int i; condition ready[N]; /* queues */ int no_forks[N] ; for (i=1; i<=N ; i++) no_forks[i] = 2;

void pickup (me) { if (no_forks[me] != 2 ) wait(ready[me] ); no_forks[right(me)] - - ; no_forks[left(me)] - - ; } void putdown (me) { no_forks[right(me)] ++ ; no_forks[left(me)] ++ ; if (no_forks[right(me)] = 2 )

signal(ready[right(me)] ); if (no_forks[left(me)] = 2 )

signal(ready[left(me)] ); }}

#define N 5 /* number of philosophers */#define left (i) (i – 1)%N#define left (i) (i + 1)%N

main ( ) { for (i=1, i<=N, i++) resume( create( philo, INITSTK, INITPRIO, “philo”, 1, i));}/*------------------------------------------------------- common philosopher--------------------------------------------------------*/philo(me ) { while ( 1) { forks.pickup(me); eating_time;

forks.putdown(me); thinking_time; }}

Page 30: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

30

Semaphore solution to dining philosophers

Page 31: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

31

Cont.

Page 32: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

32

Monitor solution to Readers/Writers problemmonitor file_access { int active_writer = 0, no_of_readings = 0; condition ok_to_read, ok_to_write; void start_read ( ) { if ( active_writer || ~empty(ok_to_write) )

wait(ok_to_read); ++ no_of_readings;

signal(ok_to_read); } void end_read ( ) { - - no_of_readings; if ( no_of_readings = 0 ) signal (ok_to_write); } void start_write ( ) { if (no_of_readings != 0 || active_writer ) wait (ok_to_write); active_writer = 1; } void end_write ( ) { active_writer = 0; if (~empty(ok_to_read)) signal (ok_to_read) else signal (ok_to_write); }}

/*------------------------------------------------------- reader process-------------------------------------------------------*/reader_process ( ) { while ( 1) { file_access.start_read ( ); busy reading;

file_access.end_read ( ); thinking_time; }}/*------------------------------------------------------ writer process------------------------------------------------------*/writer_process ( ) { while ( 1) { file_access.start_write ( ); busy writing;

file_access.end_write ( ); thinking_time; }}

Page 33: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

33

The Readers and Writers Problem

Page 34: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

34

The Sleeping Barber Problem

Page 35: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

35

The Sleeping Barber Problem

Page 36: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

36

SchedulingIntroduction to Scheduling (0)

• Bursts of CPU usage alternate with periods of I/O wait– a CPU-bound process– an I/O bound process

Page 37: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

37

CPU and I/O bound

CPU

Disk I/O

CPU bound

– I/O idle

I/O bound

– CPU idle

Page 38: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

38

Scheduling Algorithm Goals

Page 39: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

39

An example of shortest job first scheduling

Average turnaround time

(8 + 12 + 16 + 20)/4 = 56/4 = 14 (4 + 8 + 12 + 20)/4 = 44/4 = 11

Generally for sequence of 4 jobs taking a, b, c, and d average turnaround time is:

(4a + 3b + 2c + d)/4 will be minimal for a < b < c < d.

Page 40: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

40

Three level scheduling

Page 41: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

41

Scheduling in Interactive Systems (0)

• Round Robin Scheduling– list of runnable processes– list of runnable processes after B uses up its quantum

Page 42: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

42

Scheduling in Interactive Systems (1)

A scheduling algorithm with four priority classes

Page 43: 1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling

43

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