Title: Module 7: Process Synchronization
1Operating SystemsLecture 24 Critical Regions
2Input 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
3Sample 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 . . .
4Things 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).
5Problems 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.
6Semaphore Errors
- Interchange signal and wait.
- signal(mutex)
- critical section
- wait(mutex)
- What happens?
- Replace signal with wait
- wait(mutex)
- critical section
- wait(mutex)
- What happens?
7More Semaphore Errors
3. omit the wait. ... critical
section signal(mutex) What happens? 4. Omit
the signal wait(mutex) critical
section ... What happens?
8Critical 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.
9Using 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 Swhere B is a boolean
expression. - S may be a compound statement.
- While statement S is being executed, no other
process can access variable v.
10Notes 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.
11Example Bounded Buffer
- Shared data
- struct buffer
- int pooln
- int count, in, out
-
- Producer process inserts nextp into the shared
buffer - region buffer when( count lt n) poolin
nextp in (in1) n count
12Bounded Buffer Consumer Process
- Consumer process removes an item from the shared
buffer and puts it in nextc - region buffer when (count gt 0) nextc
poolout out (out1) n count--
13Implementation 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.
14Monitors
- 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
-
-
15Monitors
- 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.
16Schematic View of a Monitor
17Monitor With Condition Variables