Download presentation
Presentation is loading. Please wait.
Published byJonah Sparks Modified over 9 years ago
1
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.1 Operating System Concepts Operating Systems Lecture 24 Critical Regions
2
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.2 Operating System Concepts Input Format First line of file: number_processes switch_time Next lines of file: Set of lines for each process: First line: process_num arrival_time num_CPU_bursts Next lines: burst_number CPU_time IO_time Last line: burst_number CPU_time Repeated for each process in input file
3
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.3 Operating System Concepts Sample Input INPUT MEANING 4 5 4 processes, switch_time = 5 1 0 6 Process 1, arrival time 0, 6 bursts 1 15 400 Burst 1, CPU time 15, IO time 400 2 18 200 Burst 2, CPU time 18, IO time 200 3 15 100 4 14 400 5 25 100 6 240 Last burst, no I/O 2 12 4 Process 2, arrival time 12, 4 bursts 1 4 150 2 30 50 3 90 75 4 15...
4
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.4 Operating System Concepts Things to think about What data structure(s) will you use to store the process information? What parameters need to be passed to the function stubs that will run the schedule simulations? Event Queue: event.cc allows you to get next event and update the time. You can also insert new events in the correct order. How will you build the initial event queue from the process information? Look at event.h, list.h. (We will discuss this more later).
5
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.5 Operating System Concepts Problems with Semaphores If semaphores are used incorrectly in the program it can lead to timing errors. These errors can be difficult to detect and correct, because they occur only occasionally and only under certain circumstances. E.g. In the bounded buffer problem, the counter value would be incorrect only if two processes happened to access it at the same time. The counter would appear to have a reasonable value, so the error could go undetected.
6
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.6 Operating System Concepts Semaphore Errors 1. Interchange signal and wait. signal(mutex); critical section wait(mutex); What happens? 2. Replace signal with wait: wait(mutex); critical section wait(mutex); What happens?
7
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.7 Operating System Concepts More Semaphore Errors 3.omit the wait.... critical section signal(mutex); What happens? 4.Omit the signal: wait(mutex); critical section... What happens?
8
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.8 Operating System Concepts Critical Regions Critical Regions are a high level synchronization construct. We assume that a process consists of local data and a sequential program that operates on the data. Only the process that owns the local data can access that data. Multiple processes can access global data through the critical region construct.
9
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.9 Operating System Concepts Using a Critical Region To use a critical region, declare a shared variable. A shared variable v of type T, is declared as: v: shared T Variable v accessed only inside statement region v when B do S where B is a boolean expression. S may be a compound statement. While statement S is being executed, no other process can access variable v.
10
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.10 Operating System Concepts Notes on Critical Regions Regions referring to the same shared variable exclude each other in time. When a process tries to execute the region statement: the Boolean expression B is evaluated. If B is true, statement S is executed. If B is false, the process is delayed until B becomes true and no other process is in the region associated with v.
11
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.11 Operating System Concepts Example – Bounded Buffer Shared data: struct buffer { int pool[n]; int count, in, out; } Producer process inserts nextp into the shared buffer: region buffer when( count < n) { pool[in] = nextp; in:= (in+1) % n; count++; }
12
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.12 Operating System Concepts Bounded Buffer Consumer Process Consumer process removes an item from the shared buffer and puts it in nextc region buffer when (count > 0) { nextc = pool[out]; out = (out+1) % n; count--; }
13
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.13 Operating System Concepts Implementation of Critical Region The Critical Region is implemented with semaphores. If a process cannot enter the critical section because the Boolean expression B is false, it waits through two delays before it is allowed to reevaluate B. Mutually exclusive access to the critical section is provided by mutex. Two other semaphores are used to implement the delays.
14
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.14 Operating System Concepts Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name { shared variable declarations procedure body P1 (…) {... } procedure body P2 (…) {... } procedure body Pn (…) {... } { initialization code }
15
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.15 Operating System Concepts Monitors Only one process at a time can be active within the monitor. The programmer does not need to code the synchronization explicitly. To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; Condition variable can only be used with the operations wait and signal. The operation x.wait(); means that the process invoking this operation is suspended until another process invokes x.signal(); The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.
16
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.16 Operating System Concepts Schematic View of a Monitor
17
Silberschatz, Galvin and Gagne 2002 Modified for CSCI 399, Royden, 2005 7.17 Operating System Concepts Monitor With Condition Variables
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.