Title: Process Synchronization
1Process Synchronization
CS423UG Operating Systems
- Indranil Gupta
- Lecture 8
- Sep 12, 2005
2A Small Puzzle
Consumer process
Producer process
- x initially is 1
- x is shared between the two processes
- What is the value of x after both processes
complete?
3Agenda
- Data Races
- Critical region and mutual exclusion
- Mutual exclusion using busy waiting
- Disabling Interrupts
- Lock Variables
- Strict Alternation
- Petersons solution
- TSL
- Sleep and Wakeup
- Quiz Discussion
4A few things we know now
- Process a program in execution
- Thread a light weight process
- Scheduling
- Which process/thread (from ready queue) should
run?
5Inter-Process Communication (IPC)
- Communication
- Processes pass information to each other
- Mutual exclusion Synchronization
- Correct coordination among processes
- Also applies to threads
- Correct Coordination?! Whats the problem,
exactly?
6Data Races A simple game
- Two volunteers
- Producer produce 1 card per iteration
- Step1 increment a (shared) counter
- Step2 put a card on the table
- Consumer
- Step1 check the counter to see if it is zero
- Step2a if the counter is zero, go back to step1
- Step2b if the counter is nonzero, take a card
from the table - Step3 decrement counter
- I am the OS, and I schedule
- I decide who should run when, who should stop when
7What can Go Wrong?
- Scheduler Stops Producer before Step 2 and let
Consumer run. - What happens?
- Two volunteers
- Producer produce 1 card per iteration
- Step1 increment the counter
- Step2 put the card on the table
- Consumer
- Step1 check the counter to see if it is zero
- Step2a if the counter is zero, go back to step1
- Step2b if the counter is nonzero, take a card
from the table - Step3 decrement the counter
Context switch
Consumer gets stuck in Step 2b!
8Lets Reorder Producers Instructions
- Scheduler Stops Producer before Step 2 and let
Consumer run. - What happens?
- Two volunteers
- Producer produce 1 card per iteration
- Step1 put the card on the table
- Step2 increment the counter
- Consumer
- Step1 check the counter to see if it is zero
- Step2a if the counter is zero, go back to step1
- Step2b if the counter is nonzero, take a card
from the table - Step3 decrement the counter
What could go wrong with this example?
9Increment/Decrement the Counter
Suppose the counter is at memory address x
Consumer
Producer
- xx1 compiles to
- Load Reg, x
- Inc Reg
- Save x, Reg
- xx-1 compiles to
- Load Reg, x
- Dec Reg
- Save x, Reg
If the initial value of x was 1, after this
intervleaving, it will be 0 (should have been
left as 1)
10Those were Examples of Data Races
- Reason data sharing
- Previous game producer and consumer
- Share the counter
- Share the cards
- Examples
- Thread two threads spawned within Apache web
server both try to post (http) to a common
webpage - Process two processes both try to print by
accessing a queue with in and out variables
11Spooling Example Correct
Shared memory (queue)
Process 2
Process 1
int next_free
int next_free
out
next_free in
1
abc
4
Store F1 into next_free
Prog.c
5
2
Prog.n
6
innext_free1
in
3
F1
7
next_free in
4
F2
Store F2 into next_free
5
innext_free1
6
12Spooling Example Wrong (Race)
Shared memory
Process 2
Process 1
int next_free
int next_free
out
next_free in
1
abc
4
Prog.c
next_free in / value 7 /
5
2
Stores F1 into next_free
Prog.n
6
3
in
F1
7
F2
innext_free1
4
Stores F2 into next_free
5
innext_free1
6
13Introducing the Critical Section
- Process
- while (true)
- ENTER CRITICAL SECTION
- Access shared variables // Critical Section
- LEAVE CRITICAL SECTION
- Do other work
-
-
// also critical region
- For previous spool example, critical section
could be - access in, queue your job, inc in
14Critical Section Requirements
- Mutual Exclusion No other process must execute
within the critical section while a process is in
it. - Progress If no process is waiting in its
critical section and several processes are trying
to get into their critical section, then entry to
the critical section cannot be postponed
indefinitely. - Bounded Wait A process requesting entry to a
critical section should only have to wait for a
bounded number of other processes to enter and
leave the critical section. - Speed and Number of CPUs No assumption can be
made about speeds or number of CPUs. - No process running outside a critical section can
block other processes.
15Critical Section Timeline - Example
16Simplest Ways of Implementing Critical Sections
Busy Waiting
- Possible Solutions
- Disabling Interrupts
- Lock Variables
- Strict Alternation
- Petersons solution
- TSL
- Sleep and Wakeup
17I. Disabling Interrupts
- How does it work?
- Disable all interrupts just after entering a
critical section, and re-enable them just before
leaving it. - Why does it work?
- With interrupts disabled, no clock interrupts can
occur. (No clock interrupts means scheduler does
not run, and process cannot be preempted.) - Problems
- What if the process forgets to re-enable the
interrupts? - Bug in code, or malicious code
- Multiprocessor? (disabling interrupts only
affects one CPU) - Works, but safe to use only inside OS/kernel
18II. Busy Wait on Lock Variables
- while (lock!0)
-
- lock 1
- EnterCriticalSection
- access shared variables
- LeaveCriticalSection
- lock 0
- What happens if two processes
- simultaneously detect lock0, and
- both exit while loop before either sets
- lock1?
- Busy wait on locks may violate
- mutual exclusion requirement!
19III. Strict Alternation
- thread me / For two threads /
-
- while (true)
-
- while ( turn ! my_thread_id)
- Access shared variables // Critical
Section - turn other_thread_id
- Do other work
-
-
- Satisfies Mutual Exclusion but not Progress.
- Why?
20IIIb. Using Flags
- int flag2 false, false
- thread me
-
- while (true)
-
- flagmy_thread_id true
- while (flagother_thread_id )
- Access shared variables // Critical
Section - flagmy_thread_id false
- Do other work
-
-
- No Progress. Could block indefinitely. Why?
21IV. Petersons Solution
- int flag2false, false // shared array
- int turn // shared variable
-
- while (true)
-
- flagmy_thread_id true
- turn other_thread_id
- while (flagother_thread_id and turn
other_thread_id ) - Access shared variables // Critical
Section - flagmy_thread_id false
- Do other work
-
-
- This works!!! Why?
ENTER
EXIT
- Questions to ask What happens when
- One process tries to enter, and the
- other process tries to enter later?
- Both processes try to enter
- simultaneously?
22V. Test Set (TSL)
- Requires hardware support
- Atomically tests if a variable is 0, and sets it
to 1 (one instruction, not separable, i.e.,
atomic) - char test_and_set (Rx, char target)
- // All done atomically
-
- Rxtarget
- target 1
-
TSL Rx, target
23Using the TSL instruction
- Does this work?
- Questions to ask What happens when
- One process tries to enter, and the
- other process tries to enter later?
- Both processes try to enter
- simultaneously?
24Other Similar Hardware Instructions
- swap
- void swap (char x, y)
- //y1. All done atomically
- char temp x
- x y
- y temp
-
- Also
- Intel x86 XCHG (exchange) instruction
- SPARC CASA and CASXA instructions
Similar to TSL, but with deals with memory
25VI. Sleep and Wakeup
- Problems with previous solutions
- Busy waiting is wasteful
- Wastes CPU resources and time
- Could cause Priority Inversion (aha, Mars
Pathinder problem!) - a high priority process waits for a low priority
process to leave the critical section - the low priority process can never execute since
the high priority process is not blocked - Solution sleep and wakeup (syscall supported)
- When blocked, go to sleep
- Wakeup when it is OK to retry entering the
critical section
26- 6. Which of the following will IMMEDIATELY change
the PCB of a process? - A. The CPU context switches the process out from
the CPU. - B. The process executes an instruction.
- C. The process initiates a system call.
- D. The CPU, while executing the process, receives
an interrupt from the disk device for a request
by another process. - 7. Consider a system where no users are logged in
and where no useful processes have been started
up by the OS. What is the CPU doing? - A. The CPU may be sleeping.
- B. The CPU is busy-waiting (a while(1) loop).
- C. The CPU is running a dummy OS process that
does nothing useful. - D. Any of the above.
- 8. One of the 3 threads in a process calls
fork(). When the system call returns
successfully, how many NEW threads does the
system contain ? All threads are kernel-level
threads (assume Solaris-style threads). - A. 6
- B. 3
- C. 1
- D. None of the above.
- 9. Which of the following metrics is the LEAST
important for batch systems?
QUIZ 1 Solutions
- 1. In a computer, there are 20 ready''
processes, 5 blocked'' processes and 4
running'' processes. The computer has - A. 1 CPU
- B. 2 CPU's
- C. 3 CPU's
- D. 4 or more CPU's
- 2. Which of the following instructions should be
privileged (checked thoroughly before allowing a
process to execute)? - A. Read value of timer
- B. Read the clock
- C. Clear the memory
- D. Yield the CPU to scheduler
- 3. When a CPU divides by zero, what happens
IMMEDIATELY afterward? - A. The computer crashes.
- B. A TRAP is executed.
- C. The CPU kills the process.
- D. None of the above.
- 4. Which of the following is NOT a process?
27Reminder
- Reading for this lecture was Sections
2.3.0-2.3.4 - Reading for next lecture Sections 2.3 2.4
- More useful solutions to synchronization
- MP1