Download - Topic 4: Concurrency
![Page 1: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/1.jpg)
4: Concurrency
Zubair Nabi
April 18, 2013
Zubair Nabi 4: Concurrency April 18, 2013 1 / 22
![Page 2: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/2.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 2 / 22
![Page 3: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/3.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 3 / 22
![Page 4: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/4.jpg)
Execution
ProcessI Program in execution
I Unit of protection and resource allocationI Private address space and one or more threads
ThreadI An entity managed by the schedulerI Represents an individual execution contextI Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 5: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/5.jpg)
Execution
ProcessI Program in executionI Unit of protection and resource allocation
I Private address space and one or more threads
ThreadI An entity managed by the schedulerI Represents an individual execution contextI Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 6: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/6.jpg)
Execution
ProcessI Program in executionI Unit of protection and resource allocationI Private address space and one or more threads
ThreadI An entity managed by the schedulerI Represents an individual execution contextI Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 7: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/7.jpg)
Execution
ProcessI Program in executionI Unit of protection and resource allocationI Private address space and one or more threads
ThreadI An entity managed by the scheduler
I Represents an individual execution contextI Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 8: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/8.jpg)
Execution
ProcessI Program in executionI Unit of protection and resource allocationI Private address space and one or more threads
ThreadI An entity managed by the schedulerI Represents an individual execution context
I Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 9: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/9.jpg)
Execution
ProcessI Program in executionI Unit of protection and resource allocationI Private address space and one or more threads
ThreadI An entity managed by the schedulerI Represents an individual execution contextI Runs within the address space of the containing process
Zubair Nabi 4: Concurrency April 18, 2013 4 / 22
![Page 10: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/10.jpg)
Concurrency
Seemingly doing many things at once
With a single processor: Interleaving of different executions1 Process/OS2 Inter-process3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 11: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/11.jpg)
Concurrency
Seemingly doing many things at onceWith a single processor: Interleaving of different executions
1 Process/OS2 Inter-process3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 12: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/12.jpg)
Concurrency
Seemingly doing many things at onceWith a single processor: Interleaving of different executions
1 Process/OS
2 Inter-process3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 13: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/13.jpg)
Concurrency
Seemingly doing many things at onceWith a single processor: Interleaving of different executions
1 Process/OS2 Inter-process
3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 14: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/14.jpg)
Concurrency
Seemingly doing many things at onceWith a single processor: Interleaving of different executions
1 Process/OS2 Inter-process3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 15: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/15.jpg)
Concurrency
Seemingly doing many things at onceWith a single processor: Interleaving of different executions
1 Process/OS2 Inter-process3 Intra-process
With multiple processorsI True parallelism
Zubair Nabi 4: Concurrency April 18, 2013 5 / 22
![Page 16: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/16.jpg)
Advantages
Overlap computation and I/O
Simplify code structuring
Improve responsiveness
Seamless use of multiple CPUs
Zubair Nabi 4: Concurrency April 18, 2013 6 / 22
![Page 17: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/17.jpg)
Advantages
Overlap computation and I/O
Simplify code structuring
Improve responsiveness
Seamless use of multiple CPUs
Zubair Nabi 4: Concurrency April 18, 2013 6 / 22
![Page 18: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/18.jpg)
Advantages
Overlap computation and I/O
Simplify code structuring
Improve responsiveness
Seamless use of multiple CPUs
Zubair Nabi 4: Concurrency April 18, 2013 6 / 22
![Page 19: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/19.jpg)
Advantages
Overlap computation and I/O
Simplify code structuring
Improve responsiveness
Seamless use of multiple CPUs
Zubair Nabi 4: Concurrency April 18, 2013 6 / 22
![Page 20: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/20.jpg)
Critical sections
Recurring problem: Two (or more) threads trying to solve the sameproblem concurrently
Only one thread should be allowed to access the piece of code thatshould never be concurrently executed
I Known as a critical section
Only one thread should be executing within a critical sectionI Known as mutual exclusion
Zubair Nabi 4: Concurrency April 18, 2013 7 / 22
![Page 21: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/21.jpg)
Critical sections
Recurring problem: Two (or more) threads trying to solve the sameproblem concurrentlyOnly one thread should be allowed to access the piece of code thatshould never be concurrently executed
I Known as a critical section
Only one thread should be executing within a critical sectionI Known as mutual exclusion
Zubair Nabi 4: Concurrency April 18, 2013 7 / 22
![Page 22: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/22.jpg)
Critical sections
Recurring problem: Two (or more) threads trying to solve the sameproblem concurrentlyOnly one thread should be allowed to access the piece of code thatshould never be concurrently executed
I Known as a critical section
Only one thread should be executing within a critical sectionI Known as mutual exclusion
Zubair Nabi 4: Concurrency April 18, 2013 7 / 22
![Page 23: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/23.jpg)
Critical sections
Recurring problem: Two (or more) threads trying to solve the sameproblem concurrentlyOnly one thread should be allowed to access the piece of code thatshould never be concurrently executed
I Known as a critical section
Only one thread should be executing within a critical section
I Known as mutual exclusion
Zubair Nabi 4: Concurrency April 18, 2013 7 / 22
![Page 24: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/24.jpg)
Critical sections
Recurring problem: Two (or more) threads trying to solve the sameproblem concurrentlyOnly one thread should be allowed to access the piece of code thatshould never be concurrently executed
I Known as a critical section
Only one thread should be executing within a critical sectionI Known as mutual exclusion
Zubair Nabi 4: Concurrency April 18, 2013 7 / 22
![Page 25: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/25.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 26: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/26.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 27: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/27.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queue
I Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 28: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/28.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()
I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 29: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/29.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronization
I Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 30: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/30.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 31: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/31.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 32: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/32.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 33: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/33.jpg)
Ensuring mutex
Locks: Ability to lock and unlock a critical section (based on an atomicoperation enabled by the hardware)
I Overhead of busy waiting
Semaphores: Implemented as an integer and a queueI Ability to wait() and signal()I Can also provide condition synchronizationI Correct use requires considerable care (forgetting to call wait() orsignal())
Monitors: Explicitly declare variables and tag code as using thosevariables
I Compiler automatically declares and manages underlying primitives formutual exclusion or synchronization
I Related routines are combined together
Zubair Nabi 4: Concurrency April 18, 2013 8 / 22
![Page 34: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/34.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 9 / 22
![Page 35: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/35.jpg)
Linux Kernel
Kernel provides spinlocks and semaphores
I Spinlocks busy wait so only hold for short time
Also provides reader-writer spinlock variantsI Allows many readers or a single writer
Zubair Nabi 4: Concurrency April 18, 2013 10 / 22
![Page 36: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/36.jpg)
Linux Kernel
Kernel provides spinlocks and semaphoresI Spinlocks busy wait so only hold for short time
Also provides reader-writer spinlock variantsI Allows many readers or a single writer
Zubair Nabi 4: Concurrency April 18, 2013 10 / 22
![Page 37: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/37.jpg)
Linux Kernel
Kernel provides spinlocks and semaphoresI Spinlocks busy wait so only hold for short time
Also provides reader-writer spinlock variants
I Allows many readers or a single writer
Zubair Nabi 4: Concurrency April 18, 2013 10 / 22
![Page 38: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/38.jpg)
Linux Kernel
Kernel provides spinlocks and semaphoresI Spinlocks busy wait so only hold for short time
Also provides reader-writer spinlock variantsI Allows many readers or a single writer
Zubair Nabi 4: Concurrency April 18, 2013 10 / 22
![Page 39: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/39.jpg)
Java
Inspired by monitors
Through the synchronize primitive
Objects already encapsulate data and methods
Block synchronization is preferred over method synchronization
C# is similar too but requires explicit invocation
Zubair Nabi 4: Concurrency April 18, 2013 11 / 22
![Page 40: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/40.jpg)
Java
Inspired by monitors
Through the synchronize primitive
Objects already encapsulate data and methods
Block synchronization is preferred over method synchronization
C# is similar too but requires explicit invocation
Zubair Nabi 4: Concurrency April 18, 2013 11 / 22
![Page 41: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/41.jpg)
Java
Inspired by monitors
Through the synchronize primitive
Objects already encapsulate data and methods
Block synchronization is preferred over method synchronization
C# is similar too but requires explicit invocation
Zubair Nabi 4: Concurrency April 18, 2013 11 / 22
![Page 42: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/42.jpg)
Java
Inspired by monitors
Through the synchronize primitive
Objects already encapsulate data and methods
Block synchronization is preferred over method synchronization
C# is similar too but requires explicit invocation
Zubair Nabi 4: Concurrency April 18, 2013 11 / 22
![Page 43: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/43.jpg)
Java
Inspired by monitors
Through the synchronize primitive
Objects already encapsulate data and methods
Block synchronization is preferred over method synchronization
C# is similar too but requires explicit invocation
Zubair Nabi 4: Concurrency April 18, 2013 11 / 22
![Page 44: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/44.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 12 / 22
![Page 45: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/45.jpg)
Liveness
Want threads to make progress
To make progress need to avoid:1 Deadlock: Threads sleep waiting for each other2 Livelock: Threads execute but make no progress
For good performance also need to ensure:1 No starvation: Each thread must make progress2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 46: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/46.jpg)
Liveness
Want threads to make progressTo make progress need to avoid:
1 Deadlock: Threads sleep waiting for each other
2 Livelock: Threads execute but make no progress
For good performance also need to ensure:1 No starvation: Each thread must make progress2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 47: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/47.jpg)
Liveness
Want threads to make progressTo make progress need to avoid:
1 Deadlock: Threads sleep waiting for each other2 Livelock: Threads execute but make no progress
For good performance also need to ensure:1 No starvation: Each thread must make progress2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 48: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/48.jpg)
Liveness
Want threads to make progressTo make progress need to avoid:
1 Deadlock: Threads sleep waiting for each other2 Livelock: Threads execute but make no progress
For good performance also need to ensure:
1 No starvation: Each thread must make progress2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 49: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/49.jpg)
Liveness
Want threads to make progressTo make progress need to avoid:
1 Deadlock: Threads sleep waiting for each other2 Livelock: Threads execute but make no progress
For good performance also need to ensure:1 No starvation: Each thread must make progress
2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 50: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/50.jpg)
Liveness
Want threads to make progressTo make progress need to avoid:
1 Deadlock: Threads sleep waiting for each other2 Livelock: Threads execute but make no progress
For good performance also need to ensure:1 No starvation: Each thread must make progress2 Minimality: No unnecessary waiting or signalling
Zubair Nabi 4: Concurrency April 18, 2013 13 / 22
![Page 51: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/51.jpg)
Deadlock
Set of threads go to sleep and cannot wake up
Can only be woken by another thread who is also asleepDealing with deadlocks:
1 Ensure it never happens: Prevention and avoidance2 Let it happen but recover: Detection and recovery3 Ignore it: Let the programmer deal with it
Zubair Nabi 4: Concurrency April 18, 2013 14 / 22
![Page 52: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/52.jpg)
Deadlock
Set of threads go to sleep and cannot wake up
Can only be woken by another thread who is also asleep
Dealing with deadlocks:1 Ensure it never happens: Prevention and avoidance2 Let it happen but recover: Detection and recovery3 Ignore it: Let the programmer deal with it
Zubair Nabi 4: Concurrency April 18, 2013 14 / 22
![Page 53: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/53.jpg)
Deadlock
Set of threads go to sleep and cannot wake up
Can only be woken by another thread who is also asleepDealing with deadlocks:
1 Ensure it never happens: Prevention and avoidance
2 Let it happen but recover: Detection and recovery3 Ignore it: Let the programmer deal with it
Zubair Nabi 4: Concurrency April 18, 2013 14 / 22
![Page 54: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/54.jpg)
Deadlock
Set of threads go to sleep and cannot wake up
Can only be woken by another thread who is also asleepDealing with deadlocks:
1 Ensure it never happens: Prevention and avoidance2 Let it happen but recover: Detection and recovery
3 Ignore it: Let the programmer deal with it
Zubair Nabi 4: Concurrency April 18, 2013 14 / 22
![Page 55: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/55.jpg)
Deadlock
Set of threads go to sleep and cannot wake up
Can only be woken by another thread who is also asleepDealing with deadlocks:
1 Ensure it never happens: Prevention and avoidance2 Let it happen but recover: Detection and recovery3 Ignore it: Let the programmer deal with it
Zubair Nabi 4: Concurrency April 18, 2013 14 / 22
![Page 56: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/56.jpg)
Livelock
Deadlock is relatively easy to detect by humans
Livelock is less easy to detect as threads continue to run (as opposed toblocking in deadlock) but do nothing usefulPrevalent in code used to detect and handle deadlocks
I Two threads detect a deadlock and try to step aside for each other
Zubair Nabi 4: Concurrency April 18, 2013 15 / 22
![Page 57: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/57.jpg)
Livelock
Deadlock is relatively easy to detect by humans
Livelock is less easy to detect as threads continue to run (as opposed toblocking in deadlock) but do nothing useful
Prevalent in code used to detect and handle deadlocksI Two threads detect a deadlock and try to step aside for each other
Zubair Nabi 4: Concurrency April 18, 2013 15 / 22
![Page 58: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/58.jpg)
Livelock
Deadlock is relatively easy to detect by humans
Livelock is less easy to detect as threads continue to run (as opposed toblocking in deadlock) but do nothing usefulPrevalent in code used to detect and handle deadlocks
I Two threads detect a deadlock and try to step aside for each other
Zubair Nabi 4: Concurrency April 18, 2013 15 / 22
![Page 59: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/59.jpg)
Livelock
Deadlock is relatively easy to detect by humans
Livelock is less easy to detect as threads continue to run (as opposed toblocking in deadlock) but do nothing usefulPrevalent in code used to detect and handle deadlocks
I Two threads detect a deadlock and try to step aside for each other
Zubair Nabi 4: Concurrency April 18, 2013 15 / 22
![Page 60: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/60.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 16 / 22
![Page 61: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/61.jpg)
Concurrency without shared data
Only one thread can access any particular piece of data
Different threads can own distinct chunks of data
Ensure concurrency by allowing other threads to ask for operations to bedone on their behalf (Dynamic invocation)
Can be thought of as general message passing (Similar to email)
Can be used to implement RPC
Zubair Nabi 4: Concurrency April 18, 2013 17 / 22
![Page 62: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/62.jpg)
Concurrency without shared data
Only one thread can access any particular piece of data
Different threads can own distinct chunks of data
Ensure concurrency by allowing other threads to ask for operations to bedone on their behalf (Dynamic invocation)
Can be thought of as general message passing (Similar to email)
Can be used to implement RPC
Zubair Nabi 4: Concurrency April 18, 2013 17 / 22
![Page 63: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/63.jpg)
Concurrency without shared data
Only one thread can access any particular piece of data
Different threads can own distinct chunks of data
Ensure concurrency by allowing other threads to ask for operations to bedone on their behalf (Dynamic invocation)
Can be thought of as general message passing (Similar to email)
Can be used to implement RPC
Zubair Nabi 4: Concurrency April 18, 2013 17 / 22
![Page 64: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/64.jpg)
Concurrency without shared data
Only one thread can access any particular piece of data
Different threads can own distinct chunks of data
Ensure concurrency by allowing other threads to ask for operations to bedone on their behalf (Dynamic invocation)
Can be thought of as general message passing (Similar to email)
Can be used to implement RPC
Zubair Nabi 4: Concurrency April 18, 2013 17 / 22
![Page 65: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/65.jpg)
Concurrency without shared data
Only one thread can access any particular piece of data
Different threads can own distinct chunks of data
Ensure concurrency by allowing other threads to ask for operations to bedone on their behalf (Dynamic invocation)
Can be thought of as general message passing (Similar to email)
Can be used to implement RPC
Zubair Nabi 4: Concurrency April 18, 2013 17 / 22
![Page 66: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/66.jpg)
Advantages
Avoids race condition
Provides a flexible APII Batching: Batch X messages togetherI Scheduling: Choose the when, who, and which of message receiptI Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 67: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/67.jpg)
Advantages
Avoids race conditionProvides a flexible API
I Batching: Batch X messages togetherI Scheduling: Choose the when, who, and which of message receiptI Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 68: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/68.jpg)
Advantages
Avoids race conditionProvides a flexible API
I Batching: Batch X messages together
I Scheduling: Choose the when, who, and which of message receiptI Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 69: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/69.jpg)
Advantages
Avoids race conditionProvides a flexible API
I Batching: Batch X messages togetherI Scheduling: Choose the when, who, and which of message receipt
I Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 70: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/70.jpg)
Advantages
Avoids race conditionProvides a flexible API
I Batching: Batch X messages togetherI Scheduling: Choose the when, who, and which of message receiptI Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 71: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/71.jpg)
Advantages
Avoids race conditionProvides a flexible API
I Batching: Batch X messages togetherI Scheduling: Choose the when, who, and which of message receiptI Broadcast: Send messages to many recipients
Used as the basis for some languages, such as Linda, occam, and Erlang
Zubair Nabi 4: Concurrency April 18, 2013 18 / 22
![Page 72: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/72.jpg)
Erlang
Functional language from the mid 80s
Cheap, lightweight, language-level processes
Each variable is assigned only once (Immutable afterwards)
Explicit message passing via in order message delivery to local mailbox
Used to implement Facebook Chat
Zubair Nabi 4: Concurrency April 18, 2013 19 / 22
![Page 73: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/73.jpg)
Erlang
Functional language from the mid 80s
Cheap, lightweight, language-level processes
Each variable is assigned only once (Immutable afterwards)
Explicit message passing via in order message delivery to local mailbox
Used to implement Facebook Chat
Zubair Nabi 4: Concurrency April 18, 2013 19 / 22
![Page 74: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/74.jpg)
Erlang
Functional language from the mid 80s
Cheap, lightweight, language-level processes
Each variable is assigned only once (Immutable afterwards)
Explicit message passing via in order message delivery to local mailbox
Used to implement Facebook Chat
Zubair Nabi 4: Concurrency April 18, 2013 19 / 22
![Page 75: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/75.jpg)
Erlang
Functional language from the mid 80s
Cheap, lightweight, language-level processes
Each variable is assigned only once (Immutable afterwards)
Explicit message passing via in order message delivery to local mailbox
Used to implement Facebook Chat
Zubair Nabi 4: Concurrency April 18, 2013 19 / 22
![Page 76: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/76.jpg)
Erlang
Functional language from the mid 80s
Cheap, lightweight, language-level processes
Each variable is assigned only once (Immutable afterwards)
Explicit message passing via in order message delivery to local mailbox
Used to implement Facebook Chat
Zubair Nabi 4: Concurrency April 18, 2013 19 / 22
![Page 77: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/77.jpg)
Outline
1 Introduction
2 Concurrency in Practice
3 Liveness
4 Message passing
5 Transactions
Zubair Nabi 4: Concurrency April 18, 2013 20 / 22
![Page 78: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/78.jpg)
Transactions
Either executes correctly (commits) or has no effect at all
Properties:1 Atomicity: Either all or none of the operations are performed2 Consistency: A transaction transforms the system from one consistent
state to another3 Isolation: Each transaction gives the notion of sandboxing from the
concurrent effects of others4 Durability: The effects of committed transactions survive subsequent
system failures
Zubair Nabi 4: Concurrency April 18, 2013 21 / 22
![Page 79: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/79.jpg)
Transactions
Either executes correctly (commits) or has no effect at allProperties:
1 Atomicity: Either all or none of the operations are performed
2 Consistency: A transaction transforms the system from one consistentstate to another
3 Isolation: Each transaction gives the notion of sandboxing from theconcurrent effects of others
4 Durability: The effects of committed transactions survive subsequentsystem failures
Zubair Nabi 4: Concurrency April 18, 2013 21 / 22
![Page 80: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/80.jpg)
Transactions
Either executes correctly (commits) or has no effect at allProperties:
1 Atomicity: Either all or none of the operations are performed2 Consistency: A transaction transforms the system from one consistent
state to another
3 Isolation: Each transaction gives the notion of sandboxing from theconcurrent effects of others
4 Durability: The effects of committed transactions survive subsequentsystem failures
Zubair Nabi 4: Concurrency April 18, 2013 21 / 22
![Page 81: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/81.jpg)
Transactions
Either executes correctly (commits) or has no effect at allProperties:
1 Atomicity: Either all or none of the operations are performed2 Consistency: A transaction transforms the system from one consistent
state to another3 Isolation: Each transaction gives the notion of sandboxing from the
concurrent effects of others
4 Durability: The effects of committed transactions survive subsequentsystem failures
Zubair Nabi 4: Concurrency April 18, 2013 21 / 22
![Page 82: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/82.jpg)
Transactions
Either executes correctly (commits) or has no effect at allProperties:
1 Atomicity: Either all or none of the operations are performed2 Consistency: A transaction transforms the system from one consistent
state to another3 Isolation: Each transaction gives the notion of sandboxing from the
concurrent effects of others4 Durability: The effects of committed transactions survive subsequent
system failures
Zubair Nabi 4: Concurrency April 18, 2013 21 / 22
![Page 83: Topic 4: Concurrency](https://reader033.vdocument.in/reader033/viewer/2022051411/546c9207af79595d298b5080/html5/thumbnails/83.jpg)
References
Operating systems: Concurrent and distributed software design. JeanBacon and Tim Harris, Addison Wesley, 2003
Zubair Nabi 4: Concurrency April 18, 2013 22 / 22