CO2101 — Concurrency and Threads Tom Ridge (tr61) 14th October 2019 tr61 Concurrency 14th October 2019 1 / 31
CO2101 — Concurrency and Threads
Tom Ridge (tr61)
14th October 2019
tr61 Concurrency 14th October 2019 1 / 31
Recap
Recap
The structure of Operating Systems
Concept of process: Code + Process Control Block
Scheduling algorithms
Reflections on how user interface interacts with OS
tr61 Concurrency 14th October 2019 2 / 31
Processes and Threads
Heavyweight processes
A process is an operating system abstraction to represent what isneeded to run a program.
It consists of
Sequential Program Execution State (includes state of CPUregisters); plusProtected resources: memory state, I/O state
(If we ignore threads...) NB Strictly sequential — i.e. noconcurrency.
tr61 Concurrency 14th October 2019 3 / 31
Processes and Threads
Heavyweight processes
A process is an operating system abstraction to represent what isneeded to run a program.
It consists of
Sequential Program Execution State (includes state of CPUregisters); plusProtected resources: memory state, I/O state
(If we ignore threads...) NB Strictly sequential — i.e. noconcurrency.
tr61 Concurrency 14th October 2019 3 / 31
Processes and Threads
Heavyweight processes
A process is an operating system abstraction to represent what isneeded to run a program.
It consists of
Sequential Program Execution State (includes state of CPUregisters); plusProtected resources: memory state, I/O state
(If we ignore threads...) NB Strictly sequential — i.e. noconcurrency.
tr61 Concurrency 14th October 2019 3 / 31
Processes and Threads PCB, context switching
PCB, address space, context switch
The current state of the process is held in the PCB (ProcessControl Block)
To multiplex several processes we need to allocate CPU time toprocesses using suitable scheduling policies.
preemptive: SRT, RRnon-preemptive: FCFS, SJF
Controlled access to non-CPU resources, e.g. memory, I/O.
tr61 Concurrency 14th October 2019 4 / 31
Processes and Threads PCB, context switching
PCB, address space, context switch
The current state of the process is held in the PCB (ProcessControl Block)
To multiplex several processes we need to allocate CPU time toprocesses using suitable scheduling policies.
preemptive: SRT, RRnon-preemptive: FCFS, SJF
Controlled access to non-CPU resources, e.g. memory, I/O.
tr61 Concurrency 14th October 2019 4 / 31
Processes and Threads PCB, context switching
PCB, address space, context switch
The current state of the process is held in the PCB (ProcessControl Block)
To multiplex several processes we need to allocate CPU time toprocesses using suitable scheduling policies.
preemptive: SRT, RRnon-preemptive: FCFS, SJF
Controlled access to non-CPU resources, e.g. memory, I/O.
tr61 Concurrency 14th October 2019 4 / 31
Processes and Threads PCB, context switching
PCB, address space, context switch
The current state of the process is held in the PCB (ProcessControl Block)
To multiplex several processes we need to allocate CPU time toprocesses using suitable scheduling policies.
preemptive: SRT, RRnon-preemptive: FCFS, SJF
Controlled access to non-CPU resources, e.g. memory, I/O.
tr61 Concurrency 14th October 2019 4 / 31
Processes and Threads Threads
Threads
A thread is an independent sequence of execution within a process
tr61 Concurrency 14th October 2019 5 / 31
Processes and Threads Threads
Concept of Thread (Cont’d)
A thread is similar to a process
Both have a single sequential flow of control with a start and endAt any time a thread has a single point of executionA thread has its own execution stack & program counterSometimes a thread is called a lightweight process
However, a thread differs from a process
A thread cannot exist on its own. It exists within a processUsually created and/or controlled by a processThreads can share a process’s resources, including memory and openfiles.
Many languages (including Java, and Python to some extent) supportthread programming directly.
tr61 Concurrency 14th October 2019 6 / 31
Processes and Threads Threads
Concept of Thread (Cont’d)
A thread is similar to a process
Both have a single sequential flow of control with a start and endAt any time a thread has a single point of executionA thread has its own execution stack & program counterSometimes a thread is called a lightweight process
However, a thread differs from a process
A thread cannot exist on its own. It exists within a processUsually created and/or controlled by a processThreads can share a process’s resources, including memory and openfiles.
Many languages (including Java, and Python to some extent) supportthread programming directly.
tr61 Concurrency 14th October 2019 6 / 31
Processes and Threads Threads
Concept of Thread (Cont’d)
A thread is similar to a process
Both have a single sequential flow of control with a start and endAt any time a thread has a single point of executionA thread has its own execution stack & program counterSometimes a thread is called a lightweight process
However, a thread differs from a process
A thread cannot exist on its own. It exists within a processUsually created and/or controlled by a processThreads can share a process’s resources, including memory and openfiles.
Many languages (including Java, and Python to some extent) supportthread programming directly.
tr61 Concurrency 14th October 2019 6 / 31
Processes and Threads Threads
Concept of Thread (Cont’d)
A thread is similar to a process
Both have a single sequential flow of control with a start and endAt any time a thread has a single point of executionA thread has its own execution stack & program counterSometimes a thread is called a lightweight process
However, a thread differs from a process
A thread cannot exist on its own. It exists within a processUsually created and/or controlled by a processThreads can share a process’s resources, including memory and openfiles.
Many languages (including Java, and Python to some extent) supportthread programming directly.
tr61 Concurrency 14th October 2019 6 / 31
Review of sequential programming
What is Sequential Programming?
Traditional activity of constructing a program containing one processusing a (sequential) computer language
The program is supposed to execute on a single processor architecture
tr61 Concurrency 14th October 2019 7 / 31
Review of sequential programming Single processor architecture
Single Processor Architecture
A CPU is linked to RAM and I/O devices by buses
Both program instructions and data are stored in RAM
The CPU repeatedly executes the cycle of
Fetching, decoding and executing the next instructionReferenced by the current value of program counter (PC)
Can execute just one instruction at any time
tr61 Concurrency 14th October 2019 8 / 31
Review of sequential programming Single processor architecture
Single Processor Architecture
A CPU is linked to RAM and I/O devices by buses
Both program instructions and data are stored in RAM
The CPU repeatedly executes the cycle of
Fetching, decoding and executing the next instructionReferenced by the current value of program counter (PC)
Can execute just one instruction at any time
tr61 Concurrency 14th October 2019 8 / 31
Review of sequential programming Single processor architecture
Single Processor Architecture
A CPU is linked to RAM and I/O devices by buses
Both program instructions and data are stored in RAM
The CPU repeatedly executes the cycle of
Fetching, decoding and executing the next instructionReferenced by the current value of program counter (PC)
Can execute just one instruction at any time
tr61 Concurrency 14th October 2019 8 / 31
Review of sequential programming Single processor architecture
Single Processor Architecture
A CPU is linked to RAM and I/O devices by buses
Both program instructions and data are stored in RAM
The CPU repeatedly executes the cycle of
Fetching, decoding and executing the next instructionReferenced by the current value of program counter (PC)
Can execute just one instruction at any time
tr61 Concurrency 14th October 2019 8 / 31
Review of sequential programming Single processor architecture
Program
Program Counter
Runtime DataProcessor
(CPU)
I/O
Devices
(RAM)Random Access Memory
Bus
tr61 Concurrency 14th October 2019 9 / 31
Review of sequential programming Execution of a sequential program
Executing a Sequential Program
The execution sequence is the sequence of values of PC
Deterministic: only one possible sequence of execution
A sequential program gives the system strict instructions on the orderof executing the statements in the program.
For example, the program below
P; Q; R;
tells the system to execute the statements in the order
P => Q => R
i.e, P must precede Q and Q must precede R
tr61 Concurrency 14th October 2019 10 / 31
Review of sequential programming Execution of a sequential program
Executing a Sequential Program
The execution sequence is the sequence of values of PC
Deterministic: only one possible sequence of execution
A sequential program gives the system strict instructions on the orderof executing the statements in the program.
For example, the program below
P; Q; R;
tells the system to execute the statements in the order
P => Q => R
i.e, P must precede Q and Q must precede R
tr61 Concurrency 14th October 2019 10 / 31
Review of sequential programming Execution of a sequential program
Executing a Sequential Program
The execution sequence is the sequence of values of PC
Deterministic: only one possible sequence of execution
A sequential program gives the system strict instructions on the orderof executing the statements in the program.
For example, the program below
P; Q; R;
tells the system to execute the statements in the order
P => Q => R
i.e, P must precede Q and Q must precede R
tr61 Concurrency 14th October 2019 10 / 31
Review of sequential programming Execution of a sequential program
Executing a Sequential Program
The execution sequence is the sequence of values of PC
Deterministic: only one possible sequence of execution
A sequential program gives the system strict instructions on the orderof executing the statements in the program.
For example, the program below
P; Q; R;
tells the system to execute the statements in the order
P => Q => R
i.e, P must precede Q and Q must precede R
tr61 Concurrency 14th October 2019 10 / 31
Review of sequential programming Sequential example
A Simple Example
A simple program consists of three statements
Java Statements
[ P ] x = 1;
[ Q ] y = x+1;
[ R ] x = y+2;
The final values of x and y depend only on the order of executing thestatements
tr61 Concurrency 14th October 2019 11 / 31
Review of sequential programming Sequential example
A Simple Example
A simple program consists of three statements
Java Statements
[ P ] x = 1;
[ Q ] y = x+1;
[ R ] x = y+2;
The final values of x and y depend only on the order of executing thestatements
tr61 Concurrency 14th October 2019 11 / 31
Review of sequential programming The system level view
System Level Execution
Each statement may be compiled into several machine instructions
1 P is treated as a single machine instruction
p: (x = 1) store 1 at the address of x
2 Q (y = x+1 )is broken into 3 machine instructions as follows:
q1: load the value of x into a CPU registerq2: increment the value in this register by 1q3: store the value in this register at the address of y
3 Similarly, R (x = y+2) is broken into 3 machine instructions
r1: load the value of y into a CPU registerr2: increment the value in this register by 2r3: store the result at the address of x
tr61 Concurrency 14th October 2019 12 / 31
Review of sequential programming The system level view
System Level Execution
Each statement may be compiled into several machine instructions
1 P is treated as a single machine instruction
p: (x = 1) store 1 at the address of x
2 Q (y = x+1 )is broken into 3 machine instructions as follows:
q1: load the value of x into a CPU registerq2: increment the value in this register by 1q3: store the value in this register at the address of y
3 Similarly, R (x = y+2) is broken into 3 machine instructions
r1: load the value of y into a CPU registerr2: increment the value in this register by 2r3: store the result at the address of x
tr61 Concurrency 14th October 2019 12 / 31
Review of sequential programming The system level view
System Level Execution
Each statement may be compiled into several machine instructions
1 P is treated as a single machine instruction
p: (x = 1) store 1 at the address of x
2 Q (y = x+1 )is broken into 3 machine instructions as follows:
q1: load the value of x into a CPU registerq2: increment the value in this register by 1q3: store the value in this register at the address of y
3 Similarly, R (x = y+2) is broken into 3 machine instructions
r1: load the value of y into a CPU registerr2: increment the value in this register by 2r3: store the result at the address of x
tr61 Concurrency 14th October 2019 12 / 31
Total ordering
Total Ordering
What is meant by P must precede Q?
Q can only begin after P finishes
The execution sequence at the program level
P; Q; R;
implies the execution sequence at the system level
p, q1, q2, q3, r1, r2, r3
So the final result is x = 4; y = 2;
Single threaded computation, no overlap in the execution of thestatements — Total Ordering
tr61 Concurrency 14th October 2019 13 / 31
Total ordering
Total Ordering
What is meant by P must precede Q?
Q can only begin after P finishes
The execution sequence at the program level
P; Q; R;
implies the execution sequence at the system level
p, q1, q2, q3, r1, r2, r3
So the final result is x = 4; y = 2;
Single threaded computation, no overlap in the execution of thestatements — Total Ordering
tr61 Concurrency 14th October 2019 13 / 31
Total ordering
Total Ordering
What is meant by P must precede Q?
Q can only begin after P finishes
The execution sequence at the program level
P; Q; R;
implies the execution sequence at the system level
p, q1, q2, q3, r1, r2, r3
So the final result is x = 4; y = 2;
Single threaded computation, no overlap in the execution of thestatements — Total Ordering
tr61 Concurrency 14th October 2019 13 / 31
Total ordering
Total Ordering
What is meant by P must precede Q?
Q can only begin after P finishes
The execution sequence at the program level
P; Q; R;
implies the execution sequence at the system level
p, q1, q2, q3, r1, r2, r3
So the final result is x = 4; y = 2;
Single threaded computation, no overlap in the execution of thestatements — Total Ordering
tr61 Concurrency 14th October 2019 13 / 31
Total ordering
Nature of Sequential Programming
Total ordering
Deterministic: same input results in same output
Sequential programming⇔ Finding a strict sequence of steps toachieve the desired end
This does not apply for many practical problems
tr61 Concurrency 14th October 2019 14 / 31
Total ordering
Nature of Sequential Programming
Total ordering
Deterministic: same input results in same output
Sequential programming⇔ Finding a strict sequence of steps toachieve the desired end
This does not apply for many practical problems
tr61 Concurrency 14th October 2019 14 / 31
Total ordering
Nature of Sequential Programming
Total ordering
Deterministic: same input results in same output
Sequential programming⇔ Finding a strict sequence of steps toachieve the desired end
This does not apply for many practical problems
tr61 Concurrency 14th October 2019 14 / 31
Total ordering
Nature of Sequential Programming
Total ordering
Deterministic: same input results in same output
Sequential programming⇔ Finding a strict sequence of steps toachieve the desired end
This does not apply for many practical problems
tr61 Concurrency 14th October 2019 14 / 31
Total ordering
Nature of Sequential Programming
Total ordering
Deterministic: same input results in same output
Sequential programming⇔ Finding a strict sequence of steps toachieve the desired end
This does not apply for many practical problems
tr61 Concurrency 14th October 2019 14 / 31
Non-sequential problem solving Calculating factors of a large number
Chinese General Problem
A Chinese princess wanted to marry one of her father’s generals. Eachof them had 1,000,000 soldiers. A general who could win her heartmust be young, handsome, and intelligent enough to:
Find all non-trivial factors of 368,788,194,383 in one month
How would you solve this problem if you were a general with 1,000,000soldiers under your command?
tr61 Concurrency 14th October 2019 15 / 31
Non-sequential problem solving Calculating factors of a large number
Clever General Bingxing’s Solution
1 Calculated 607280 = d√
368788194383 e
2 Gave each number in [2, 607280] to one of his 607,279 soldiers to checkwhether it is a factor
3 20 minutes later those soldiers who got 7, 17, 23, 119, 161, 257, 391, 1799,2737, 4369, 5911, 30583, 41377, 100487, and 524287 reported that theirnumber were factors
4 Find the remaining factors: 52684027769, 21693423199, 16034269321,3099060457, 2290609903, 1434973519, 943192313, 204996217, 134741759,84410207, 62390153, 12058601, 8912879, 3670009, and 703409
5 General Bingxing won the princess’s hand with these factors in 1 hour
NB: 368788194383 = 7× 17× 23× 257× 524287
tr61 Concurrency 14th October 2019 16 / 31
Non-sequential problem solving Calculating factors of a large number
Clever General Bingxing’s Solution
1 Calculated 607280 = d√
368788194383 e2 Gave each number in [2, 607280] to one of his 607,279 soldiers to check
whether it is a factor
3 20 minutes later those soldiers who got 7, 17, 23, 119, 161, 257, 391, 1799,2737, 4369, 5911, 30583, 41377, 100487, and 524287 reported that theirnumber were factors
4 Find the remaining factors: 52684027769, 21693423199, 16034269321,3099060457, 2290609903, 1434973519, 943192313, 204996217, 134741759,84410207, 62390153, 12058601, 8912879, 3670009, and 703409
5 General Bingxing won the princess’s hand with these factors in 1 hour
NB: 368788194383 = 7× 17× 23× 257× 524287
tr61 Concurrency 14th October 2019 16 / 31
Non-sequential problem solving Calculating factors of a large number
Clever General Bingxing’s Solution
1 Calculated 607280 = d√
368788194383 e2 Gave each number in [2, 607280] to one of his 607,279 soldiers to check
whether it is a factor
3 20 minutes later those soldiers who got 7, 17, 23, 119, 161, 257, 391, 1799,2737, 4369, 5911, 30583, 41377, 100487, and 524287 reported that theirnumber were factors
4 Find the remaining factors: 52684027769, 21693423199, 16034269321,3099060457, 2290609903, 1434973519, 943192313, 204996217, 134741759,84410207, 62390153, 12058601, 8912879, 3670009, and 703409
5 General Bingxing won the princess’s hand with these factors in 1 hour
NB: 368788194383 = 7× 17× 23× 257× 524287
tr61 Concurrency 14th October 2019 16 / 31
Non-sequential problem solving Calculating factors of a large number
Clever General Bingxing’s Solution
1 Calculated 607280 = d√
368788194383 e2 Gave each number in [2, 607280] to one of his 607,279 soldiers to check
whether it is a factor
3 20 minutes later those soldiers who got 7, 17, 23, 119, 161, 257, 391, 1799,2737, 4369, 5911, 30583, 41377, 100487, and 524287 reported that theirnumber were factors
4 Find the remaining factors: 52684027769, 21693423199, 16034269321,3099060457, 2290609903, 1434973519, 943192313, 204996217, 134741759,84410207, 62390153, 12058601, 8912879, 3670009, and 703409
5 General Bingxing won the princess’s hand with these factors in 1 hour
NB: 368788194383 = 7× 17× 23× 257× 524287
tr61 Concurrency 14th October 2019 16 / 31
Non-sequential problem solving Partial ordering of parallel solution
Parallel Computation and Partial Ordering
The operations carried out by Bingxing’s 607,279 soldiers were NOT ina total order.
They were carried out in parallel
Each individual soldier did his operations in sequence
The operations over the whole computation can be viewed as a partialorder
Many real world problems can be approached in this way
tr61 Concurrency 14th October 2019 17 / 31
Non-sequential problem solving Partial ordering of parallel solution
Parallel Computation and Partial Ordering
The operations carried out by Bingxing’s 607,279 soldiers were NOT ina total order.
They were carried out in parallel
Each individual soldier did his operations in sequence
The operations over the whole computation can be viewed as a partialorder
Many real world problems can be approached in this way
tr61 Concurrency 14th October 2019 17 / 31
Non-sequential problem solving Partial ordering of parallel solution
Parallel Computation and Partial Ordering
The operations carried out by Bingxing’s 607,279 soldiers were NOT ina total order.
They were carried out in parallel
Each individual soldier did his operations in sequence
The operations over the whole computation can be viewed as a partialorder
Many real world problems can be approached in this way
tr61 Concurrency 14th October 2019 17 / 31
Non-sequential problem solving Partial ordering of parallel solution
Parallel Computation and Partial Ordering
The operations carried out by Bingxing’s 607,279 soldiers were NOT ina total order.
They were carried out in parallel
Each individual soldier did his operations in sequence
The operations over the whole computation can be viewed as a partialorder
Many real world problems can be approached in this way
tr61 Concurrency 14th October 2019 17 / 31
Non-sequential problem solving Partial ordering of parallel solution
Parallel Computation and Partial Ordering
The operations carried out by Bingxing’s 607,279 soldiers were NOT ina total order.
They were carried out in parallel
Each individual soldier did his operations in sequence
The operations over the whole computation can be viewed as a partialorder
Many real world problems can be approached in this way
tr61 Concurrency 14th October 2019 17 / 31
Concurrent programming
What is Concurrent Programming
Constructing a program containing multiple processes/threads thatexecute in parallel
These processes may run on
A multi-processor systemA single processor system
Needs language support, e.g, Java Threads; POSIX thread support, etc.
tr61 Concurrency 14th October 2019 18 / 31
Concurrent programming
What is Concurrent Programming
Constructing a program containing multiple processes/threads thatexecute in parallel
These processes may run on
A multi-processor systemA single processor system
Needs language support, e.g, Java Threads; POSIX thread support, etc.
tr61 Concurrency 14th October 2019 18 / 31
Concurrent programming
What is Concurrent Programming
Constructing a program containing multiple processes/threads thatexecute in parallel
These processes may run on
A multi-processor systemA single processor system
Needs language support, e.g, Java Threads; POSIX thread support, etc.
tr61 Concurrency 14th October 2019 18 / 31
Concurrent programming
What is Concurrent Programming
Constructing a program containing multiple processes/threads thatexecute in parallel
These processes may run on
A multi-processor systemA single processor system
Needs language support, e.g, Java Threads; POSIX thread support, etc.
tr61 Concurrency 14th October 2019 18 / 31
Concurrent programming Benefits of concurrency
Why Concurrent Programming?
Improve efficiency in program execution using multi-CPU hardware(Chinese General Problem)
Improve CPU utilisation via multi-tasking on a uni-CPU system(operating systems)
Some applications are inherently non-deterministic and concurrent,e.g, embedded traffic lights controller
The order of program operations is determined by external events, e.g, asensor is triggered by a coming vehicleImpossible to predict the order of these events, e.g, a car from the northcomes first, and then one from the east, and so on
tr61 Concurrency 14th October 2019 19 / 31
Concurrent programming Benefits of concurrency
Why Concurrent Programming?
Improve efficiency in program execution using multi-CPU hardware(Chinese General Problem)
Improve CPU utilisation via multi-tasking on a uni-CPU system(operating systems)
Some applications are inherently non-deterministic and concurrent,e.g, embedded traffic lights controller
The order of program operations is determined by external events, e.g, asensor is triggered by a coming vehicleImpossible to predict the order of these events, e.g, a car from the northcomes first, and then one from the east, and so on
tr61 Concurrency 14th October 2019 19 / 31
Concurrent programming Benefits of concurrency
Why Concurrent Programming?
Improve efficiency in program execution using multi-CPU hardware(Chinese General Problem)
Improve CPU utilisation via multi-tasking on a uni-CPU system(operating systems)
Some applications are inherently non-deterministic and concurrent,e.g, embedded traffic lights controller
The order of program operations is determined by external events, e.g, asensor is triggered by a coming vehicleImpossible to predict the order of these events, e.g, a car from the northcomes first, and then one from the east, and so on
tr61 Concurrency 14th October 2019 19 / 31
Concurrent programming Benefits of concurrency
Why Concurrent Programming?
Improve efficiency in program execution using multi-CPU hardware(Chinese General Problem)
Improve CPU utilisation via multi-tasking on a uni-CPU system(operating systems)
Some applications are inherently non-deterministic and concurrent,e.g, embedded traffic lights controller
The order of program operations is determined by external events, e.g, asensor is triggered by a coming vehicleImpossible to predict the order of these events, e.g, a car from the northcomes first, and then one from the east, and so on
tr61 Concurrency 14th October 2019 19 / 31
Concurrent programming Representing concurrency in code
How to Represent Concurrent Program
Use COBEGIN/COEND to bracket the processes (this is pseudocode!)
G1; ...; Gk; [ calculate p=607279 ]COBEGINS1; [ is 2 a factor ? ]S2; [ is 3 a factor ? ]...;S(p-1); [ is 607279 a factor ? ]
COEND;R1; ...; Rl; [ report to the princess ]
The program ends only if all processes in COBEGIN/COEND terminate
The statements in COBEGIN/COEND may overlap in execution (butmight not!)
tr61 Concurrency 14th October 2019 20 / 31
Concurrent programming Representing concurrency in code
How to Represent Concurrent Program
Use COBEGIN/COEND to bracket the processes (this is pseudocode!)
G1; ...; Gk; [ calculate p=607279 ]COBEGINS1; [ is 2 a factor ? ]S2; [ is 3 a factor ? ]...;S(p-1); [ is 607279 a factor ? ]
COEND;R1; ...; Rl; [ report to the princess ]
The program ends only if all processes in COBEGIN/COEND terminate
The statements in COBEGIN/COEND may overlap in execution (butmight not!)
tr61 Concurrency 14th October 2019 20 / 31
Concurrent programming Representing concurrency in code
How to Represent Concurrent Program
Use COBEGIN/COEND to bracket the processes (this is pseudocode!)
G1; ...; Gk; [ calculate p=607279 ]COBEGINS1; [ is 2 a factor ? ]S2; [ is 3 a factor ? ]...;S(p-1); [ is 607279 a factor ? ]
COEND;R1; ...; Rl; [ report to the princess ]
The program ends only if all processes in COBEGIN/COEND terminate
The statements in COBEGIN/COEND may overlap in execution (butmight not!)
tr61 Concurrency 14th October 2019 20 / 31
Concurrent programming Concurrency with multi-processors
Multi-Processor System
A computer with multi-CPUs/cores is a Parallel Computer System
Parallel computation can be implemented on a parallel computersystem
If each task is computed by its own CPU, the computation is calledMaximum Parallel Computation
E.g, if a system has 607279 CPUs, each soldier’s task can be assigned toits own CPU
Maximum parallelism may not be always possible
tr61 Concurrency 14th October 2019 21 / 31
Concurrent programming Concurrency with multi-processors
Multi-Processor System
A computer with multi-CPUs/cores is a Parallel Computer System
Parallel computation can be implemented on a parallel computersystem
If each task is computed by its own CPU, the computation is calledMaximum Parallel Computation
E.g, if a system has 607279 CPUs, each soldier’s task can be assigned toits own CPU
Maximum parallelism may not be always possible
tr61 Concurrency 14th October 2019 21 / 31
Concurrent programming Concurrency with multi-processors
Multi-Processor System
A computer with multi-CPUs/cores is a Parallel Computer System
Parallel computation can be implemented on a parallel computersystem
If each task is computed by its own CPU, the computation is calledMaximum Parallel Computation
E.g, if a system has 607279 CPUs, each soldier’s task can be assigned toits own CPU
Maximum parallelism may not be always possible
tr61 Concurrency 14th October 2019 21 / 31
Concurrent programming Concurrency with multi-processors
Multi-Processor System
A computer with multi-CPUs/cores is a Parallel Computer System
Parallel computation can be implemented on a parallel computersystem
If each task is computed by its own CPU, the computation is calledMaximum Parallel Computation
E.g, if a system has 607279 CPUs, each soldier’s task can be assigned toits own CPU
Maximum parallelism may not be always possible
tr61 Concurrency 14th October 2019 21 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Concurrency with uni-processors
Uni-Processor Multi-Tasking System
A uni-CPU system can support multi-tasking/multi-thread
We can treat each soldier as a process or thread
Each process/thread has its own process counter
The program counter (PC) forks to produce many process/threadcounters, which later re-join
In each CPU cycle, a process is non-deterministically chosen and itsnext command is loaded and executed
There may be many different possible paths
This CPU sharing technique is interleaving.
tr61 Concurrency 14th October 2019 22 / 31
Concurrent programming Interleaving
Example of Interleaving
A concurrent program has two processes p1 and p2:
p1 has three statements A, B, and C; p2 has two S and T
PROCESS p1;BEGIN A; B; C END;
PROCESS p2;BEGIN S; T END;
BEGINCOBEGINp1; p2;
COENDEND
tr61 Concurrency 14th October 2019 23 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Possible interleavings
1 A ; B ; C ; S ; T
2 A ; B ; S ; C ; T
3 A ; S ; B ; C ; T
4 S ; A ; B ; C ; T
5 A ; B ; S ; T ; C
6 A ; S ; B ; T ; C
7 A ; S ; T ; B ; C
8 S ; T ; A ; B ; C
9 S ; A ; B ; T ; C
10 S ; A ; T ; B ; C
tr61 Concurrency 14th October 2019 24 / 31
Concurrent programming Interleaving
Back to first example
Each statement may be compiled into several machine instructions
1 P is treated as a single machine instruction
p: (x = 1) store 1 at the address of x
2 Q (y = x+1) is broken into 3 machine instructions as follows:
q1: load the value of x into a CPU registerq2: increment the value in this register by 1q3: store the value in this register at the address of y
3 Similarly, R (x = y+2) is broken into 3 machine instructions
r1: load the value of y into a CPU registerr2: increment the value in this register by 2r3: store the result at the address of x
tr61 Concurrency 14th October 2019 25 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?
Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; p
results in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?
Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; p
results in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; p
results in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; p
results in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; presults in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; presults in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Possible interleavings
(Assume that variables are automatically initialized to zero.)
Already seen that p; q1 ; q2 ; q3 ; r1 ; r2 ; r3results in x=4 ; y=2
What about p ; q1 ; r1 ; q2 ; r2 ; q3 ; r3 ?Result is x=2; y=2
And q1 ; r1 ; q2 ; r2 ; q3 ; r3 ; presults in x=1 ; y=1
etc.
The results depend on the order of interleaving.
tr61 Concurrency 14th October 2019 26 / 31
Concurrent programming Interleaving
Complexity of Interleaving
Given a concurrent program with processes p and q
PROCESS p;BEGIN S1; ...; Sm END;
PROCESS q;BEGIN T1; ...; Tn END;
BEGINCOBEGIN p; q COEND
END.
The number f(m,n) of interleavings is
the binomial coefficient
Cn+mm = Cn+m
n =(n+m)!
n!m!
The number f(m,n) grows very fast with m and n
tr61 Concurrency 14th October 2019 27 / 31
Concurrent programming Interleaving
Complexity of Interleaving
Given a concurrent program with processes p and q
PROCESS p;BEGIN S1; ...; Sm END;
PROCESS q;BEGIN T1; ...; Tn END;
BEGINCOBEGIN p; q COEND
END.
The number f(m,n) of interleavings is
the binomial coefficient
Cn+mm = Cn+m
n =(n+m)!
n!m!
The number f(m,n) grows very fast with m and n
tr61 Concurrency 14th October 2019 27 / 31
Concurrent programming Interleaving
Complexity of Interleaving
Given a concurrent program with processes p and q
PROCESS p;BEGIN S1; ...; Sm END;
PROCESS q;BEGIN T1; ...; Tn END;
BEGINCOBEGIN p; q COEND
END.
The number f(m,n) of interleavings is
the binomial coefficient
Cn+mm = Cn+m
n =(n+m)!
n!m!
The number f(m,n) grows very fast with m and n
tr61 Concurrency 14th October 2019 27 / 31
Concurrent programming Interleaving
n : 2 4 6 8 10 · · ·f(n,n) : 6 70 924 12870 184756 · · ·
tr61 Concurrency 14th October 2019 28 / 31
Concurrent programming Concurrency critical issues
Issues in Concurrent Programming
The concurrent processes must interact with each other in orderto share resources or exchange data
Synchronisation: when, how, and with what languageabstractions can we synchronise computation to eliminateunacceptable interleavings, and thus unacceptable outputs?
Distribution: how can we distribute processes among a numberof processors, and how can a process on one processor interactwith another process on a different processor.
tr61 Concurrency 14th October 2019 29 / 31
Concurrent programming Concurrency critical issues
Issues in Concurrent Programming
The concurrent processes must interact with each other in orderto share resources or exchange data
Synchronisation: when, how, and with what languageabstractions can we synchronise computation to eliminateunacceptable interleavings, and thus unacceptable outputs?
Distribution: how can we distribute processes among a numberof processors, and how can a process on one processor interactwith another process on a different processor.
tr61 Concurrency 14th October 2019 29 / 31
Concurrent programming Concurrency critical issues
Issues in Concurrent Programming
The concurrent processes must interact with each other in orderto share resources or exchange data
Synchronisation: when, how, and with what languageabstractions can we synchronise computation to eliminateunacceptable interleavings, and thus unacceptable outputs?
Distribution: how can we distribute processes among a numberof processors, and how can a process on one processor interactwith another process on a different processor.
tr61 Concurrency 14th October 2019 29 / 31
Concurrent programming Concurrency critical issues
Issues in Concurrent Programming
The concurrent processes must interact with each other in orderto share resources or exchange data
Synchronisation: when, how, and with what languageabstractions can we synchronise computation to eliminateunacceptable interleavings, and thus unacceptable outputs?
Distribution: how can we distribute processes among a numberof processors, and how can a process on one processor interactwith another process on a different processor.
tr61 Concurrency 14th October 2019 29 / 31
Concurrent programming Concurrency critical issues
Issues in Concurrent Programming
The concurrent processes must interact with each other in orderto share resources or exchange data
Synchronisation: when, how, and with what languageabstractions can we synchronise computation to eliminateunacceptable interleavings, and thus unacceptable outputs?
Distribution: how can we distribute processes among a numberof processors, and how can a process on one processor interactwith another process on a different processor.
tr61 Concurrency 14th October 2019 29 / 31
Summary
Summary
Processes, threads, context switching
Sequential programming, compared with
Concurrent programming
tr61 Concurrency 14th October 2019 30 / 31
Summary
Summary
Processes, threads, context switching
Sequential programming, compared with
Concurrent programming
tr61 Concurrency 14th October 2019 30 / 31
Summary
Summary
Processes, threads, context switching
Sequential programming, compared with
Concurrent programming
tr61 Concurrency 14th October 2019 30 / 31
Summary
Next time
Processes sharing resources
Critical regions, mutual exclusion
Algorithms for solving critical region issues
tr61 Concurrency 14th October 2019 31 / 31