1 / 37

Introduction to Synchronization

Klara Nahrstedt Lecture 10 2/7/07. Introduction to Synchronization. CS241 Administrative. This week SMP2, Deadline Monday 9am, 2/12/07 Regular Quiz 3 on Friday, 2/9/07 Reminder: Read Stallings Chapter 5 and R & R Chapter 13 and 14. Overview. Introduction to synchronization

gail
Download Presentation

Introduction to Synchronization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Klara Nahrstedt Lecture 10 2/7/07 Introduction to Synchronization

  2. CS241 Administrative This week SMP2, Deadline Monday 9am, 2/12/07 Regular Quiz 3 on Friday, 2/9/07 Reminder: Read Stallings Chapter 5 and R & R Chapter 13 and 14

  3. Overview Introduction to synchronization Why do we need synchronization? What is a data race? Critical region, mutual exclusion, progress, bounded waiting

  4. Inter-Process Communication (IPC) IPC needed in multiprogramming – management of multiple processes within a uni-processor system multiprocessing – management of multiple processes within a multi-processor system Pass information to each other via shared resource Mutual exclusion & Synchronization Proper sequencing

  5. Spooling Example: Correct Shared memory Process 1 Process 2 int next_free; int next_free; … out next_free = in; 1 4 abc Stores F1 into next_free; Prog.c 2 5 Prog.n 6 in=next_free+1 3 in F1 7 next_free = in 4 F2 Stores F2 into next_free; 5 … in=next_free+1 6

  6. Spooling Example: Data Races Shared memory Process 1 Process 2 int next_free; int next_free; out … next_free = in; 1 4 abc Prog.c next_free = in /* value: 7 */ 2 5 Stores F1 into next_free; Prog.n 3 6 in F1 7 F2 in=next_free+1 4 Stores F2 into next_free; 5 … in=next_free+1 6

  7. Critical Region (Critical Section) Process { while (true) { ENTER CRITICAL SECTION Access shared variables; // Critical Section; LEAVE CRITICAL SECTION Do other work } }

  8. Critical Region Requirements Mutual Exclusion Progress Bounded Wait No Speed and Number of CPU assumptions

  9. Critical Region Requirements 1 of 2 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.

  10. Critical Region Requirements • Mutual Exclusion • Progress • 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.

  11. Progress Bounded Wait Mutual Exclusion Good concrete example... Oversimplifying Assumptions

  12. visual learning aid: ‘bmp’ in alphabetical order Progress Bounded Wait Mutual Exclusion Oversimplifying Assumptions

  13. Progress Bounded Wait Mutual Exclusion Oversimplifying Assumptions

  14. Progress Bounded Wait Mutual Exclusion No cutting in! Oversimplifying Assumptions

  15. Progress Bounded Wait Mutual Exclusion Are there door locks? No cutting in! Oversimplifying Assumptions

  16. Progress Bounded Wait Mutual Exclusion Are there door locks? No cutting in! We'll be first if we sprint Oversimplifying Assumptions

  17. Progress Bounded Wait Mutual Exclusion Well, Did you see anybody go in? Are there door locks? No cutting in! We'll be first if we sprint Oversimplifying Assumptions

  18. Bounded Wait? • Progress? What's the difference?

  19. 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

  20. 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. • 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

  21. Critical Regions (2) Mutual exclusion using critical regions

  22. Mutual Exclusion With Busy Waiting Possible Solutions Software-only candidate solutions (Two-Process Solutions) Lock Variables Turn Mutual Exclusion Other Flag Mutual Exclusion Two Flag Mutual Exclusion Two Flag and Turn Mutual Exclusion Hardware solutions Disabling Interrupts; Test-and-set; Swap (Exchange) Semaphores

  23. Lock Variables while (lock) {/* spin spin spin spin */} lock = 1; /* EnterCriticalSection; */ access shared variable; /* LeaveCriticalSection; */ lock = 0; What's the problem?

  24. Turn-based Mutual Exclusionwith strict alternation ProcessMe/* For two processes */ { while (true) { while ( turn != my_process_id) { } access shared variables turn = other_process_id do other work } }

  25. Turn-based Mutual Exclusionwith strict alternation Satisfies Mutual Exclusion? ProcessMe/* For two processes */ { while (true) { while ( turn != my_process_id) { } access shared variables turn = other_process_id do other work } }

  26. Turn-based Mutual Exclusion with strict alternation Satisfies Bounded Waiting? ProcessMe/* For two processes */ { while (true) { while ( turn != my_process_id) { } access shared variables turn = other_process_id do other work } }

  27. Turn-based Mutual Exclusion with strict alternation Satisfies Progress? ProcessMe /* For two processes */ { while (true) { while ( turn != my_process_id) { } access shared variables turn = other_process_id do other work } } z

  28. Other Flag Mutual Exclusion int owner[2] = {false, false} ProcessMe { while (true) { while (owner[other_process_id] ) { } owner[my_process_id] = true access shared variables owner[my_process_id] = false do other work } } Progress?

  29. Other Flag Mutual Exclusion int owner[2] = {false, false} ProcessMe { while (true) { while (owner[other_process_id] ) { } owner[my_process_id] = true access shared variables owner[my_process_id] = false do other work } } Mutual exclusion? z

  30. 2 Flag Mutual Exclusion int owner[2]= {false, false} ProcessMe { while (true) {owner[my_process_id] = true while (owner[other_process_id] ) { } access shared variables owner[my_process_id] = false do other work } } Mutual Exclusion?

  31. 2 Flag Mutual Exclusion int owner[2]= {false, false} ProcessMe { while (true) {owner[my_process_id] = true while (owner[other_process_id] ) { } access shared variables owner[my_process_id] = false do other work } } Progress?

  32. 2 Flag Mutual Exclusion int owner[2]= {false, false} ProcessMe { while (true) {owner[my_process_id] = true while (owner[other_process_id] ) { } access shared variables owner[my_process_id] = false do other work } } Bounded Waiting? z

  33. 2 Flag and Turn Mutual Exclusion int owner[2]={false, false} int turn; ProcessMe { while (true) { owner[my_process_id] = true; turn = other_process_id; while (owner[other_process_id] and turn == other_process_id ) { } access shared variables owner[my_process_id] = false do other work } } Problems?

  34. 2 Flag and Turn Mutual Exclusion Peterson Solution int owner[2]={false, false} int turn; ProcessMe { while (true) { owner[my_process_id] = true; turn = other_process_id; while (owner[other_process_id] and turn == other_process_id) { } access shared variables owner[my_process_id] = false do other work } }

  35. Discussion In uni-processor Concurrent processes cannot be overlapped, only interleaved Process runs until it invokes system call, or is interrupted To guarantee mutual exclusion, hardware support could help by allowing disabling interrupts While(true) { /* disable interrupts */ /* critical section */ /* enable interrupts */ /* remainder */ } What’s the problem with this solution?

  36. Discussion In multi-processors Several processors share memory Processors behave independently in a peer relationship Interrupt disabling will not work We need hardware support where access to a memory location excludes any other access to that same location The hardware support is based on execution of multiple instructions atomically Atomic execution of a set of instructions means that these instructions are treated as a single step that cannot be interrupted

  37. Summary Synchronizations are important for correct multi-threading programs Data races Critical regions Solutions to protect critical regions Software-only approaches Next lecture: Multi-processor hardware solutions; semaphores

More Related