<<

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 In the beginning, a computer was only capable of executing a single program at a time. I Multitasking: Execute multiple programs “at the same time”.

I Modern operating systems are all multitasking I Essentially shares the computing resources, such as CPU(s), main memory, and I/O channels. I Single-CPU machine: only one 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 Preemptive

I : processes control the CPU

I Used in early multitasking operating systems, e.g., Win 3.1 I Transferring control explicitly from one to another (co-routines) according to a cooperative model I Runtime support is simpler to implement I have to cooperation: bugs in processes may the systems I Pre-emptive multi-tasking: Kernel schedules CPU to each task

I Used in modern multitasking operating systems I ’s kernel manages (schedules) process’ access to CPU I Pre-emption: an action performed by kernel: it forces a process to abandon its running even if it could safely execute I 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 3

Switch() 3

4 4 P1 P2 P1 P2 Interrupt

Switch() Interrupt

5

6

5 6 SSC - Communication and Networking Concurrency: background and concepts

Preemptive Multitasking

Selection Process Process creation termination ready running

Preemption

waiting Synchronisation Synchronisation statement executed statement executed by other processes by the process SSC - Communication and Networking Concurrency: background and concepts

Concurrent programming: Two basic units

I Process: a program

I has a self-contained environment I has a complete, private set of basic run-time resources I has its own memory space I totally controlled by the operating system I Thread: also called lightweight process, which is a dispatchable unit of work in a process.

I has a definite beginning and an end I run inside a single process I share the same , 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 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 Processes are typically independent and might consist of multiple threads I Processes have separate address spaces for code, data and other resources, whereas threads share the address space of the process that created it

I Threads are easier to create than processes I Multithreading requires careful programming I A thread has its own stacks and registers

I Stack: a reserved region of memory data that is operated in a last-in-first-out manner I Register: a small amount of storage available as part of CPU

I 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 Other Other Code Data Code Data resources resources

Register Register Register

Stack Stack Stack SSC - Communication and Networking Concurrency: background and concepts

Context switching

I 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 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, or any kind of data I : 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 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 each thread, and signals at the end of the timeslice I Steps for context switching:

I Step 1: interrupt current thread by the CPU timer I Step 2: The CPU saves all information required , e.g., register and programme counter for the current thread onto a stack I 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 next I Step 5: Run the new thread until a interrupt-return is called to switch back to a previously executing thread I Step 6: Transfers all the information from the context structure associated with the previously executing thread to the stack I 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 Share the same address space, less overhead for operating system I Context-switching between threads is normally inexpensive I 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 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.