1 / 43

Processes

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.

sancho
Télécharger la présentation

Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. ProcessesThe Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant

  3. Process Termination Conditions which terminate processes • Normal exit (voluntary) • Error exit (voluntary) • Fatal error (involuntary) • Killed by another process (involuntary)

  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 number of parameters priority pointer to the stack

  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. 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. Process States • Possible process states • running • blocked • ready • Transitions between states shown

  8. Interprocess CommunicationRace Conditions process 0 process 1 Two processes want to access shared memory at same time

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

  10. Critical Regions (1) Mutual exclusion using critical regions

  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 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; }

  13. Peterson's solution 1981

  14. TSL (Test and Set Lock) Dijkstra 1968

  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. 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. Producer-consumer generalization: counting semaphores capacity 8 pointer i 7 0 1 6 consumer producer 2 5 4 3

  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. Monitors: Hoare 1974, Hansen 1975

  20. Producer-consumer problem with monitors • only one monitor procedure active at one time • buffer has N slots

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

  23. Producer-consumer problem in Java

  24. Producer-consumer problem in Java (cont)

  25. Message Passing

  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. Dining Philosophers problem • Philosophers eat/think • Eating needs 1 forks • Pick one fork at a time • How to prevent deadlock

  28. Dining Philosophers problem Wrong solution

  29. 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)] ); } }

  30. Semaphore solution to dining philosophers

  31. Cont.

  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. The Readers and Writers Problem

  34. The Sleeping Barber Problem

  35. The Sleeping Barber Problem

  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

  37. CPU and I/O bound Disk I/O I/O bound – CPU idle CPU bound – I/O idle CPU

  38. Scheduling Algorithm Goals

  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. Three level scheduling

  41. Scheduling in Interactive Systems (0) • Round Robin Scheduling • list of runnable processes • list of runnable processes after B uses up its quantum

  42. Scheduling in Interactive Systems (1) A scheduling algorithm with four priority classes

  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

More Related