+ All Categories
Home > Documents > Threads, SMP, and Microkernels

Threads, SMP, and Microkernels

Date post: 23-Feb-2016
Category:
Upload: lindsey
View: 49 times
Download: 0 times
Share this document with a friend
Description:
Threads, SMP, and Microkernels. Chapter 4. Outline. Threads Symmetric Multiprocessing (SMP) Microkernel Linux Threads. Process Characteristics. Unit of resource ownership - process is allocated: a virtual address space to hold the process image - PowerPoint PPT Presentation
Popular Tags:
55
Threads, SMP, and Microkernels Chapter 4
Transcript
Page 1: Threads, SMP, and Microkernels

Threads, SMP, and Microkernels

Chapter 4

Page 2: Threads, SMP, and Microkernels

2

Outline

Threads Symmetric Multiprocessing (SMP) Microkernel Linux Threads

Page 3: Threads, SMP, and Microkernels

3

Process Characteristics Unit of resource ownership - process is

allocated: a virtual address space to hold the process

image control of some resources (files, I/O devices...)

Unit of scheduling/execution - process is an execution path through one or more programs execution may be interleaved with other

process(es) the process has an execution state and a

dispatching priority

Page 4: Threads, SMP, and Microkernels

4

Process Characteristics – New Concept These two characteristics are treated

independently by some recent OS

The unit of dispatching is usually referred to a thread or a lightweight process

The unit of resource ownership is usually referred to as a process or task

Page 5: Threads, SMP, and Microkernels

5

Multithreading vs. Single threading Multithreading: when the OS supports

multiple threads of execution within a single process

Single threading: when the OS does not recognize the concept of thread

MS-DOS supports a single user process and a single thread

Traditional UNIX supports multiple user processes but only supports one thread per process

Solaris supports multiple threads

Page 6: Threads, SMP, and Microkernels

6

Threads and Processes

Page 7: Threads, SMP, and Microkernels

7

Processes Have a virtual address space which holds

the process image Protected access to processors, other

processes, files, and I/O resources Have process-specific context

ID State Other attributes

Page 8: Threads, SMP, and Microkernels

8

Threads What properties do threads have? Have an execution state (running, ready,

etc.) Save thread context when not running Have an execution stack and some per-

thread static storage for local variables Have access to the memory address space

and resources of its process all threads of a process share this when one thread alters a (non-private) memory

item, all other threads (of the process) sees that a file open with one thread, is available to others

Page 9: Threads, SMP, and Microkernels

9

Single Threaded and Multithreaded Process Models

Thread Control Block contains a register image, thread priority and thread state information

Page 10: Threads, SMP, and Microkernels

10

Benefits of Threads vs ProcessesTake less time to: create a new thread than a process terminate a thread than a process switch between two threads within the

same process communicate with each other (via shared

resources) without invoking the kernel

Page 11: Threads, SMP, and Microkernels

11

Thread use in a Single-User System

Foreground and background work Asynchronous processing Speed of execution Modular program structure

Page 12: Threads, SMP, and Microkernels

12

Applications of Threads Example: a file server on a LAN It needs to handle several file requests

over a short period Hence more efficient to create (and

destroy) a single thread for each request On a SMP machine: multiple threads can

possibly be executing simultaneously on different processors

Example 2: one thread displays menu and reads user input while the other thread executes user commands

Page 13: Threads, SMP, and Microkernels

13

Application Benefits of Threads

Consider an application that consists of several independent parts that do not need to run in sequence

Each part can be implemented as a thread Whenever one thread is blocked waiting for

an I/O, execution could possibly switch to another thread of the same application (instead of switching to another process) Thread switching is faster than process

switching

Page 14: Threads, SMP, and Microkernels

14

Benefits and Challenges of Threads

Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel

Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data (to be discussed later)

Page 15: Threads, SMP, and Microkernels

15

Example of inconsistent view 3 variables: A, B, C which are shared by thread T1 and

thread T2 T1 computes C = A+B T2 transfers amount X from A to B, e.g.,

T2 must do: A = A -X and B = B+X What are the possible execution scenarios? If T1 computes A+B after T2 has done A = A-X but

before B = B+X T1 will not obtain the correct result for C = A + B

Programmer’s responsibility to ensure program correctness Race conditions could be difficult to identify and debug

Page 16: Threads, SMP, and Microkernels

16

Threads States and Actions

Three key states: running, ready, blocked Several actions that affect all of the threads in a

