SSC - Concurrency and Multi-threading Basic conceptsszh/teaching/ssc/... · Concurrency: background...
Transcript of SSC - Concurrency and Multi-threading Basic conceptsszh/teaching/ssc/... · Concurrency: background...
SSC - Communication and Networking
SSC - Concurrency and Multi-threadingBasic concepts
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Communication and Networking
Outline
Outline of Topics
Concurrency: background and concepts
SSC - Communication and Networking
Concurrency: background and concepts
Concurrency: history
I In the beginning, a computer was only capable of executing asingle program at a time.
I Multitasking: Execute multiple programs “at the same time”.I Modern operating systems are all multitaskingI Essentially shares the computing resources, such as CPU(s),
main memory, and I/O channels.I Single-CPU machine: only one task can be executed at one
time through time-slicing of the CPU.I Multi-CPU machine: tasks can be executed simultaneously,
distributed among or time-slicing the CPUs.
SSC - Communication and Networking
Concurrency: background and concepts
Multitasking: Cooperative vs PreemptiveI Cooperative multitasking: processes control the CPU
I Used in early multitasking operating systems, e.g., Win 3.1I Transferring control explicitly from one process to another
(co-routines) according to a cooperative modelI Runtime support is simpler to implementI Programmers have to handle cooperation: bugs in processes
may lock the systems
I Pre-emptive multi-tasking: Kernel schedules CPU to eachtask
I Used in modern multitasking operating systemsI Operating system’s kernel manages (schedules) process’ access
to CPUI Pre-emption: an action performed by kernel: it forces a process
to abandon its running state even if it could safely executeI Used time slicing mechanism: a process is suspend when a
specified amount of time has expired
SSC - Communication and Networking
Concurrency: background and concepts
Multitasking: Cooperative vs Preemptive
P1 P2
Switch()
Switch()
Switch()
Switch()
1
2
35
46
P1 P21
35
24
6
Interrupt Interrupt
InterruptInterrupt
Cooperative multitasking Pre-emptive multitasking
SSC - Communication and Networking
Concurrency: background and concepts
Preemptive Multitasking
ready
waiting
running
Selection
Preemption
Synchronisation statement executed
by the process
Synchronisation statement executed by other processes
Process creation
Process termination
SSC - Communication and Networking
Concurrency: background and concepts
Concurrent programming: Two basic units
I Process: a programI has a self-contained execution environmentI has a complete, private set of basic run-time resourcesI has its own memory spaceI totally controlled by the operating system
I Thread: also called lightweight process, which is adispatchable unit of work in a process.
I has a definite beginning and an endI run inside a single processI share the same address space, the resources allocated and the
environment of that process
SSC - Communication and Networking
Concurrency: background and concepts
More about threads
I Both processes and threads provide an execution environment,but creating a new thread requires fewer resources thancreating a new process.
I A process can be divided into multiple independent threads
I A process can be a single thread, which is called main thread.
I A standalone Java application starts with main thread (main() )
I This main thread can start new independent threads.
I Multithreading: executing multiple threads inside the sameprocess (program).
SSC - Communication and Networking
Concurrency: background and concepts
Concurrency: Two basic units
Single threaded process Multithreaded process
Thread execution
SSC - Communication and Networking
Concurrency: background and concepts
Main thread and threads
Time
Begin End Suspended/Resumed
Main thread
Thread 2
Thread 3
SSC - Communication and Networking
Concurrency: background and concepts
Difference between threads and processes
I Processes are typically independent and might consist ofmultiple threads
I Processes have separate address spaces for code, data andother resources, whereas threads share the address space ofthe process that created it
I Threads are easier to create than processesI Multithreading requires careful programming
I A thread has its own stacks and registersI Stack: a reserved region of memory data that is operated in a
last-in-first-out mannerI Register: a small amount of storage available as part of CPU
I Processes use inter-process communication mechanismsprovided by the OS to communicate with other processes,while threads can directly communicate with other threads inthe same process
SSC - Communication and Networking
Concurrency: background and concepts
Difference between threads and processes
Code DataOther
resources
Register Stack
Single thread process
Code DataOther
resources
Register
Stack
Multithreaded process
Stack Stack
Register Register
Memory segments
SSC - Communication and Networking
Concurrency: background and concepts
Context switching
I Context switching: a procedure of multi-threading for thesystem to switch between threads running on the availableCPUs
I A context is the minimal set of data used by this task thatmust be saved to allow a task interruption at any point inttime
I Data to be saved include:I Registers: one of a small set of data holding places in CPU,
which may hold a computer instruction, a storage address, orany kind of data
I Program counter: known as an instruction address register,which is a small amount of fast memory that holds the addressof the instruction to be executed immediately after the currentone
I other necessary operating system specific data
SSC - Communication and Networking
Concurrency: background and concepts
Context switching: how it works
I A CPU timer: determines the end of the timeslice for eachthread, and signals at the end of the timeslice
I Steps for context switching:I Step 1: interrupt current thread by the CPU timerI Step 2: The CPU saves all information required , e.g., register
and programme counter for the current thread onto a stackI Step 3: Move this information from the stack into a data
structure called context structure.I Step 4: A scheduler algorithm decides which thread to run nextI Step 5: Run the new thread until a interrupt-return is called to
switch back to a previously executing threadI Step 6: Transfers all the information from the context structure
associated with the previously executing thread to the stackI Step 7: resume previous executing thread
SSC - Communication and Networking
Concurrency: background and concepts
Advantages of multi-threading
Advantages compared with multi-processingI Improves the performance of the program by better usage of
system resources:I Share the same address space, less overhead for operating
systemI Context-switching between threads is normally inexpensiveI Better usage of CPU time, e.g., while one thread is blocked
(e.g., waiting for completion of an I/O operation), anotherthread can use the CPU time to perform computations
I Simpler program design: Control and communication betweenthreads is easy and inexpensive.
I More responsive programs
SSC - Communication and Networking
Concurrency: background and concepts
Disadvantages of multi-threading
Disadvantages/costs compared with single-threading
I Context switching overhead: even lighter thanmulti-processing, CPU still needs to save the register, programcounter etc. of the current thread, and load the same of thenext thread to execute.
I More complex design: data shared and accessed by multiplethreads needs special attention
I Increased resource consumption: CPU time, memory to keepits local stack, and operating system resources to manage thethread
SSC - Communication and Networking
Concurrency: background and concepts
Concurrent programming - three important concepts
I Atomicity : An operation is said atomic when it cannot beinterrupted.
I Visibility: If an action in one thread is visible to anotherthread, then the result of that action can be observed by thesecond thread.
I Ordering: Since in multi-threading program, “the order ofexecution is not guaranteed”, we need ordering constraints todefine what order should the actions in threads executed.
We shall see these concepts in Java examples in the next fewlectures.