CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads...
Transcript of CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads...
![Page 1: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/1.jpg)
CS 31: Introduction to Computer Systems
22-23: Threads & SynchronizationApril 16-18, 2019
![Page 2: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/2.jpg)
Making Programs Run Faster
• We all like how fast computers are…
• In the “old days” (1980’s - 2005):– Algorithm too slow? Wait for HW to catch up.
• Modern CPUs exploit parallelism for speed:– Executes multiple instructions at once– Reorders instructions on the fly
![Page 3: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/3.jpg)
From Herb Sutter,
Dr. Dobbs Journal
Processor Design Trends
Transistors (*10^3)
Clock Speed(MHZ)
Power (W)
ILP (IPC)Instruction LevelParallelism
![Page 4: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/4.jpg)
The “Multi-Core Era”
• Today, can’t make a single core go much faster.– Limits on clock speed, heat, energy consumption
• Use extra transistors to put multiple CPU cores on the chip.
• Exciting: CPU capable of doing a lot more!• Problem: up to the programmer to take advantage of
multiple cores– Humans bad at thinking in parallel
![Page 5: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/5.jpg)
Parallel Abstraction
• To speed up a job, must divide it across multiple cores.
• A process contains both execution information and memory/resources.
• What if we want to separate the execution information to give us parallelism in our programs?
![Page 6: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/6.jpg)
Which components of a process might we replicate to take advantage of multiple CPU cores?
A. The entire address space (memory)
B. Parts of the address space (memory)
C. OS resources (open files, etc.)
D. Execution state (PC, registers, etc.)
E. More than one of these (which?)
![Page 7: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/7.jpg)
Which components of a process might we replicate to take advantage of multiple CPU cores?
A. The entire address space (memory – not duplicated)
B. Parts of the address space (memory - stack)
C. OS resources (open files, etc – not duplicated.)
D. Execution state (PC, registers, etc.)
E. More than one of these (which?)
![Page 8: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/8.jpg)
Threads
• Modern OSes separate the concepts of processes and threads.– The process defines the address space and general process
attributes (e.g., open files)– The thread defines a sequential execution stream within a
process (PC, SP, registers)
• A thread is bound to a single process– Processes, however, can have multiple threads– Each process has at least one thread (e.g. main)
![Page 9: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/9.jpg)
Threads
Text
Data
Thread 1PC1
SP1
Process 1
OS
Heap
This is the picture we’ve been using all along:
A process with a single thread, which has execution state (registers) and a stack.
Stack 1
![Page 10: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/10.jpg)
Threads
Thread 1PC1
SP1
Thread 2
PC2
SP2
Process 1
Text
Data
OS
Heap
Stack 2
We can add a thread to the process. New threads share all memory (VAS) with other threads.
New thread gets private registers, local stack.
Stack 1
![Page 11: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/11.jpg)
Threads
Thread 1PC1
SP1
Thread 2
Thread 3
PC2
SP2PC3
SP3
Process 1
Text
Data
Stack 3
OS
Heap
Stack 2
Stack 1
A third thread added.
Note: they’re all executing the same program (shared instructions in text), though they may be at different points in the code.
![Page 12: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/12.jpg)
Why Use Threads?
• Separating threads and processes makes it easier to support parallel applications:– Creating multiple paths of execution does not require
creating new processes (less state to store, initialize –Light Weight Process )
– Low-overhead sharing between threads in same process (threads share page tables, access same memory)
• Concurrency (multithreading) can be very useful
![Page 13: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/13.jpg)
Concurrency?
• Several computations or threads of control are executing simultaneously, and potentially interacting with each other.
• We can multitask! Why does that help?– Taking advantage of multiple CPUs / cores– Overlapping I/O with computation– Improving program structure
![Page 14: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/14.jpg)
Recall: Processes
Text
Data
Stack
Process 1
Text
Data
Stack
Process 2
Text
Data
Stack
Process n
…
KernelSystemCalls
write
read
forkSystem
Management Scheduling
ContextSwitching
![Page 15: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/15.jpg)
Scheduling Threads
• We have basically two options1. Kernel explicitly selects among threads in a process2. Hide threads from the kernel, and have a user-level
scheduler inside each multi-threaded process
• Why do we care? – Think about the overhead of switching between threads – Who decides which thread in a process should go first? – What about blocking system calls?
![Page 16: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/16.jpg)
User-Level Threads
Text
Data
Process 1
Text
Data
Process 2
Text
Data
Process n
…
KernelSystemCalls
write
read
fork
Stack Stack Stack
Thread C/S + Sched Thread C/S + Sched Thread C/S + Sched
System Management
ProcessScheduling
ProcessContext
Switching
Library divides stack region
Threads are invisible to the kernel
![Page 17: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/17.jpg)
Kernel-Level Threads
Text
Data
Process 1
Text
Data
Process 2
Text
Data
Process n
…
KernelSystemCalls
write
read
fork
Stack 3Stack 2Stack 1
Stack 2
Stack 1Stack 1
System Management
Thread +Process
Scheduling
ThreadContext
Switching
Kernel Context switching over threads
Each process has explicitly mapped regions for stacks
![Page 18: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/18.jpg)
If you call thread_create() on a modern OS (Linux/Mac/Windows), which type of thread would you expect to receive? (Why? Which would you pick?)
A. Kernel threads
B. User threads
C. Some other sort of threads
![Page 19: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/19.jpg)
Kernel vs. User Threads
• Kernel-level threads
– Integrated with OS (informed scheduling)
– Slower to create, manipulate, synchronize
• Requires getting the OS involved, which means changing context
(relatively expensive)
• User-level threads
– Faster to create, manipulate, synchronize
– Not integrated with OS (uninformed scheduling)
• If one thread makes a syscall, all of them get blocked because the
OS doesn’t distinguish.
![Page 20: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/20.jpg)
Threads & Sharing
• Code (text) shared by all threads in process• Global variables and static objects are shared– Stored in the static data segment, accessible by any thread
• Dynamic objects and other heap objects are shared– Allocated from heap with malloc/free or new/delete
• Local variables should not be shared – Refer to data on the stack– Each thread has its own stack– Never pass/share/store a pointer to a local variable on
another thread’s stack!!
![Page 21: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/21.jpg)
Threads & Sharing
• Local variables should not be shared – Refer to data on the stack– Each thread has its own stack– Never pass/share/store a pointer to a local variable on
another thread’s stack
…
function C
function D
…
function A
function B
Shared Heapint *x;
Z
Thread 1’s stack Thread 2’s stack
Thread 2 can dereference x to access Z.Function B returns…
![Page 22: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/22.jpg)
Threads & Sharing
• Local variables should not be shared – Refer to data on the stack– Each thread has its own stack– Never pass/share/store a pointer to a local variable on
another thread’s stack
…
function C
function D
…
function A
function B
Shared Heapint *x;
Thread 1’s stack Thread 2’s stack
Thread 2 can dereference x to access Z.
Z
Shared data on heap!
![Page 23: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/23.jpg)
Thread-level Parallelism
• Speed up application by assigning portions to CPUs/cores that process in parallel
• Requires:– partitioning responsibilities (e.g., parallel algorithm)– managing their interaction
• Example: game of life (next lab)
One core: Three cores:
![Page 24: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/24.jpg)
If one CPU core can run a program at a rate of X, how quickly will the program run on two cores? Why?
A. Slower than one core (<X)B. The same speed (X)C. Faster than one core, but not double (X-2X)D. Twice as fast (2X)E. More than twice as fast(>2X)
![Page 25: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/25.jpg)
If one CPU core can run a program at a rate of X, how quickly will the program run on two cores? Why?
A. Slower than one core (<X)B. The same speed (X)C. Faster than one core, but not double (X-2X)D. Twice as fast (2X)E. More than twice as fast(>2X)
![Page 26: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/26.jpg)
Parallel Speedup
• Performance benefit of parallel threads depends on many factors:– algorithm divisibility– communication overhead– memory hierarchy and locality– implementation quality
• For most programs, more threads means more communication, diminishing returns.
![Page 27: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/27.jpg)
Summary
• Physical limits to how much faster we can make a single core run.– Use transistors to provide more cores.– Parallelize applications to take advantage.
• OS abstraction: thread– Shares most of the address space with other threads in
same process– Gets private execution context (registers) + stack
• Coordinating threads is challenging!
![Page 28: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/28.jpg)
Recap
• To speed up a job, must divide it across multiple cores.
• Thread: abstraction for execution within process.– Threads share process memory.– Threads may need to communicate to achieve goal
• Thread communication:– To solve task (e.g., neighbor GOL cells)– To prevent bad interactions (synchronization)
![Page 29: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/29.jpg)
Synchronization
• Synchronize: to (arrange events to) happen at same time (or ensure that they don’t)
• Thread synchronization– When one thread has to wait for another– Events in threads that occur “at the same time”
• Uses of synchronization– Prevent race conditions– Wait for resources to become available
![Page 30: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/30.jpg)
Synchronization Example
• Coordination required:– Threads in different regions must work together to
compute new value for boundary cells.– Threads might not run at the same speed (depends on
the OS scheduler). Can’t let one region get too far ahead.
One core: Four cores:
![Page 31: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/31.jpg)
Thread Ordering(Why threads require care. Humans aren’t good at reasoning about this.)
• As a programmer you have no idea when threads will run. The OS schedules them, and the schedule will vary across runs.
• It might decide to context switch from one thread to another at any time.
• Your code must be prepared for this!– Ask yourself: “Would something bad happen if we
context switched here?”
![Page 32: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/32.jpg)
Example: The Credit/Debit Problem
• Say you have $1000 in your bank account– You deposit $100– You also withdraw $100
• How much should be in your account?
• What if your deposit and withdrawal occur at the same time, at different ATMs?
![Page 33: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/33.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
![Page 34: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/34.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
![Page 35: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/35.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
Switch to T1
Read $1000 into bDebit by $100Write $900
![Page 36: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/36.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
Switch to T1
Read $1000 into bDebit by $100Write $900
Switch back to T0
Read $1000 into bCredit $100Write $1100
Bank gave you $100!
What went wrong?
![Page 37: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/37.jpg)
“Critical Section”
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Bank gave you $100!
What went wrong?
Badness if context switch here!
![Page 38: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/38.jpg)
To Avoid Race Conditions
1. Identify critical sections
2. Use synchronization to enforce mutual exclusion– Only one thread active in a critical section
Thread 0
------------------------- Critical -- Section -------------------------
Thread 1
------------------------- Critical -- Section -------------------------
![Page 39: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/39.jpg)
What Are Critical Sections?
• Sections of code executed by multiple threads– Access shared variables, often making local copy– Places where order of execution or thread interleaving
will affect the outcome
• Must run atomically with respect to each other– Atomicity: runs as an entire unit or not at all. Cannot
be divided into smaller parts.
![Page 40: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/40.jpg)
Which code region is a critical section?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
a += 1
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
a += 1
return a;}
Thread B
s = 40;
sharedmemory
AC
B
D E
![Page 41: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/41.jpg)
Which code region is a critical section?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
a += 1
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
a += 1
return a;}
Thread B
s = 40;
sharedmemory
![Page 42: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/42.jpg)
Which values might the shared s variable hold after both threads finish?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
Thread B
s = 40;
sharedmemory
A. 30B. 20 or 30C. 20, 30, or 50D. Another set of values
![Page 43: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/43.jpg)
If A runs first
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 50;
sharedmemory
Thread A Thread B
![Page 44: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/44.jpg)
B runs after A Completes
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 30;
sharedmemory
Thread A Thread B
![Page 45: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/45.jpg)
What about interleaving?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 40;
sharedmemory
Thread A Thread B
![Page 46: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/46.jpg)
Is there a race condition?Suppose count is a global variable, multiple threads increment it:count++;
A. Yes, there’s a race condition (count++ is a critical section).B. No, there’s no race condition (count++ is not a critical section).C. Cannot be determined
movl (%edx), %eax // read count valueaddl $1, %eax // modify valuemovl %eax, (%edx) // write count
How about if compiler implements it as:
incl (%edx) // increment value
How about if compiler implements it as:
![Page 47: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/47.jpg)
Four Rules for Mutual Exclusion
1. No two threads can be inside their critical sections at the same time.
2. No thread outside its critical section may prevent others from entering their critical sections.
3. No thread should have to wait forever to enter its critical section. (Starvation)
4. No assumptions can be made about speeds or number of CPU’s.
![Page 48: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/48.jpg)
How to Achieve Mutual Exclusion?
• Surround critical section with entry/exit code• Entry code should act as a gate– If another thread is in critical section, block– Otherwise, allow thread to proceed
• Exit code should release other entry gates
< entry code >
< critical section >
< exit code >
< entry code >
< critical section >
< exit code >
![Page 49: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/49.jpg)
Possible Solution: Spin Lock?
• Lock indicates whether any thread is in critical section.
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
Note: While loop has no body. Keeps checking the condition as quickly as possible until it becomes false. (It “spins”)
![Page 50: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/50.jpg)
Possible Solution: Spin Lock?
• Lock indicates whether any thread is in critical section.• Is there a problem here?– A: Yes, this is broken.– B: No, this ought to work.
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
![Page 51: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/51.jpg)
Possible Solution: Spin Lock?
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
• What if a context switch occurs at this point?
![Page 52: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/52.jpg)
Possible Solution: Take Turns?
• Alternate which thread can enter critical section• Is there a problem?– A: Yes, this is broken.– B: No, this ought to work.
T0while (turn != T0);
< critical section >
turn = T1;
T1while (turn != T1);
< critical section >
turn = T0;
shared int turn = T0;
![Page 53: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/53.jpg)
Possible Solution: Take Turns?
• Rule #2: No thread outside its critical section may prevent others from entering their critical sections.
T0while (turn != T0);
< critical section >
turn = T1;
T1while (turn != T1);
< critical section >
turn = T0;
shared int turn = T0;
![Page 54: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/54.jpg)
Possible Solution: State Intention?
• Each thread states it wants to enter critical section• Is there a problem?– A: Yes, this is broken.– B: No, this ought to work.
T0flag[T0] = TRUE;
while (flag[T1]);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
while (flag[T0]);
< critical section >
flag[T1] = FALSE;
shared boolean flag[2] = {FALSE, FALSE};
![Page 55: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/55.jpg)
Possible Solution: State Intention?
• What if threads context switch between these two lines?
• Rule #3: No thread should have to wait forever to enter its critical section.
T0flag[T0] = TRUE;
while (flag[T1]);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
while (flag[T0]);
< critical section >
flag[T1] = FALSE;
shared boolean flag[2] = {FALSE, FALSE};
![Page 56: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/56.jpg)
Peterson’s Solution
• If there is competition, take turns; otherwise, enter• Is there a problem?• A: Yes, this is broken.• B: No, this ought to work.
T0flag[T0] = TRUE;
turn = T1;
while (flag[T1] && turn==T1);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
turn = T0;
while (flag[T0] && turn==T0);
< critical section >
flag[T1] = FALSE;
shared int turn;shared boolean flag[2] = {FALSE, FALSE};
![Page 57: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/57.jpg)
Spinlocks are Wasteful
• If a thread is spinning on a lock, it’s using the CPU without making progress.– Single-core system, prevents lock holder from
executing.– Multi-core system, waste core time when something
else could be running.
• Ideal: thread can’t enter critical section? Schedule something else. Consider it blocked.
![Page 58: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/58.jpg)
![Page 59: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/59.jpg)
Atomicity
• How do we get away from having to know about all other interested threads?
• The implementation of acquiring/releasing critical section must be atomic.– An atomic operation is one which executes as though it
could not be interrupted – Code that executes “all or nothing”
• How do we make them atomic? – Atomic instructions (e.g., test-and-set, compare-and-swap)– Allows us to build “semaphore” OS abstraction
![Page 60: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/60.jpg)
Semaphores
• Semaphore: OS synchronization variable– Has integer value– List of waiting threads
• Works like a gate• If sem > 0, gate is open– Value equals number of threads that can enter
• Else, gate is closed– Possibly with waiting threads
criticalsection
sem = 1sem = 2
sem = 3
sem = 0
![Page 61: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/61.jpg)
Semaphores
• Associated with each semaphore is a queue of waiting threads
• When wait() is called by a thread: – If semaphore is open, thread continues – If semaphore is closed, thread blocks on queue
• Then signal() opens the semaphore:– If a thread is waiting on the queue, the thread is
unblocked– If no threads are waiting on the queue, the signal is
remembered for the next thread
![Page 62: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/62.jpg)
Semaphore Operations
sem s = n; // declare and initialize
wait (sem s) // Executes atomicallydecrement s;if s < 0, block thread (and associate with s);
signal (sem s) // Executes atomicallyincrement s;if blocked threads, unblock (any) one of them;
![Page 63: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/63.jpg)
Semaphore Operations
Based on what you know about semaphores, should a process be able to check beforehand whether wait(s) will cause it to block?
A. Yes, it should be able to check.B. No, it should not be able to check.
sem s = n; // declare and initialize
wait (sem s) // Executes atomicallydecrement s;if s < 0, block thread (and associate with s);
signal (sem s) // Executes atomicallyincrement s;if blocked threads, unblock (any) one of them;
![Page 64: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/64.jpg)
Semaphore Operations
• No other operations allowed• In particular, semaphore’s value can’t be tested!• No thread can tell the value of s
sem s = n; // declare and initialize
wait (sem s)decrement s;if s < 0, block thread (and associate with s);
signal (sem s)increment s;if blocked threads, unblock (any) one of them;
![Page 65: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/65.jpg)
Mutual Exclusion with Semaphores
• Use a “mutex” semaphore initialized to 1• Only one thread can enter critical section• Simple, works for any number of threads• Is there any busy-waiting?
T0wait (mutex);
< critical section >
signal (mutex);
T1wait (mutex);
< critical section >
signal (mutex);
sem mutex = 1;
![Page 66: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/66.jpg)
Locking Abstraction
• One way to implement critical sections is to “lock the door” on the way in, and unlock it again on the way out– Typically exports “nicer” interface for semaphores in user space
• A lock is an object in memory providing two operations – acquire()/lock(): before entering the critical section– release()/unlock(): after leaving a critical section
• Threads pair calls to acquire() and release()– Between acquire()/release(), the thread holds the lock– acquire() does not return until any previous holder releases – What can happen if the calls are not paired?
![Page 67: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/67.jpg)
Using Locks
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
Thread B
s = 40;
sharedmemory
![Page 68: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/68.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Nobody
![Page 69: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/69.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
![Page 70: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/70.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
![Page 71: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/71.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
Lock already owned.
Must Wait!
![Page 72: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/72.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 50;Lock l;
sharedmemory
Thread A Thread B
Held by: Nobody
![Page 73: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/73.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 30;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread B
![Page 74: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/74.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 30;Lock l;
sharedmemory
• No matter how we order threads or when we context switch, result will always be 30, like we expected (and probably wanted).
Thread A Thread B
Held by: Nobody
![Page 75: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/75.jpg)
Summary
• We have no idea when OS will schedule or context switch our threads.– Code must be prepared, tough to reason about.
• Threads often must synchronize– To safely communicate / transfer data, without races
• Synchronization primitives help programmers– Kernel-level semaphores: limit # of threads that can do
something, provides atomicity– User-level locks: built upon semaphore, provides mutual
exclusion (usually part of thread library)
![Page 76: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/76.jpg)
Recap
• To speed up a job, must divide it across multiple cores.
• Thread: abstraction for execution within process.– Threads share process memory.– Threads may need to communicate to achieve goal
• Thread communication:– To solve task (e.g., neighbor GOL cells)– To prevent bad interactions (synchronization)
![Page 77: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/77.jpg)
Synchronization
• Synchronize: to (arrange events to) happen at same time (or ensure that they don’t)
• Thread synchronization– When one thread has to wait for another– Events in threads that occur “at the same time”
• Uses of synchronization– Prevent race conditions– Wait for resources to become available
![Page 78: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/78.jpg)
Synchronization Example
• Coordination required:– Threads in different regions must work together to
compute new value for boundary cells.– Threads might not run at the same speed (depends on
the OS scheduler). Can’t let one region get too far ahead.
One core: Four cores:
![Page 79: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/79.jpg)
Thread Ordering(Why threads require care. Humans aren’t good at reasoning about this.)
• As a programmer you have no idea when threads will run. The OS schedules them, and the schedule will vary across runs.
• It might decide to context switch from one thread to another at any time.
• Your code must be prepared for this!– Ask yourself: “Would something bad happen if we
context switched here?”
![Page 80: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/80.jpg)
Example: The Credit/Debit Problem
• Say you have $1000 in your bank account– You deposit $100– You also withdraw $100
• How much should be in your account?
• What if your deposit and withdrawal occur at the same time, at different ATMs?
![Page 81: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/81.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
![Page 82: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/82.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
![Page 83: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/83.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
Switch to T1
Read $1000 into bDebit by $100Write $900
![Page 84: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/84.jpg)
Credit/Debit Problem: Race Condition
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Say T0 runs first
Read $1000 into b
Switch to T1
Read $1000 into bDebit by $100Write $900
Switch back to T0
Read $1000 into bCredit $100Write $1100
Bank gave you $100!
What went wrong?
![Page 85: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/85.jpg)
“Critical Section”
Thread T0
Credit (int a) {int b;
b = ReadBalance ();b = b + a;WriteBalance (b);
PrintReceipt (b);}
Thread T1
Debit (int a) {int b;
b = ReadBalance ();b = b - a;WriteBalance (b);
PrintReceipt (b);}
Bank gave you $100!
What went wrong?
Badness if context switch here!
![Page 86: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/86.jpg)
To Avoid Race Conditions
1. Identify critical sections
2. Use synchronization to enforce mutual exclusion– Only one thread active in a critical section
Thread 0
------------------------- Critical -- Section -------------------------
Thread 1
------------------------- Critical -- Section -------------------------
![Page 87: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/87.jpg)
What Are Critical Sections?
• Sections of code executed by multiple threads– Access shared variables, often making local copy– Places where order of execution or thread interleaving
will affect the outcome
• Must run atomically with respect to each other– Atomicity: runs as an entire unit or not at all. Cannot
be divided into smaller parts.
![Page 88: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/88.jpg)
Which code region is a critical section?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
a += 1
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
a += 1
return a;}
Thread B
s = 40;
sharedmemory
AC
B
D E
![Page 89: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/89.jpg)
Which code region is a critical section?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
a += 1
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
a += 1
return a;}
Thread B
s = 40;
sharedmemory
![Page 90: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/90.jpg)
Which values might the shared s variable hold after both threads finish?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
Thread B
s = 40;
sharedmemory
A. 30B. 20 or 30C. 20, 30, or 50D. Another set of values
![Page 91: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/91.jpg)
If A runs first
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 50;
sharedmemory
Thread A Thread B
![Page 92: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/92.jpg)
B runs after A Completes
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 30;
sharedmemory
Thread A Thread B
![Page 93: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/93.jpg)
What about interleaving?
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
main (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
s = 40;
sharedmemory
Thread A Thread B
![Page 94: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/94.jpg)
Is there a race condition?Suppose count is a global variable, multiple threads increment it:count++;
A. Yes, there’s a race condition (count++ is a critical section).B. No, there’s no race condition (count++ is not a critical section).C. Cannot be determined
movl (%edx), %eax // read count valueaddl $1, %eax // modify valuemovl %eax, (%edx) // write count
How about if compiler implements it as:
incl (%edx) // increment value
How about if compiler implements it as:
![Page 95: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/95.jpg)
Four Rules for Mutual Exclusion
1. No two threads can be inside their critical sections at the same time.
2. No thread outside its critical section may prevent others from entering their critical sections.
3. No thread should have to wait forever to enter its critical section. (Starvation)
4. No assumptions can be made about speeds or number of CPU’s.
![Page 96: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/96.jpg)
How to Achieve Mutual Exclusion?
• Surround critical section with entry/exit code• Entry code should act as a gate– If another thread is in critical section, block– Otherwise, allow thread to proceed
• Exit code should release other entry gates
< entry code >
< critical section >
< exit code >
< entry code >
< critical section >
< exit code >
![Page 97: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/97.jpg)
Possible Solution: Spin Lock?
• Lock indicates whether any thread is in critical section.
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
Note: While loop has no body. Keeps checking the condition as quickly as possible until it becomes false. (It “spins”)
![Page 98: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/98.jpg)
Possible Solution: Spin Lock?
• Lock indicates whether any thread is in critical section.• Is there a problem here?– A: Yes, this is broken.– B: No, this ought to work.
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
![Page 99: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/99.jpg)
Possible Solution: Spin Lock?
T0while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
T1while (lock == CLOSED);
lock = CLOSED;
< critical section >
lock = OPEN;
shared int lock = OPEN;
• What if a context switch occurs at this point?
![Page 100: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/100.jpg)
Possible Solution: Take Turns?
• Alternate which thread can enter critical section• Is there a problem?– A: Yes, this is broken.– B: No, this ought to work.
T0while (turn != T0);
< critical section >
turn = T1;
T1while (turn != T1);
< critical section >
turn = T0;
shared int turn = T0;
![Page 101: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/101.jpg)
Possible Solution: Take Turns?
• Rule #2: No thread outside its critical section may prevent others from entering their critical sections.
T0while (turn != T0);
< critical section >
turn = T1;
T1while (turn != T1);
< critical section >
turn = T0;
shared int turn = T0;
![Page 102: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/102.jpg)
Possible Solution: State Intention?
• Each thread states it wants to enter critical section• Is there a problem?– A: Yes, this is broken.– B: No, this ought to work.
T0flag[T0] = TRUE;
while (flag[T1]);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
while (flag[T0]);
< critical section >
flag[T1] = FALSE;
shared boolean flag[2] = {FALSE, FALSE};
![Page 103: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/103.jpg)
Possible Solution: State Intention?
• What if threads context switch between these two lines?
• Rule #3: No thread should have to wait forever to enter its critical section.
T0flag[T0] = TRUE;
while (flag[T1]);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
while (flag[T0]);
< critical section >
flag[T1] = FALSE;
shared boolean flag[2] = {FALSE, FALSE};
![Page 104: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/104.jpg)
Peterson’s Solution
• If there is competition, take turns; otherwise, enter• Is there a problem?• A: Yes, this is broken.• B: No, this ought to work.
T0flag[T0] = TRUE;
turn = T1;
while (flag[T1] && turn==T1);
< critical section >
flag[T0] = FALSE;
T1flag[T1] = TRUE;
turn = T0;
while (flag[T0] && turn==T0);
< critical section >
flag[T1] = FALSE;
shared int turn;shared boolean flag[2] = {FALSE, FALSE};
![Page 105: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/105.jpg)
Spinlocks are Wasteful
• If a thread is spinning on a lock, it’s using the CPU without making progress.– Single-core system, prevents lock holder from
executing.– Multi-core system, waste core time when something
else could be running.
• Ideal: thread can’t enter critical section? Schedule something else. Consider it blocked.
![Page 106: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/106.jpg)
![Page 107: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/107.jpg)
Atomicity
• How do we get away from having to know about all other interested threads?
• The implementation of acquiring/releasing critical section must be atomic.– An atomic operation is one which executes as though it
could not be interrupted – Code that executes “all or nothing”
• How do we make them atomic? – Atomic instructions (e.g., test-and-set, compare-and-swap)– Allows us to build “semaphore” OS abstraction
![Page 108: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/108.jpg)
Semaphores
• Semaphore: OS synchronization variable– Has integer value– List of waiting threads
• Works like a gate• If sem > 0, gate is open– Value equals number of threads that can enter
• Else, gate is closed– Possibly with waiting threads
criticalsection
sem = 1sem = 2
sem = 3
sem = 0
![Page 109: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/109.jpg)
Semaphores
• Associated with each semaphore is a queue of waiting threads
• When wait() is called by a thread: – If semaphore is open, thread continues – If semaphore is closed, thread blocks on queue
• Then signal() opens the semaphore:– If a thread is waiting on the queue, the thread is
unblocked– If no threads are waiting on the queue, the signal is
remembered for the next thread
![Page 110: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/110.jpg)
Semaphore Operations
sem s = n; // declare and initialize
wait (sem s) // Executes atomicallydecrement s;if s < 0, block thread (and associate with s);
signal (sem s) // Executes atomicallyincrement s;if blocked threads, unblock (any) one of them;
![Page 111: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/111.jpg)
Semaphore Operations
Based on what you know about semaphores, should a process be able to check beforehand whether wait(s) will cause it to block?
A. Yes, it should be able to check.B. No, it should not be able to check.
sem s = n; // declare and initialize
wait (sem s) // Executes atomicallydecrement s;if s < 0, block thread (and associate with s);
signal (sem s) // Executes atomicallyincrement s;if blocked threads, unblock (any) one of them;
![Page 112: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/112.jpg)
Semaphore Operations
• No other operations allowed• In particular, semaphore’s value can’t be tested!• No thread can tell the value of s
sem s = n; // declare and initialize
wait (sem s)decrement s;if s < 0, block thread (and associate with s);
signal (sem s)increment s;if blocked threads, unblock (any) one of them;
![Page 113: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/113.jpg)
Mutual Exclusion with Semaphores
• Use a “mutex” semaphore initialized to 1• Only one thread can enter critical section• Simple, works for any number of threads• Is there any busy-waiting?
T0wait (mutex);
< critical section >
signal (mutex);
T1wait (mutex);
< critical section >
signal (mutex);
sem mutex = 1;
![Page 114: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/114.jpg)
Locking Abstraction
• One way to implement critical sections is to “lock the door” on the way in, and unlock it again on the way out– Typically exports “nicer” interface for semaphores in user space
• A lock is an object in memory providing two operations – acquire()/lock(): before entering the critical section– release()/unlock(): after leaving a critical section
• Threads pair calls to acquire() and release()– Between acquire()/release(), the thread holds the lock– acquire() does not return until any previous holder releases – What can happen if the calls are not paired?
![Page 115: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/115.jpg)
Using Locks
main (){ int a,b;
a = getShared();b = 10;a = a + b;saveShared(a);
return a;}
Thread Amain (){ int a,b;
a = getShared();b = 20;a = a - b;saveShared(a);
return a;}
Thread B
s = 40;
sharedmemory
![Page 116: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/116.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Nobody
![Page 117: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/117.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
![Page 118: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/118.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
![Page 119: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/119.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 40;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread A
Lock already owned.
Must Wait!
![Page 120: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/120.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 50;Lock l;
sharedmemory
Thread A Thread B
Held by: Nobody
![Page 121: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/121.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 30;Lock l;
sharedmemory
Thread A Thread B
Held by: Thread B
![Page 122: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/122.jpg)
Using Locks
main (){ int a,b;
acquire(l);a = getShared();b = 10;a = a + b;saveShared(a);release(l);
return a;}
main (){ int a,b;
acquire(l);a = getShared();b = 20;a = a - b;saveShared(a);release(l);
return a;}
s = 30;Lock l;
sharedmemory
• No matter how we order threads or when we context switch, result will always be 30, like we expected (and probably wanted).
Thread A Thread B
Held by: Nobody
![Page 123: CS 31: Introduction to Computer Systemschaganti/cs31/s19/lecs/22... · 2019. 4. 18. · Threads •Modern OSesseparate the concepts of processes and threads. –The process defines](https://reader035.fdocuments.in/reader035/viewer/2022071100/5fd8eddbee81fd6a3c0686eb/html5/thumbnails/123.jpg)
Summary
• We have no idea when OS will schedule or context switch our threads.– Code must be prepared, tough to reason about.
• Threads often must synchronize– To safely communicate / transfer data, without races
• Synchronization primitives help programmers– Kernel-level semaphores: limit # of threads that can do
something, provides atomicity– User-level locks: built upon semaphore, provides mutual
exclusion (usually part of thread library)