KUKUM Real Time System Module #3 POSIX programming- Shm Lecture 4.
Real Time System KUKUM Last lecture- Summary Definition of Real Time System Types of RTS Slow/Fast...
-
Upload
frank-howard -
Category
Documents
-
view
227 -
download
0
description
Transcript of Real Time System KUKUM Last lecture- Summary Definition of Real Time System Types of RTS Slow/Fast...
Real Time System KUKUM
Last lecture- Summarybull Definition of Real Time Systembull Types of RTSbull SlowFast RTSbull What is OS amp its functionality Kernel RTOS
how is it different from OSndash Multitasking ndash pre-emptable (priority)ndash Process synchndash Interrupt latency Predictablendash Mask interruptndash Sys call
Real Time System KUKUM
Concurrency and Concurrent Programming
Real Time System KUKUM
Objectives
bull Able to explain thendash Concept of concurrencyndash Interaction between concurrent processesndash Identify pathological cases ( to be avoided)
bull Able to design and implement concurrent software
Real Time System KUKUM
Lecture contents
bull Concept of concurrencybull Abstraction of concurrent programmingbull Correctness of concurrent programbull Writing concurrent program
Real Time System KUKUM
Concept of Concurrency
In programming
There are two fundamental types of programming
Sequential parallel
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Concurrency and Concurrent Programming
Real Time System KUKUM
Objectives
bull Able to explain thendash Concept of concurrencyndash Interaction between concurrent processesndash Identify pathological cases ( to be avoided)
bull Able to design and implement concurrent software
Real Time System KUKUM
Lecture contents
bull Concept of concurrencybull Abstraction of concurrent programmingbull Correctness of concurrent programbull Writing concurrent program
Real Time System KUKUM
Concept of Concurrency
In programming
There are two fundamental types of programming
Sequential parallel
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Objectives
bull Able to explain thendash Concept of concurrencyndash Interaction between concurrent processesndash Identify pathological cases ( to be avoided)
bull Able to design and implement concurrent software
Real Time System KUKUM
Lecture contents
bull Concept of concurrencybull Abstraction of concurrent programmingbull Correctness of concurrent programbull Writing concurrent program
Real Time System KUKUM
Concept of Concurrency
In programming
There are two fundamental types of programming
Sequential parallel
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Lecture contents
bull Concept of concurrencybull Abstraction of concurrent programmingbull Correctness of concurrent programbull Writing concurrent program
Real Time System KUKUM
Concept of Concurrency
In programming
There are two fundamental types of programming
Sequential parallel
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Concept of Concurrency
In programming
There are two fundamental types of programming
Sequential parallel
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Sequential Programmingbull In SP processes are executed in order one
followed by anotherbull Analogy shopping in a supermarket
bull Buy meat then buy vegetables then buy drinks then pay at the counter
bull For a given input the computer always executes the same sequence instructions
bull Program is easier to write (logical)bull Most probably not suitable to solve real-time
problems
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Example of SP
function add_two_list( int1 int2 integer)beginsum= 0sum = int1sum = sum + int2return sumend add_two_list
A single sequence of
control
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Concurrent programming (CP)
bull In CP processes are executed in parallelbull Analogy shopping in supermarket
bull Mother buys meat father buys veg son buys drinks ( at once) then all meet by the pay counter
bull Harder to model and to programbull Each time the sequence of execution of
instructions is non deterministicbull Appropriate for time critical real time problems
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Conceptuallyhellipbull We study it by looking at the
ndash Examples abstraction and model of CPndash Classical problems which illustrate the concepts
bull Programming-wisendash Some languages have no support for
concurrency so we need other means to achive it eg OS support (UNIX system calls)
ndash Using lsquotaskrsquo in ADA or lsquothreadrsquo in C++
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Definition ndash concurrent programmingldquoConcurrent programmingrdquo is the term given to
programming notations and techniques for expressing potential parallellism and for solving the related sycnhronization and communication problemsrdquo
Eg x yz= 6015120
Where xy and z are all assigned their respective values concurrently( undeterministic which assignment is done first)
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Definition ndash Program amp Process(Ben-Arirsquos definition)
bull A concurrent program is a set of ldquo sequential programsrdquo which are executed in abstract parallelism
bull Each sequential program is called a process and the whole set of process is called the concurrent program
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Examples of concurrencyoverlapped IO and computation
Main
IO
Start IO
Wait for IO
End of IO
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
hellip examples of concurrencyMulti programming
bull A concurrent execution of several independent programs
bull Eg running excel word and email concurrently on a PC where the excel is calculating something a file is being edited and email is being constantly checked for mail arival
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
hellip examples of concurrencyMulti tasking
bull Solving a problem by decomposing it into several sub-tasks Examples
bull Sort algorithmsbull Summation of huge set of numberbull Computing binomial coefficient
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Why do we need concurrency
ndash Improve speed of execution o processesndash Different peripherals have different speedndash True to reality- real time behavior of problems
in everyday life
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Types of concurrency
bull Semi-concurrencyndash Two or more processes will run on the same
processor one at a time using and releasing the processor managed by the OS
bull True concurrencyndash Many processes run on several processors at the
same time (transputer environment)
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Try this outThis exercise is intended to get a feeling of the problem inherent in CPndash Assume you can write an SP program which produces an endless series
of the following sentence
ldquoA person with one watchknows what time it isa person with two watchesis never surerdquo
ndash Now write a pseudo-CP program to do the above using two processes where P1 prints out the first two lines and P2 the rest What is the output look like
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Abstraction of concurrent programming
TimingInterleaving
Atomicity of operation
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Abstraction
bull An abstraction is an idealizedsimplified model of a phenomenon The model can then be used to study a more complex system which uses the abstraction
bull Examplendash Human body organs and the various systemsndash Computer OS-gt instructions -gt electronicsndash Complex software system components and their
interconnections
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
hellipAbstractionbull To understand CP we need a number of
abstractions about processesndash Interleavingndash Timingndash Atomicity of operations in a process
bull We want to use these to identify the problems and also prove the correctness of a CP
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Interleavingbull For analysis of CP we say that the execution sequences of
concurrent processes are interleaved and each process has its own processor time
bull Between interleaving processes there can be contention and communicationndash Contention processes compete for resourcesndash Communication processes may need to exchange
informationbull In fact there are various scheduling mechanism used to
manage the interleaving order of processes To show that a CP is incorrect we must be able to show that there exist at least one bad interleaving sequence which does not meet the system specification
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
hellip Interleaving
bull A concurrent program must be correct under all kinds of interleaving
bull Furthermore fairness must be guaranteed ie The scheduler manages is such a way that eventually every instruction in each process must be executed
bull If it can be shown that a process is deferred forever in its execution under a sequence of interleaving then the process is said to be ldquostarvedrdquo
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Timingbull The interleaving abstraction also makes no assumption about absolute timing
between the execution of processesndash If process-P must finish before process-Q is started it is irrelevant if it finishes 1 second or 1
hour before
bull This is a weak specification and can be misleading But it is a necessary abstraction becausehellipndash Execution sequence are more appropriate in proving correctness rather than absolute timingndash Systems are always upgraded with newer and faster hardware and if correctness depends on
absolute timing we always need to recheck its correctness every time a new device with a new timing is installed
bull Of course in a real system the specification must include the absolute real time requirement otherwise the system may be theoretically correct but still does not meet its requirement
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Atomic Instructionsbull Interleaving of processes are done on
sequences of atomic (undivisible) instructions
bull The correctness of a CP depends on the level of atomicity used by the computer when processes interleaved
bull For example consider the following two processes which execute in parallel but at different atomic levels
bull Instruction levelbull Register level
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
hellip Atomic instructionsN Integer = 0 -- a global var
process P is --specification of process PBegin
N= N+1End P
process Q is --specification of process QBegin
N= N+1End Q
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Instruction levelProcess Instruction Value of N(Initially) 0P INC N 1Q INC N 2
Process Instruction Value of N(Initially) 0Q INC N 1p INC N 2
Result is correct since there are 2 processes each incrementingN by 1 so final value of N=2 whichever process executes first
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Reqister levelAt the register level instruction the statement N=N+1 is equivalent to the series of instructions as follow
LOAD Reg NADD Reg 1STORE N Reg
Where LOAD ADD and store are atomic instructions and N is the global
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Register level
Process Instruction N Reg(1) Reg(2)(Initially) 0 - -P LOAD Reg N 0 0 -Q LOAD Reg N 0 0 0P ADD Reg 1 0 1 0Q ADD Reg 1 0 1 1P STORE Reg N 1 1 1Q STORE Reg N 1 1 1
Result is not correct based on the specification in the requirement
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
By comparisonbull It shows that a different level of atomicity leads to
a wrong result when processed are interleavedbull Obviously there are tradeoffs between the level of
atomicity vs correctness and between the level of atomicity vs difficulty in implementation
bull Generally prefer a higher level of atomicity although need to consider speed
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-
Real Time System KUKUM
Concurrency
Implementation
C++ thread
OS support
Example
Defn
Types
Multi tasking
Multi programming
tech to parallelismSolve synch amp comm
True Conc
Semi ConcAbstraction
Atomicity
Interleaving
Timing
- Last lecture- Summary
- Concurrency and Concurrent Programming
- Objectives
- Lecture contents
- Concept of Concurrency
- Sequential Programming
- Example of SP
- Concurrent programming (CP)
- Conceptuallyhellip
- Definition ndash concurrent programming
- Definition ndash Program amp Process (Ben-Arirsquos definition)
- Examples of concurrency overlapped IO and computation
- hellip examples of concurrency Multi programming
- hellip examples of concurrency Multi tasking
- Why do we need concurrency
- Types of concurrency
- Try this out
- Abstraction of concurrent programming
- Abstraction
- hellipAbstraction
- Interleaving
- hellip Interleaving
- Timing
- Atomic Instructions
- hellip Atomic instructions
- Instruction level
- Reqister level
- Register level
- By comparison
- Slide 30
-