Concurrency Flashcards
What is concurrency?
The ability to perform multiple processes simultaneously, including the potential interaction between them
What part does the OS play in concurrency
It schedules processes
Give 2 reasons why we use concurrency
Allows programs to interact with other systems/ users
Makes use of multi-core systems
Multiple cores sare processes that the OS needs to run
Each process that the OS needs to run has its own memory space
Explain the structure of multi-threading
Begins by running main thread
Main thread consists of multiple child threads
When child thread starts running, main thread is free - it begins execution of the next child thread
How does memory allocation work with threads
Memory space allocated to a single process
All threads involved in that process share this memory space
Threads have their own local variables
What is a thread
Threads can be treated as lightweight processes. There are many threads within a single process.
Explain the structure of threads when a program is run
Main thread begins to run immediately
Main thread consists of many child threads
Once the first child thread begins to run, the main thread is free so it starts up the next child thread
How can we use sequential execution to reduce nondeterminism from multi-threaded execution
Map the multi-thread program to a sequential execution
Have all threads behave deterministically
Thus, entire program deterministic
How can multi-threading cause nondeterminism (when different outputs are produced for the same input)
If two child threads are at the same level, we would want them to run concurrently
OS scheduling can cause nondeterministic behaviour due to environmental conditions (e.g. memory availability, interrupts etc.)
This affects the concurrency of the two threads
Explain how the OS assists with the execution process of processes
The OS decides which cores execute which processes
Memory space is allocated per process that the OS needs to undertake
What is liveness
Ensuring code performs correctly in time
An acceptable time depends on the application
How do you synchronize a set of statements
Pass an object in as a parameter
All statements in the synchronized block will be synchronized
synchronized (this) { // everything in here is synchronized }
How do synchronized methods work
One synchronized method is executed from an object
All synchronized methods for the same object block suspend execution
Until the first thread is done with the object
What are race conditions
When two threads try to access/change data at the same time