process The OS must manage these at the process level.

They have no suspend state because all threads within the same process share the same address space

Suspending (i.e., swapping) a single thread involves suspending all threads of the same process (logically)

Termination of a process, terminates all threads within the process

Page 17: Threads, SMP, and Microkernels

17

Thread Operations

Operations associated with a change in thread state Spawn (create another thread) Block

Issue: will blocking a thread block other, or all, threads

Unblock Finish (thread)

Deallocate register context and stacks

Page 18: Threads, SMP, and Microkernels

18

Example: Remote Procedure Call

Consider: A program that performs two remote procedure

calls (RPCs) to two different hosts to obtain a combined result.

Page 19: Threads, SMP, and Microkernels

19

RPCUsing Single Thread

Page 20: Threads, SMP, and Microkernels

20

RPC Using One Thread per Server

Page 21: Threads, SMP, and Microkernels

21

Multithreading on a Uniprocessor

Page 22: Threads, SMP, and Microkernels

22

Adobe PageMaker

Page 23: Threads, SMP, and Microkernels

23

Categories of Thread Implementation

User Level Thread (ULT)

Kernel level Thread (KLT) also called: kernel-supported threads lightweight processes.

Page 24: Threads, SMP, and Microkernels

24

User-Level Threads (ULT) The kernel is not aware

of the existence of threads

All thread management is done by the application by using a thread library

Thread switching does not require kernel mode privileges (no mode switching)

Scheduling is application specific

Page 25: Threads, SMP, and Microkernels

25

Threads library

Contains code for: creating and destroying threads passing messages and data between threads scheduling thread execution saving and restoring thread contexts

Page 26: Threads, SMP, and Microkernels

26

Relationships between ULTThread and Process Scheduling and States

Page 27: Threads, SMP, and Microkernels

27

Kernel activity for ULTs The kernel is not aware of thread activity

but it is still managing process activity When a thread makes a system call, the

whole process will be blocked but for the thread library that thread is still

in the running state So thread states are independent of

process states

Page 28: Threads, SMP, and Microkernels

28

Advantages and inconveniences of ULT Advantages

Thread switching does not involve the kernel: no mode switching

Scheduling can be application specific: choose the best algorithm.

ULTs can run on any OS. Only needs a thread library

Inconveniences Many system calls are

blocking and the kernel blocks processes. So all threads within the process will be blocked

The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors.

Page 29: Threads, SMP, and Microkernels

29

Kernel-Level Threads (KLT) All thread management is

done by kernel No thread library but an

API to the kernel thread facility

Kernel maintains context information for the process and the threads

Switching between threads requires the kernel

Scheduling on a thread basis

Ex: Windows NT and OS/2

Page 30: Threads, SMP, and Microkernels

30

Advantages and inconveniences of KLT Advantages

the kernel can simultaneously schedule many threads of the same process on many processors

blocking is done on a thread level

kernel routines can be multithreaded

Inconveniences thread switching within

the same process involves the kernel. We have 2 mode switches per thread switch

this results in a slow down

Page 31: Threads, SMP, and Microkernels

31

Combined ULT/KLT Approaches Thread creation done

in the user space Bulk of scheduling

and synchronization of threads done in the user space

The programmer may adjust the number of KLTs

May combine the best of both approaches

Example is Solaris NOTE: modern threads

are “smart”

Page 32: Threads, SMP, and Microkernels

32

Thread and Process Operation Latencies: an ExampleOperation User-Level

ThreadsKernel-Level Threads

Processes

Null Fork 34 948 11,300

Signal-Wait 37 441 1,840

Page 33: Threads, SMP, and Microkernels

Review Quiz 4-1True or False?

1. Processes generally are faster than threads, because processes have all the resources.

2. Thread switching for user-level threads generally are faster than that of kernel-level threads.

3. Threads generally are more reliable than processes.Which of the following programs could be higher

performance using multiple threads?Program 1 …

read message 1 from server A; // blocking callread message 2 from server B; // blocking call compare message 1 and message 2….33

Page 34: Threads, SMP, and Microkernels

Review Quiz 4-1Program 2:

…. for (i = 0; i < 1000000; i++) {

list [i] = i; }

Program 3: … for (i = 1; i < 1000000; i++) {

list [i] = list [i – 1] + i; }

34

Page 35: Threads, SMP, and Microkernels

35

Outline

Threads Symmetric Multiprocessing (SMP) Microkernel Linux

Page 36: Threads, SMP, and Microkernels

