



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Quiz; Class: Operating Systems Design; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;
Typology: Quizzes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




CS423UG Operating Systems
CS 423UG - Operating Systems, Indranil Gupta 2
â˘x initially is == 1 â˘x is shared between the two processes
Producer process Consumer process
CS 423UG - Operating Systems, Indranil Gupta 3
CS 423UG - Operating Systems, Indranil Gupta 4
CS 423UG - Operating Systems, Indranil Gupta 5
CS 423UG - Operating Systems, Indranil Gupta 6
CS 423UG - Operating Systems, Indranil Gupta 7
What can Go Wrong?
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
Step1: put the card on the table Step2: increment the counter
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
Examples
CS 423UG - Operating Systems, Indranil Gupta 11
Spooling Example: Correct
Shared memory (queue)
abc Prog.c Prog.n
âŚ
⌠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+
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+
4 in=next_free+
5
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
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âŚ
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âŚ
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:
Solution: sleep and wakeup (syscall
supported)
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'sQUIZ 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 &
MP