Week 2 - Concurrency Flashcards
Concurrency
Sequential system: Where parts of a computation are executed to completion, one after each other.
Concurrent System: Is where one or more computations are executing at the same time.
Concurrent system is almost the same as a parallel system.
Concurrency issues
Multi-tasking operating systems, where many processes are running at once.
Individual applications like Web Servers, that could be processing many “requests” simultaneously.
Multicore processors where single application is running across more than one core.
Parallel computers and Distributed Systems in general.
Threads
Specific sequence of instructions defined by same program, or by section of it.
These instructions sequences can run in parallel or interleaved in an unpredictable way.
Processes
Processes have one or more threads.
Processes and Threads
Every process has at least one control flow with a single address space.
A process may have multiple control flows within the same address space.
Parallelism vs Multitasking
Threads may run on different cores making it truly parallel.
Multiple threads share the same “core” by multitasking.
Concurrent Programs
Use thread libraries.
For example occam, uses PAR for do in parallel, and SEQ to execute in sequence. (This is for code)
POSIX threads
POSIX stands for Portable Operating System Interface.
Low-level programming language, often used in the C language.
Create POSIX parent thread
When creating it calls pthread_create library, passing it a pointer of a function with code for new thread.
A parent can create any of number of threads, just as a child can create their own children.
Concurrency in Java
Java creates threads using libraries it doesn’t contain explicit parallel constructs.
But supports concurrency through modifiers like synchronized, volatile on declarations.
JAVA threads
Similar to POSIX, but uses OOP patterns.
run is defined as java.lang.Threads
Concurrent programs
Number of possible orderings grow massively with program size.
As a result it is harder to design and debug programs.
non-deterministic meaning different orders of execution may lead to different outcomes.
Interference
More serious case of non-determinism.
Programmer might have expected each thread increments variable c by 1.
Making c increase by 2.
Unpredictable behaviour, when concurrent threads adversely affect one another’s behaviour, is called interference.
Race Conditions
Race conditions is the name given to interference, they are the same thing.
Avoiding Interference
To avoid this race condition type is to make sure that threads never have any variables in common.
What happens with processes, it having different address spaces, and no variables shared.