430 likes | 624 Views
Processes. 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling. Chapter 2. Processes The Process Model. Multiprogramming of four programs Conceptual model of 4 independent, sequential processes Only one program active at any instant. Process Termination.
E N D
Processes 2.1 Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Chapter 2
ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant
Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)
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 number of parameters priority pointer to the stack
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 ); • }
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); • }
Process States • Possible process states • running • blocked • ready • Transitions between states shown
Interprocess CommunicationRace Conditions process 0 process 1 Two processes want to access shared memory at same time
Critical Regions (0) 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 (1) Mutual exclusion using critical regions
Mutual Exclusion with Busy Waiting Proposed solution to critical region problem (a) Process 0. (b) Process 0.
Dekker’s algorithm for two processes 1965 P0 exit P1 out P1 enter P0 enter 0 P0 in 1 NEED[0] 0 Critical region 1 0 NEED[1] 1 0 P1 in 1 P1 enter P0 out P0 enter P1 exit TURN 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; }
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); • }
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); • } • }
Producer-consumer generalization: counting semaphores capacity 8 pointer i 7 0 1 6 consumer producer 2 5 4 3
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); • }
Producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots
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); • } • }
Process states revisited – kernel as a monitor running ready CPU kernel I/O blocked condition signal (sem2) wait (sem2) sem2 wait (sem1) signal (sem1) sem1 suspend resume create kill
Barriers • Use of a barrier • processes approaching a barrier • all processes but one blocked at barrier • last process arrives, all are let through
Dining Philosophers problem • Philosophers eat/think • Eating needs 1 forks • Pick one fork at a time • How to prevent deadlock
Dining Philosophers problem Wrong solution
Monitor solution to Dining Philosophers #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; } } 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)] ); } }
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; } }
SchedulingIntroduction to Scheduling (0) • Bursts of CPU usage alternate with periods of I/O wait • a CPU-bound process • an I/O bound process
CPU and I/O bound Disk I/O I/O bound – CPU idle CPU bound – I/O idle CPU
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.
Scheduling in Interactive Systems (0) • Round Robin Scheduling • list of runnable processes • list of runnable processes after B uses up its quantum
Scheduling in Interactive Systems (1) A scheduling algorithm with four priority classes
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