Concurrency Threads Address Spaces and Processes Andy Wang

  • Slides: 35
Download presentation
Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS

Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS 5765

Why Concurrency?

Why Concurrency?

Why Concurrency? n Allows multiple applications to run at the same time ¡ Analogy:

Why Concurrency? n Allows multiple applications to run at the same time ¡ Analogy: juggling

Benefits of Concurrency

Benefits of Concurrency

Benefits of Concurrency n n Can run multiple apps simultaneously Better resource utilization ¡

Benefits of Concurrency n n Can run multiple apps simultaneously Better resource utilization ¡ n Resources unused by one application can be used by the others Better average response time ¡ No need to wait for other applications to complete

Benefits of Concurrency n Better performance ¡ ¡ ¡ One application uses only the

Benefits of Concurrency n Better performance ¡ ¡ ¡ One application uses only the processor One application uses only the disk drive Completion time is shorter when running both concurrently than consecutively

Drawbacks of Concurrency

Drawbacks of Concurrency

Drawbacks of Concurrency n n Applications need to be protected from one another Additional

Drawbacks of Concurrency n n Applications need to be protected from one another Additional coordination mechanisms among applications Overhead to switch among applications Potential performance degradation when running too many applications

Thread n A sequential execution stream ¡ ¡ The smallest CPU scheduling unit Can

Thread n A sequential execution stream ¡ ¡ The smallest CPU scheduling unit Can be programmed as if it owns the entire CPU n ¡ Implication: an infinite loop within a thread won’t halt the system Illusion of multiple CPUs on a single-CPU machine

Thread States n n n Program counter Register values Execution stacks

Thread States n n n Program counter Register values Execution stacks

Thread Benefits n n Simplified programming model per thread Example: Microsoft Word ¡ ¡

Thread Benefits n n Simplified programming model per thread Example: Microsoft Word ¡ ¡ ¡ One thread for grammar check; one thread for spelling check; one thread formatting; and so on… Can be programmed independently Simplifies the development of large applications

Address Space n Contains all states necessary to run a program ¡ ¡ ¡

Address Space n Contains all states necessary to run a program ¡ ¡ ¡ Code, data, stack Program counter Register values Resources required by the program Status of the running program

Process n An address space + at least one thread of execution ¡ ¡

Process n An address space + at least one thread of execution ¡ ¡ n Address space offers protection among processes Threads offer concurrency A fundamental unit of computation

Process =? Program n n Program: a collection of statements in C or any

Process =? Program n n Program: a collection of statements in C or any programming languages Process: a running instance of the program, with additional states and system resources

Process >? Program n Two processes can run the same program ¡ The code

Process >? Program n Two processes can run the same program ¡ The code segment of two processes are the same program

Program >? Process n A program can create multiple processes ¡ Example: gcc, chrome

Program >? Process n A program can create multiple processes ¡ Example: gcc, chrome

Real-life Analogy?

Real-life Analogy?

Real-life Analogy? n n Program: a recipe Process: everything needed to cook ¡ n

Real-life Analogy? n n Program: a recipe Process: everything needed to cook ¡ n n e. g. , kitchen Two chefs can cook the same recipe in different kitchens One complex recipe can involve several chefs

Some Definitions n Uniprogramming: running one process at a time n Multiprogramming: running multiple

Some Definitions n Uniprogramming: running one process at a time n Multiprogramming: running multiple processes on a machine

Some Definitions n Multithreading: having multiple threads per address space n Multiprocessing: running programs

Some Definitions n Multithreading: having multiple threads per address space n Multiprocessing: running programs on a machine with multiple processors Multitasking: a single user can run multiple processes n

Classifications of OSes Single thread Multiple threads Single address Multiple space address spaces MS

Classifications of OSes Single thread Multiple threads Single address Multiple space address spaces MS DOS, Traditional Macintosh UNIX Embedded systems Windows, i. OS

Threads & Dispatching Loop n A thread owns a thread control block ¡ ¡

Threads & Dispatching Loop n A thread owns a thread control block ¡ ¡ Execution states of the thread The status of the thread n ¡ Running or sleeping Scheduling information of the thread n e. g. , priority

Dispatching Loop n Threads are run from a dispatching loop ¡ ¡ Can be

Dispatching Loop n Threads are run from a dispatching loop ¡ ¡ Can be thought as a per-CPU thread LOOP n Context switch n n n Run thread Save states Choose a new thread to run Load states from a different thread Scheduling

Simple? Not quite… n n n How does the dispatcher regain control after a

Simple? Not quite… n n n How does the dispatcher regain control after a thread starts running? What states should a thread save? How does the dispatcher choose the next thread?

How does the dispatcher regain control? n Two ways: 1. Internal events (“Sleeping Beauty”)

How does the dispatcher regain control? n Two ways: 1. Internal events (“Sleeping Beauty”) ¡ Yield—a thread gives up CPU voluntarily ¡ ¡ 2. A thread is waiting for I/O A thread is waiting for some other thread External events ¡ ¡ Interrupts—a complete disk request Timer—it’s like an alarm clock

What states should a thread save? n Anything that the next thread may trash

What states should a thread save? n Anything that the next thread may trash before a context switch ¡ ¡ ¡ Program counter Registers Changes in execution stack

How does the dispatcher choose the next thread? n n The dispatcher keeps a

How does the dispatcher choose the next thread? n n The dispatcher keeps a list of threads that are ready to run If no threads are ready ¡ n Dispatcher just loops If one thread is ready ¡ Easy

How does the dispatcher choose the next thread? n If more than one thread

How does the dispatcher choose the next thread? n If more than one thread are ready ¡ ¡ We choose the next thread based on the scheduling policies Examples n n n FIFO (first in, first out) LIFO (last in, first out) Priority-based policies

How does the dispatcher choose the next thread? n Additional control by the dispatcher

How does the dispatcher choose the next thread? n Additional control by the dispatcher on how to share the CPU ¡ Suppose we have threads Run to completion A B C Timeshare the CPU A B C A C Time

Per-thread States n Each thread can be in one of the three states 1.

Per-thread States n Each thread can be in one of the three states 1. 2. 3. Running: has the CPU Blocked: waiting for I/O or another thread Ready to run: on the ready list, waiting for the CPU

Per-thread State Diagram Running Scheduled Ready I/O request Yield, timer I/O complete Blocked

Per-thread State Diagram Running Scheduled Ready I/O request Yield, timer I/O complete Blocked

For Multi-core Machines n Each core has a dispatcher loop ¡ n Decide which

For Multi-core Machines n Each core has a dispatcher loop ¡ n Decide which thread will execute next One core has a global dispatcher loop ¡ Decide which core to execute a thread

Parallelism vs. Concurrency n Parallel computations ¡ n Computations can happen at the same

Parallelism vs. Concurrency n Parallel computations ¡ n Computations can happen at the same time on separate cores Concurrent computations ¡ One unit of computation does not depend on another unit of computation n n Can be done in parallel on multiple cores Can time share a single core

Amdahl’s Law n

Amdahl’s Law n

Amdahl’s Law n

Amdahl’s Law n