Chapter 5 CPU Scheduling Operating System Concepts with

  • Slides: 32
Download presentation
Chapter 5: CPU Scheduling Operating System Concepts with Java – 8 th Edition 5.

Chapter 5: CPU Scheduling Operating System Concepts with Java – 8 th Edition 5. 1 Silberschatz, Galvin and Gagne © 2009

Algorithm Evaluation n How to compare scheduling algorithms? n How to determine which is

Algorithm Evaluation n How to compare scheduling algorithms? n How to determine which is a good algorithm for a given system n Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload n Queueing models n Simulation Operating System Concepts with Java – 8 th Edition 5. 2 Silberschatz, Galvin and Gagne © 2009

Evaluation of CPU schedulers by Simulation Operating System Concepts with Java – 8 th

Evaluation of CPU schedulers by Simulation Operating System Concepts with Java – 8 th Edition 5. 3 Silberschatz, Galvin and Gagne © 2009

End of Chapter 5 Operating System Concepts with Java – 8 th Edition 5.

End of Chapter 5 Operating System Concepts with Java – 8 th Edition 5. 4 Silberschatz, Galvin and Gagne © 2009

Chapter 6: Process Synchronization Operating System Concepts with Java – 8 th Edition 5.

Chapter 6: Process Synchronization Operating System Concepts with Java – 8 th Edition 5. 5 Silberschatz, Galvin and Gagne © 2009

Chapter 6: Process Synchronization n n n n Background The Critical-Section Problem Peterson’s Solution

Chapter 6: Process Synchronization n n n n Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions Operating System Concepts with Java – 8 th Edition 5. 6 Silberschatz, Galvin and Gagne © 2009

Objectives n To introduce the critical-section problem, whose solutions can be used to ensure

Objectives n To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data n To present both software and hardware solutions of the critical-section problem n To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity Operating System Concepts with Java – 8 th Edition 5. 7 Silberschatz, Galvin and Gagne © 2009

Background n Processes and threads provide concurrency n Data sharing among cooperating processes/threads n

Background n Processes and threads provide concurrency n Data sharing among cooperating processes/threads n Simultaneous access to shared data (especially simultaneous writes) lead to data inconsistency n Producer/Consumer problem example Operating System Concepts with Java – 8 th Edition 5. 8 Silberschatz, Galvin and Gagne © 2009

Producer while (count == BUFFER. SIZE) ; // do nothing // add an item

Producer while (count == BUFFER. SIZE) ; // do nothing // add an item to the buffer[in] = item; in = (in + 1) % BUFFER. SIZE; ++count; Operating System Concepts with Java – 8 th Edition 5. 9 Silberschatz, Galvin and Gagne © 2009

Consumer while (count == 0) ; // do nothing // remove an item from

Consumer while (count == 0) ; // do nothing // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER. SIZE; --count; Operating System Concepts with Java – 8 th Edition 5. 10 Silberschatz, Galvin and Gagne © 2009

Race Condition n count++ can be implemented as register 1 = count register 1

Race Condition n count++ can be implemented as register 1 = count register 1 = register 1 + 1 count = register 1 n count-- can be implemented as register 2 = count register 2 = register 2 - 1 count = register 2 Operating System Concepts with Java – 8 th Edition 5. 11 Silberschatz, Galvin and Gagne © 2009

Race Condition (Contd. ) n Consider a scenario with one producer and one consumer

Race Condition (Contd. ) n Consider a scenario with one producer and one consumer count++ and count-- executed simultaneously n Count value should remain unaltered n Execution interleaving with “count = 5” initially: T 0: producer execute register 1 = count {register 1 = 5} T 1: producer execute register 1 = register 1 + 1 {register 1 = 6} T 2: consumer execute register 2 = count {register 2 = 5} T 3: consumer execute register 2 = register 2 - 1 {register 2 = 4} T 4: producer execute count = register 1 {count = 6 } T 5: consumer execute count = register 2 {count = 4} l Operating System Concepts with Java – 8 th Edition 5. 12 Silberschatz, Galvin and Gagne © 2009

Critical Section n A conceptual tool to help programmers avoid race n n conditions

Critical Section n A conceptual tool to help programmers avoid race n n conditions A section of code where shared memory/resources (variables, files, tables, etc. ) are modified At most one cooperating process can be in the critical region at any given point in time Processes needs to follow a protocol when they modify shared resources – Critical Section Problem l Process need to request permission before they enter critical region Program structured as “Entry Section”, Critical Section”, “Exit Section” and “Remainder Section” Operating System Concepts with Java – 8 th Edition 5. 13 Silberschatz, Galvin and Gagne © 2009

Structure of a Typical Process Operating System Concepts with Java – 8 th Edition

Structure of a Typical Process Operating System Concepts with Java – 8 th Edition 5. 14 Silberschatz, Galvin and Gagne © 2009

Critical Section Solution Requirements 1. Mutual Exclusion - If process Pi is executing in

Critical Section Solution Requirements 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Operating System Concepts with Java – 8 th Edition 5. 15 Silberschatz, Galvin and Gagne © 2009

Critical Section Solution Assumptions Assume that each process executes at a nonzero speed No

Critical Section Solution Assumptions Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes Operating System Concepts with Java – 8 th Edition 5. 16 Silberschatz, Galvin and Gagne © 2009

Peterson’s Solution n Two process solution n Theoretical solution – May not work correctly

Peterson’s Solution n Two process solution n Theoretical solution – May not work correctly on modern n n architectures Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: l int turn; l boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready! Operating System Concepts with Java – 8 th Edition 5. 17 Silberschatz, Galvin and Gagne © 2009

