DOC PREVIEW
U of I CS 241 - Process Synchronization (1)

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 System Programming Process Synchronization (1)ContentAdministrativeReviewInter-Process Communication (IPC)A simple gameFirst RoundSecond RoundData RacesSpooling Example: CorrectSpooling Example: RacesCritical Region (Critical Section)Critical Region RequirementCritical Regions (2)Mutual Exclusion With Busy WaitingDisabling InterruptsLock VariablesSolution HistoryTurn Mutual Exclusion (Strict Alteration)Turn Mutual Exclusion (Strict Alteration)Other Flag Mutual ExclusionSlide 222 Flag Mutual ExclusionSlide 24Slide 252 Flag and Turn Mutual Exclusion (Peterson Solution)Slide 27SummaryCS241 System Programming Process Synchronization (1)Klara NahrstedtLecture 62/1/200601/14/19 CS 241 - System Programming, Klara Nahrstedt2Content Data Races–A simple gameCritical region and mutual exclusionMutual exclusion using busy waiting–Disabling Interrupts–Lock Variables–Strict Alternation–Peterson’s solutionSummary01/14/19 CS 241 - System Programming, Klara Nahrstedt3AdministrativeRead : T:2.2 on Process SynchronizationRead: R&R 13 and 14MP1 posted – deadline February 1301/14/19 CS 241 - System Programming, Klara Nahrstedt4ReviewProcess: a program in executionThreads: a light weight process01/14/19 CS 241 - System Programming, Klara Nahrstedt5Inter-Process Communication (IPC)Communication–Pass information to each otherMutual exclusion & Synchronization–Proper sequencing The last one also applies to threads01/14/19 CS 241 - System Programming, Klara Nahrstedt6A simple game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 counterI am the OS–I decide who should go, who should stop01/14/19 CS 241 - System Programming, Klara Nahrstedt7First RoundStop Producer before step2 and let Consumer go.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 switch01/14/19 CS 241 - System Programming, Klara Nahrstedt8Second RoundStop Producer before step2 and let Consumer go.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 counter01/14/19 CS 241 - System Programming, Klara Nahrstedt9Data RacesReason: data sharingPrevious game: producer and consumer–Share the counter–Share the cards01/14/19 CS 241 - System Programming, Klara Nahrstedt10Spooling Example: CorrectShared memoryabcProg.cProg.n4567F2……outinProcess 1Process 2next_free = in;next_free = inint next_free;Stores F1 into next_free;Stores F2 into next_free;int next_free;124in=next_free+1F1in=next_free+135601/14/19 CS 241 - System Programming, Klara Nahrstedt11Spooling Example: RacesShared memoryabcProg.cProg.n4567……outinProcess 1Process 2next_free = in;next_free = in/* value: 7 */int next_free;Stores F1 into next_free;Stores F2 into next_free;int next_free;132in=next_free+1F1in=next_free+1456F201/14/19 CS 241 - System Programming, Klara Nahrstedt12Critical Region (Critical Section)Process { while (true) { ENTER CRITICAL SECTION Access shared variables; // Critical Section; LEAVE CRITICAL SECTION Do other work } }01/14/19 CS 241 - System Programming, Klara Nahrstedt13Critical Region Requirement–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 may be made about speeds or number of CPUs. 01/14/19 CS 241 - System Programming, Klara Nahrstedt14Critical Regions (2)Mutual exclusion using critical regions01/14/19 CS 241 - System Programming, Klara Nahrstedt15Mutual Exclusion With Busy WaitingPossible Solutions–Disabling Interrupts–Lock Variables–Strict Alternation–Peterson’s solution–TSL01/14/19 CS 241 - System Programming, Klara Nahrstedt16Disabling 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. (The CPU is only switched from one process to another as a result of clock or other interrupts, and with interrupts disabled, no switching can occur.) Problems:–What if the process forgets to enable the interrupts?–Multiprocessor? (disabling interrupts only affects one CPU)Only used inside OS01/14/19 CS 241 - System Programming, Klara Nahrstedt17Lock VariablesWhile (lock);lock = 1;EnterCriticalSection; access shared variable;LeaveCriticalSection;lock = 0;Does the above code work?01/14/19 CS 241 - System Programming, Klara Nahrstedt18 Solution HistoryApproaches:1. Turn Mutual Exclusion2. Other Flag Mutual Exclusion3. Two Flag Mutual Exclusion4. Two Flag and Turn Mutual Exclusion01/14/19 CS 241 - System Programming, Klara Nahrstedt19Turn Mutual Exclusion(Strict Alteration)Process; /* For two processes */{ while (true) { while ( turn != my_process_id) { }; Access shared variables; // Critical Section; turn = other_process_id; Do other work } }mutual exclusion a) yes, b) noWhat Properties does this satisfy?01/14/19 CS 241 - System Programming, Klara Nahrstedt20Turn Mutual Exclusion (Strict Alteration)Process; /* For two processes */{ while (true) { while ( turn != my_process_id) { }; Access shared variables; // Critical Section; turn = other_process_id; Do other work } }Progress: a) yes, b) noWhat Properties does this satisfy?01/14/19 CS 241 -


View Full Document

U of I CS 241 - Process Synchronization (1)

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Process Synchronization (1)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Process Synchronization (1) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Process Synchronization (1) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?