36

Traditional View

Traditionally, the computer has been viewed as a sequential machine. A processor executes instructions one at a time in

sequence Each instruction is a sequence of operations

Two popular approaches to providing parallelism Symmetric MultiProcessors (SMPs) Clusters (ch 16)

Page 37: Threads, SMP, and Microkernels

37

Categories of Computer Systems Based on Instruction and data streams:

Single Instruction Single Data (SISD) stream Single processor executes a single instruction

stream to operate on data stored in a single memory

Single Instruction Multiple Data (SIMD) stream Each instruction (same instruction) is executed

on a different set of data by the different processors

Page 38: Threads, SMP, and Microkernels

38

Categories of Computer Systems

• Multiple Instruction Single Data (MISD) stream (Never implemented) A sequence of data is transmitted to a set of

processors, each of execute a different instruction sequence

Multiple Instruction Multiple Data (MIMD) A set of processors simultaneously execute different

instruction sequences on different data sets

Page 39: Threads, SMP, and Microkernels

39

Parallel Processor Architectures

Page 40: Threads, SMP, and Microkernels

40

Symmetric Multiprocessing Kernel can execute on any processor

Allowing portions of the kernel to execute in parallel

Typically each processor does self-scheduling from the pool of available process or threads

Page 41: Threads, SMP, and Microkernels

41

Typical SMP Organization

Page 42: Threads, SMP, and Microkernels

42

Multiprocessor OS Design Considerations

The key design issues include Simultaneous and concurrent processes or

threads Scheduling Synchronization Memory Management Reliability and Fault Tolerance

Page 43: Threads, SMP, and Microkernels

43

Windows SMP Support

Threads can run on any processor But an application can restrict affinity

Soft Affinity The dispatcher tries to assign a ready thread to the

same processor it last ran on. Why? This helps reuse data still in that processor’s memory

caches from the previous execution of the thread. Hard Affinity

An application restricts threads to certain processor

Page 44: Threads, SMP, and Microkernels

44

Outline

Threads Symmetric Multiprocessing (SMP) Microkernel Linux

Page 45: Threads, SMP, and Microkernels

45

Microkernel

A microkernel is a small OS core that provides the foundation for modular extensions.

Big question is how small must a kernel be to qualify as a microkernel Must drivers be in user space?

In theory, this approach provides a high degree of flexibility and modularity.

Page 46: Threads, SMP, and Microkernels

46

Kernel Architecture

Page 47: Threads, SMP, and Microkernels

47

Microkernel Design: Memory Management

Low-level memory management - Mapping each virtual page to a physical page frame Most memory management tasks occur in user

space

Page 48: Threads, SMP, and Microkernels

48

Microkernel Design:Interprocess Communication

Communication between processes or threads in a microkernel OS is via messages.

A message includes: A header that identifies the sending and

receiving process and A body that contains direct data, a pointer to a

block of data, or some control information about the process.

Page 49: Threads, SMP, and Microkernels

49

Microkernal Design:I/O and interrupt management

Within a microkernel it is possible to handle hardware interrupts as messages and to include I/O ports in address spaces. a particular user-level process is assigned to the

interrupt and the kernel maintains the mapping.

Page 50: Threads, SMP, and Microkernels

50

Benefits of aMicrokernel Organization

Uniform interfaces on requests made by a process.

Extensibility Flexibility Portability Reliability Distributed System Support Object Oriented Operating Systems

Page 51: Threads, SMP, and Microkernels

51

Outline

Threads Symmetric Multiprocessing (SMP) Microkernel Linux

Page 52: Threads, SMP, and Microkernels

52

Different Approaches to Processes

Differences between different OS’s support of processes include How processes are named Whether threads are provided How processes are represented How process resources are protected What mechanisms are used for inter-process

communication and synchronization How processes are related to each other

Page 53: Threads, SMP, and Microkernels

53

Linux Tasks

A process, or task, in Linux is represented by a task_struct data structure

This contains a number of categories including: State Scheduling information Identifiers Interprocess communication And others

Page 54: Threads, SMP, and Microkernels

54

Linux Process/Thread Model

Page 55: Threads, SMP, and Microkernels

55

Linux Threads

Traditional Unix: single thread Modern Unix: KLTs Linux: ULTs pthread (POSIX thread) libraries ULTs belonging to the same process are

mapped into kernel-level processes that share the same group ID.

Shared ID is used for resource sharing and to avoid context switching


Recommended