Algorithm for Process Pi while (true) { flag[i] = true; turn = j; while

Algorithm for Process Pi while (true) { flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } Operating System Concepts with Java – 8 th Edition 5. 18 Silberschatz, Galvin and Gagne © 2009

Peterson’s Solution – Points to Note n Each process plays the “nice guy” n

Peterson’s Solution – Points to Note n Each process plays the “nice guy” n Asserts it is the other processes turn to enter into the critical section n The infinite wait loop is broken if at least one of the following holds The other process is not interested in entering the critical region l The other process’s write on the “turn” variable survived (i. e. , turn has been set to this process) l Operating System Concepts with Java – 8 th Edition 5. 19 Silberschatz, Galvin and Gagne © 2009

Peterson’s Solution – Correctness proof n Mutual exclusion Impossible for both processes to break

Peterson’s Solution – Correctness proof n Mutual exclusion Impossible for both processes to break the while loop simultaneously – Why? n Progress and bounded wait l l l Process Pi can be stuck in the wait loop only if turn == j and flag[j] == true If both conditions hold -- Pj wants to be in critical region and it has necessary permission to do so When Pj exits the critical section it sets flag[j] to false Pi enters critical region after at most one entry by Pj When Pj is in the remainder region it has no effect on Pi’s entry into critical region Operating System Concepts with Java – 8 th Edition 5. 20 Silberschatz, Galvin and Gagne © 2009

Locks – A Generic Hardware Paradigm n Critical regions protected by locks n Processes

Locks – A Generic Hardware Paradigm n Critical regions protected by locks n Processes need to acquire lock before entering CR n Acquiring and releasing locks are atomic operations while (true) { acquire lock critical section release lock remainder section } Operating System Concepts with Java – 8 th Edition 5. 21 Silberschatz, Galvin and Gagne © 2009

Synchronization Hardware n Modern machines provide special atomic hardware instructions l Atomic mean non-interruptable

Synchronization Hardware n Modern machines provide special atomic hardware instructions l Atomic mean non-interruptable (i. e. , the instruction executes as one unit) n get. And. Set() -- Test memory word and set its value n swap() – exchange the contents of two memory words Operating System Concepts with Java – 8 th Edition 5. 22 Silberschatz, Galvin and Gagne © 2009

Illustration of get. And. Set and swap Operating System Concepts with Java – 8

Illustration of get. And. Set and swap Operating System Concepts with Java – 8 th Edition 5. 23 Silberschatz, Galvin and Gagne © 2009

Solution using Get. And. Set Instruction Operating System Concepts with Java – 8 th

Solution using Get. And. Set Instruction Operating System Concepts with Java – 8 th Edition 5. 24 Silberschatz, Galvin and Gagne © 2009

Solution using Swap Instruction Operating System Concepts with Java – 8 th Edition 5.

Solution using Swap Instruction Operating System Concepts with Java – 8 th Edition 5. 25 Silberschatz, Galvin and Gagne © 2009

Semaphore n Synchronization tool for programmers n Semaphore S – integer variable n Two

Semaphore n Synchronization tool for programmers n Semaphore S – integer variable n Two standard operations modify S: acquire() and release() l Originally called P() (proberen) and V() (verhogen) n Can only be accessed only via the above atomic operations Operating System Concepts with Java – 8 th Edition 5. 26 Silberschatz, Galvin and Gagne © 2009

Semaphore for Mutual Exclusion n Binary semaphore – integer value can range only between

Semaphore for Mutual Exclusion n Binary semaphore – integer value can range only between 0 and 1 l Also known as mutex locks Operating System Concepts with Java – 8 th Edition 5. 27 Silberschatz, Galvin and Gagne © 2009

Java Example Using Semaphores Operating System Concepts with Java – 8 th Edition 5.

Java Example Using Semaphores Operating System Concepts with Java – 8 th Edition 5. 28 Silberschatz, Galvin and Gagne © 2009

Java Example Using Semaphores Operating System Concepts with Java – 8 th Edition 5.

Java Example Using Semaphores Operating System Concepts with Java – 8 th Edition 5. 29 Silberschatz, Galvin and Gagne © 2009

Semaphore for Imposing Order n P 1 and P 2 are concurrently running processes

Semaphore for Imposing Order n P 1 and P 2 are concurrently running processes n S 1– Statement in process P 1; S 2 – Statement in Process P 2 n Ensure that S 2 gets executed only after S 1 Initialize semaphore synch to 0 Process P 1: Process P 2: S 1; synch. release(); synch. acquire(); S 2; Operating System Concepts with Java – 8 th Edition 5. 30 Silberschatz, Galvin and Gagne © 2009

Counting Semaphores n Counting semaphore – integer value can range over an unrestricted domain

Counting Semaphores n Counting semaphore – integer value can range over an unrestricted domain n Controlling access to a resource with finite number of instances n Initialize semaphore to # of available instances n A process wanting to access resource will do an acquire on the semaphore n Processes will do a release on the semaphore after using the resource n Processes will wait if all available resources are currently being used Operating System Concepts with Java – 8 th Edition 5. 31 Silberschatz, Galvin and Gagne © 2009

Semaphore Implementation n Must guarantee that no two processes can execute acquire () and

Semaphore Implementation n Must guarantee that no two processes can execute acquire () and release () on the same semaphore at the same time n Disadvantage: Required busy waiting l Processes continually wait in the entry code l Spinlock – another name for this type of semaphore l Wastes CPU cycles Operating System Concepts with Java – 8 th Edition 5. 32 Silberschatz, Galvin and Gagne © 2009