Download presentation
Presentation is loading. Please wait.
Published byJohn Floyd Modified over 9 years ago
1
1 Processes Chapter 2 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling
2
2 Processes The Process Model Multiprogramming of four programs Conceptual model of 4 independent, sequential processes Only one program active at any instant
3
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)
4
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 parameters priority
5
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 ); }
6
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); }
7
7 Process States Possible process states –running –blocked –ready Transitions between states shown
8
8 Interprocess Communication Race Conditions Two processes want to access shared memory at same time process 0process 1
9
9 Critical Regions (0) Four conditions to provide mutual exclusion 1. 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
10
10 Critical Regions (1) Mutual exclusion using critical regions
11
11 Mutual Exclusion with Busy Waiting Proposed solution to critical region problem (a) Process 0. (b) Process 0.
12
Dekker’s algorithm for two processes 1965 no define FALSE 0 no define TRUE 1 no 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 Critical region P0 exit P0 enter P1 enter 1 0 1 1 0 1 0 NEED[1]
13
13 Peterson's solution 1981
14
14 TSL (Test and Set Lock) Dijkstra 1968
15
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); }
16
16 Process Synchronization – Semaphores Dijkstra 1965 int 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); }
17
17 Producer-consumer generalization: counting semaphores 1 7 6 5 4 3 2 0 producer consumer pointer i capacity 8
18
18 Counting Semaphores int 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); }
19
19 Monitors: Hoare 1974, Hansen 1975
20
20 Producer-consumer problem with monitors –only one monitor procedure active at one time –buffer has N slots
21
21 Producer-consumer monitor elaborated monitor 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); }
22
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
23
23 Producer-consumer problem in Java
24
24 Producer-consumer problem in Java (cont)
25
25 Message Passing
26
26 Barriers Use of a barrier –processes approaching a barrier –all processes but one blocked at barrier –last process arrives, all are let through
27
27 Dining Philosophers problem Philosophers eat/think Eating needs 1 forks Pick one fork at a time How to prevent deadlock
28
28 Dining Philosophers problem Wrong solution
29
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; }
30
30 Semaphore solution to dining philosophers
31
31 Cont.
32
32 Monitor solution to Readers/Writers problem monitor 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; }
33
33 The Readers and Writers Problem
34
34 The Sleeping Barber Problem
35
35 The Sleeping Barber Problem
36
36 Scheduling Introduction to Scheduling (0) Bursts of CPU usage alternate with periods of I/O wait –a CPU-bound process –an I/O bound process
37
37 CPU and I/O bound CPU Disk I/O CPU bound – I/O idle I/O bound – CPU idle
38
38 Scheduling Algorithm Goals
39
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.
40
40 Three level scheduling
41
41 Scheduling in Interactive Systems (0) Round Robin Scheduling –list of runnable processes –list of runnable processes after B uses up its quantum
42
42 Scheduling in Interactive Systems (1) A scheduling algorithm with four priority classes
43
43 Scheduling in Real-Time Systems Schedulable real-time system Given –m periodic events –event i occurs within period P i and requires C i seconds Then the load can only be handled if
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.