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

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
G53OPS Operating Systems
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Review: Producer-Consumer using Semaphores #define N 100// number of slots in the buffer Semaphore mutex = 1;// controls access to critical region Semaphore.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Processes (Διεργασίες)
Processes and Threads.
1 Process States (1) Possible process states –running –blocked –ready Transitions between states shown.
Scheduling Chap 2. Scheduling Introduction to Scheduling (1) Bursts of CPU usage alternate with periods of I/O wait –a CPU-bound process –an I/O bound.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
操作系统原理 OPERATING SYSTEM Chapter 2 Processes and Threads 进程与线程.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Inter Process Communication
Interprocess Communication Race Conditions
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Classical Synchronization Problems
Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Lecture 13: Producer-Consumer and Semaphores
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 5: Process Synchronization (Con’t)
Message Passing, Scheduler
Operating Systems Chapter 2: Processes and Threads
Semaphore Originally called P() and V() wait (S) { while S <= 0
Processes and Threads Part III
Chapter 7: Synchronization Examples
Lecture 13: Producer-Consumer and Semaphores
Presentation transcript:

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

2 Processes The 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 1.Normal exit (voluntary) 2.Error exit (voluntary) 3.Fatal error (involuntary) 4.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 pointer to the stack number of parameters priority

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 Communication Race Conditions Two processes want to access shared memory at same time process 0process 1

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

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 NEED[1]

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 producer consumer pointer i capacity 8

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 CPU ready sem1 sem2 I/O blocked create resume wait (sem2) wait (sem1) signal (sem1) signal (sem2) suspend kill kernel condition

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 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 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 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 CPU and I/O bound CPU Disk I/O CPU bound – I/O idle I/O bound – CPU idle

38 Scheduling Algorithm Goals

39 An example of shortest job first scheduling Average turnaround time ( )/4 = 56/4 = 14 ( )/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 P i and requires C i seconds Then the load can only be handled if