Process Synchronization - Operating Systems Design - Slides | CS 423, Quizzes of Operating Systems

Material Type: Quiz; Class: Operating Systems Design; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;

Typology: Quizzes

Pre 2010

Uploaded on 03/16/2009

koofers-user-sth
koofers-user-sth 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Process Synchronization
Indranil Gupta
Lecture 8
Sep 12, 2005
CS423UG Operating Systems
CS 423UG -Operating Systems, Indranil Gupta 2
A Small Puzzle
x:=x+1; x:=x-1
•x initially is == 1
•x is shared between the two processes
•What is the value of x after both processes complete?
Producer process Consumer process
CS 423UG -Operating Systems, Indranil Gupta 3
Agenda
Data Races
Critical region and mutual exclusion
Mutual exclusion using busy waiting
Disabling Interrupts
Lock Variables
Strict Alternation
Peterson’s solution
TSL
Sleep and Wakeup
Quiz Discussion
CS 423UG -Operating Systems, Indranil Gupta 4
A few things we know now…
1. “Process”: a program in execution
2. “Thread”: a light weight process
3. “Scheduling”:
Which process/thread (from ready queu e)
should run?
CS 423UG -Operating Systems, Indranil Gupta 5
Inter-Process Communication (IPC)
Communication
Processes pass information to each other
Mutual exclusion & Synchronization
Correct coordination among processes
Also applies to threads
“Correct Coordination”?! What’s the
problem, exactly?
CS 423UG -Operating Systems, Indranil Gupta 6
Data 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 shoul d stop
when
pf3
pf4
pf5

Partial preview of the text

Download Process Synchronization - Operating Systems Design - Slides | CS 423 and more Quizzes Operating Systems in PDF only on Docsity!

Process Synchronization

Indranil Gupta

Lecture 8

Sep 12, 2005

CS423UG Operating Systems

CS 423UG - Operating Systems, Indranil Gupta 2

A Small Puzzle

 x:=x+1;  x:=x-

•x initially is == 1 •x is shared between the two processes

  • What is the value of x after both processes complete?

Producer process Consumer process

CS 423UG - Operating Systems, Indranil Gupta 3

Agenda

Data Races

Critical region and mutual exclusion

Mutual exclusion using busy waiting

Disabling Interrupts

Lock Variables

Strict Alternation

Peterson’s solution

TSL

Sleep and Wakeup

Quiz Discussion

CS 423UG - Operating Systems, Indranil Gupta 4

A few things we know now…

1. “Process”: a program in execution

2. “Thread”: a light weight process

3. “Scheduling”:

 Which process/thread (from ready queue)

should run?

CS 423UG - Operating Systems, Indranil Gupta 5

Inter-Process Communication (IPC)

Communication

Processes pass information to each other

Mutual exclusion & Synchronization

Correct coordination among processes

Also applies to threads

“Correct Coordination”?! What’s the

problem, exactly?

CS 423UG - Operating Systems, Indranil Gupta 6

Data 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 step

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

CS 423UG - Operating Systems, Indranil Gupta 7

What 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 step  Step2b: if the counter is nonzero, take a card from the table  Step3: decrement the counter

Context switch

Consumer gets stuck in Step 2b! CS 423UG - Operating Systems, Indranil Gupta 8

Let’s Reorder Producer’s 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 step  Step2b: if the counter is nonzero, take a card from the table  Step3: decrement the counter

What could go wrong with this example?

CS 423UG - Operating Systems, Indranil Gupta 9

“Increment/Decrement the Counter”

 x:=x+1; compiles to

Load Reg, x

Inc Reg

Save x, Reg

 x:=x-1; compiles to

Load Reg, x

Dec Reg

If the initial value of x was 1, after Save x, Reg this intervleaving, it will be 0 (should have been left as 1)

Producer Consumer

Suppose the counter is at memory address “x”

CS 423UG - Operating Systems, Indranil Gupta 10

Those 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

CS 423UG - Operating Systems, Indranil Gupta 11

Spooling Example: Correct

Shared memory (queue)

abc Prog.c Prog.n

F

…

… out

in

Process 1 Process 2

next_free = in;

next_free = in

int next_free;

Store F1 into next_free;

Store F2 into next_free;

int next_free;

in=next_free+

F

3 in=next_free+

CS 423UG - Operating Systems, Indranil Gupta 12

Spooling Example: Wrong (Race)

Shared memory

abc Prog.c Prog.n

…

… out

in

Process 1

next_free = in;

next_free = in /* value: 7 */

int next_free;

Stores F1 into next_free;

Stores F2 into next_free;

in=next_free+

F

4 in=next_free+

5

F

Process 2 int next_free;

CS 423UG - Operating Systems, Indranil Gupta 19

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

CS 423UG - Operating Systems, Indranil Gupta 20

IIIb. Using Flags

int flag[2]= {false, false};

thread me;

while (true)

flag[my_thread_id] = true;

while (flag[other_thread_id] ) { };

Access shared variables; // Critical Section;

flag[my_thread_id] = false;

Do other work

No Progress. Could block indefinitely. Why?

CS 423UG - Operating Systems, Indranil Gupta 21

IV. Peterson’s Solution int flag[2]={false, false}; // shared array int turn; // shared variable { while (true) { flag[my_thread_id] = true; turn = other_thread_id; while (flag[other_thread_id] and turn == other_thread_id ) { }; Access shared variables; // Critical Section flag[my_thread_id] = false; Do other work } }

This works!!! Why?

Questions to ask: What happens when…

  1. One process tries to enter, and the other process tries to enter later?
  2. Both processes try to enter simultaneously?

EXIT

ENTER

CS 423UG - Operating Systems, Indranil Gupta 22

V. 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 { Rx=*target; *target = 1; }

TSL Rx, target

CS 423UG - Operating Systems, Indranil Gupta 23

Using the TSL

instruction

Does this work? Questions to ask: What happens when…

  1. One process tries to enter, and the other process tries to enter later?
  2. Both processes try to enter simultaneously?

CS 423UG - Operating Systems, Indranil Gupta 24

Other Similar Hardware Instructions

swap

void swap (char* x,* y);

//*y==1. 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

CS 423UG - Operating Systems, Indranil Gupta 25

VI. 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 CS 423UG - Operating Systems, Indranil Gupta^26

  1. In a computer, there are 20 ready'' processes, 5blocked''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 IMMEDIATELYafterward? 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? A. A Firefox browser. B. A Command shell. C. A File. D. Acrobat PDF reader.
  5. Which of the following is common to two different user-level threads that belong to the same process? A. Program Counter. B. Stack. C. Address Space. D. None of the above.
    1. 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.
    2. Consider a system where no users are logged in and whereno 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.
    3. One of the 3 threads in a process calls fork(). When thesystem call returns successfully, how many NEW threads does the system contain? All threads are kernel-levelthreads (assume Solaris-style threads). A. 6 B. 3 C. 1 D. None of the above.
    4. Which of the following metrics is the LEAST important for batch systems? A. Turnaround Time B. Response Time C. Waiting Time D. Efficiency
    5. Which of the following processes is typically the MOST I/O- bound? A. Internet Explorer Browser (text pages) B. Unix Command shell C. Emacs editor window D. Windows Media Player (MPEG player)

QUIZ 1 Solutions

CS 423UG - Operating Systems, Indranil Gupta 27

Reminder

Reading for this lecture was: Sections

2.3.0-2.3.

Reading for next lecture: Sections 2.3 &

More useful solutions to synchronization

MP