SSC - Concurrency and Multi-threading Basic concepts

advertisement
SSC - Communication and Networking
SSC - Concurrency and Multi-threading
Basic concepts
Shan He
School for Computational Science
University 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
I
In the beginning, a computer was only capable of executing a
single program at a time.
Multitasking: Execute multiple programs “at the same time”.
I
I
I
I
Modern operating systems are all multitasking
Essentially shares the computing resources, such as CPU(s),
main memory, and I/O channels.
Single-CPU machine: only one task can be executed at one
time through time-slicing of the CPU.
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 Preemptive
I
Cooperative multitasking: processes control the CPU
I
I
I
I
I
Used in early multitasking operating systems, e.g., Win 3.1
Transferring control explicitly from one process to another
(co-routines) according to a cooperative model
Runtime support is simpler to implement
Programmers have to handle cooperation: bugs in processes
may lock the systems
Pre-emptive multi-tasking: Kernel schedules CPU to each
task
I
I
I
I
Used in modern multitasking operating systems
Operating system’s kernel manages (schedules) process’ access
to CPU
Pre-emption: an action performed by kernel: it forces a process
to abandon its running state even if it could safely execute
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
Cooperative multitasking
Pre-emptive multitasking
1
2
1
2
Switch()
Switch()
Interrupt
Interrupt
3
P2
Switch()
P1
4
4
P1
3
Switch()
Interrupt
P2
Interrupt
5
6
5
6
SSC - Communication and Networking
Concurrency: background and concepts
Preemptive Multitasking
Selection
Process
creation
Process
termination
ready
running
Preemption
waiting
Synchronisation
statement executed
by other processes
Synchronisation
statement executed
by the process
SSC - Communication and Networking
Concurrency: background and concepts
Concurrent programming: Two basic units
I
Process: a program
I
I
I
I
I
has a self-contained execution environment
has a complete, private set of basic run-time resources
has its own memory space
totally controlled by the operating system
Thread: also called lightweight process, which is a
dispatchable unit of work in a process.
I
I
I
has a definite beginning and an end
run inside a single process
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 than
creating 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 same
process (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
Main thread
Thread 2
Thread 3
Time
Begin
End
Suspended/Resumed
SSC - Communication and Networking
Concurrency: background and concepts
Difference between threads and processes
I
I
Processes are typically independent and might consist of
multiple threads
Processes have separate address spaces for code, data and
other resources, whereas threads share the address space of
the process that created it
I
I
I
A thread has its own stacks and registers
I
I
I
Threads are easier to create than processes
Multithreading requires careful programming
Stack: a reserved region of memory data that is operated in a
last-in-first-out manner
Register: a small amount of storage available as part of CPU
Processes use inter-process communication mechanisms
provided by the OS to communicate with other processes,
while threads can directly communicate with other threads in
the same process
SSC - Communication and Networking
Concurrency: background and concepts
Difference between threads and processes
Single thread process
Multithreaded process
Memory segments
Code
Register
Data
Other
resources
Code
Data
Other
resources
Stack
Register
Register
Register
Stack
Stack
Stack
SSC - Communication and Networking
Concurrency: background and concepts
Context switching
I
Context switching: a procedure of multi-threading for the
system to switch between threads running on the available
CPUs
I
A context is the minimal set of data used by this task that
must be saved to allow a task interruption at any point int
time
Data to be saved include:
I
I
I
I
Registers: one of a small set of data holding places in CPU,
which may hold a computer instruction, a storage address, or
any kind of data
Program counter: known as an instruction address register,
which is a small amount of fast memory that holds the address
of the instruction to be executed immediately after the current
one
other necessary operating system specific data
SSC - Communication and Networking
Concurrency: background and concepts
Context switching: how it works
I
I
A CPU timer: determines the end of the timeslice for each
thread, and signals at the end of the timeslice
Steps for context switching:
I
I
I
I
I
I
I
Step 1: interrupt current thread by the CPU timer
Step 2: The CPU saves all information required , e.g., register
and programme counter for the current thread onto a stack
Step 3: Move this information from the stack into a data
structure called context structure.
Step 4: A scheduler algorithm decides which thread to run next
Step 5: Run the new thread until a interrupt-return is called to
switch back to a previously executing thread
Step 6: Transfers all the information from the context structure
associated with the previously executing thread to the stack
Step 7: resume previous executing thread
SSC - Communication and Networking
Concurrency: background and concepts
Advantages of multi-threading
Advantages compared with multi-processing
I Improves the performance of the program by better usage of
system resources:
I
I
I
Share the same address space, less overhead for operating
system
Context-switching between threads is normally inexpensive
Better usage of CPU time, e.g., while one thread is blocked
(e.g., waiting for completion of an I/O operation), another
thread can use the CPU time to perform computations
I
Simpler program design: Control and communication between
threads 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 than
multi-processing, CPU still needs to save the register, program
counter etc. of the current thread, and load the same of the
next thread to execute.
I
More complex design: data shared and accessed by multiple
threads needs special attention
I
Increased resource consumption: CPU time, memory to keep
its local stack, and operating system resources to manage the
thread
SSC - Communication and Networking
Concurrency: background and concepts
Concurrent programming - three important concepts
I
Atomicity : An operation is said atomic when it cannot be
interrupted.
I
Visibility: If an action in one thread is visible to another
thread, then the result of that action can be observed by the
second thread.
I
Ordering: Since in multi-threading program, “the order of
execution is not guaranteed”, we need ordering constraints to
define what order should the actions in threads executed.
We shall see these concepts in Java examples in the next few
lectures.
Download