1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts...

62
1 Threads, SMP, and Microkernels Chapter 4

Transcript of 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts...

Page 1: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

1

Threads, SMP, and Microkernels

Chapter 4

Page 2: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

2

Focus and Subtopics

Focus: More advanced concepts related to process management : Resource ownership vs execution

Processes and threads Symmetric Multiprocessing Microkernels Discussions on threads for contemporary OS

Windows Solaris Linux

Page 3: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

3

Characteristics that embody a Process Resource ownership - process includes a

virtual address space to hold the process image. Process are allocated resources from time to time.

Scheduling/execution- Process execution follows an execution path that may be interleaved with other processes

These two characteristics are treated independently by the operating system

Page 4: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

4

Process

Dispatching is referred to as a thread or lightweight process

Resource of ownership is referred to as a process or task

Page 5: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

5

Multithreading

The ability of an OS to support multiple threads of execution

There are 4 different approaches to usage of threads. One Process, one thread of execution (thread as

an entity is not recognize – thread of execution is within process)

One Process, multiple threads Multiple Process, one thread per process Multiple Process, multiple threads.

Page 6: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

6

Multithreading

Most operating systems support multiple threads of execution within a single process: MS-DOS only supports a single thread Java runtime environment is a one process

environment which supports multiple threads. UNIX supports multiple user processes but only

supports one thread per process Windows, Solaris, Linux, Mach, and OS/2 support

multiple threads per process

Page 7: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

7

MSDOSJava Virtual Environment

UNIX

Windows, Solaris,

Mach, OS/2

Page 8: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

8

A Process within multithreaded environment Have a virtual address space which holds the

process image Protected access to

Processors other processes (interprocess communication) Files I/O resources (devices and channels)

Page 9: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

9

Threads - within a process :

An execution state (running, ready, etc.) A saved thread context when not running

(can view it as an independent program counter within a process)

Has an execution stack Some per-thread static storage for local

variables Access to the memory and resources of its

process, shared by all threads of a process.

Page 10: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

10

Code/Text

Data

Code/Text and Data (share by all threads)

Thread1 Thread2 Thread3

Single Thread vs multiple threads

No thread

One process, Multiple threads

• Thread keeps different PCs because they are running different parts of the code in the process

• All threads share state and resources of the process

Page 11: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

11

Benefits of Threads

Takes less time to create a new thread than a process

Less time to terminate a thread than a process Less time to switch between two threads within the

same process Since threads within the same process share

memory and files, they can communicate with each other without invoking the kernel

Page 12: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

12

Example benefits of usage of threads An application have multiple functionality:

Use threads for each functionality – better than a single process with many functionality

A file server: Each new file request means a thread is created

for file management for each request Many threads can be created and destroyed. In multi CPU machine, a CPU can handle one

thread. Resources are shared

Page 13: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

13

Example Uses of Threads

In a Single-User Multiprocessing System Foreground and background work:

Each thread perform a specific function Asynchronous processing:

Each asynchronous element implemented as thread – backup

Speed of execution: Simultaneous thread execution

Modular program structure: Easier to design

Page 14: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

14

Actions that effect all threads

Suspending a process involves suspending all threads of the process since all threads share the same address space

Termination of a process, terminates all threads within the process

Page 15: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

15

Thread Functionality

Thread States: Threads have execution states like process

Thread Synchronization Threads may need to synchronize with one

another

Page 16: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

16

Thread States Key States for thread

Running Ready Blocked

What about Suspend? Does not make sense. This is a process concept. If a process is swapped out, all its threads are also swapped out.

Basic thread operations associated with a change in thread state Spawn

A spawned process with threads also spawn a process with threads Block

A thread that is waiting for an event is blocked, allowing OS to pick up another thread to run.

Unblock When a thread that is waiting for an event had the event occurs, it is

unblocked and moved to ready queue. Finish

When a thread completes, OS deallocate its register context and stacks

Page 17: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

17

Remote Procedure Call Using Process (Single Thread of execution)

Page 18: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

18

Benefit of threads:Remote Procedure Call Using 2 Threads

Page 19: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

19

Benefit of threads:Multithreading

Page 20: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

20

Thread Synchronization

Threads share same address space (Code, data etc)

If a thread alter something in the address space, it will effect other threads.

Thus the need to synchronize activities of various threads

Issues in process synchronization is the same in thread synchronization

Page 21: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

21

Example use of thread inAdobe PageMaker• Writing, design, production for desktop

publishing

• Thread structure for Adobe Pagemaker in OS/2 operating System

• Three active threads:

• Event-handling

• Screen-redraw

• Service

• Each thread perform certain functions

• For synchronization between threads, the software use message passing.

Page 22: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

22

Threads Implementation

User Level threads (ULT) Kernel-level threads (KLT)

or kernel supported threads or lightweight processes

Page 23: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

23

User-Level Threads

All thread management is done by the application

The kernel is not aware of the existence of threads

Application can use thread library to Create thread Destroy thread Message passing between

thread Save/restore thread context

etc.

Process

(a) Pure User-level

Page 24: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

24

User-Level Threads

All the above takes place within the process, within user address space. As far as the kernel is concerned, it is scheduling a process .

An application (a process) can start with a single thread, at any time another thread can be spawned using the utility in the thread library. Thread library is responsible to create approriate thread data structure for the new thread, pass control to ready thread in the process.

Page 25: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

25

User-Level Threads

Page 26: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

26

Process B Running

Thread 2 Running

Process B Blocked

Thread 2 percieved as running

Process B Ready

Thread 2 percieved as running

Process B Running Again

Thread 1 runningThread 2 blocked

Page 27: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

27

Kernel-Level Threads Windows is an example of this

approach Kernel maintains context

information for the process and the threads

In this case, thread is managed by the OS kernel. There is no kernel management code in the application itself.

Scheduling is done on a thread basis ie: kernel schedule thread as well as process.

Process

Page 28: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

28

Kernel-Level Threads

Page 29: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

29

User Level thread vs Kernel Level Thread : Advantages and disadvantages

User Level Thread Kernel Level Thread

Advantages thread switching does not require switching to kernel mode.

scheduling is based on application

program of this type can run on any OS.

kernel can schedule multiple thread from the same process on different processor.

if a thread is blocked, kernel can schedule another thread of the same process.

kernel routine itself can be multithreaded.

Disdvantages a system call cause all thread in the process to be blocked.

the whole process is assigned to one processor, thus threads executes alternately.

switching thread require mode switch to kernel mode. This means it is slower due to overheads..

Page 30: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

30

Comparing ULT, KLT and Processes

Time to create, schedule, execute and complete in microseconds

Comparison in VAX Running UNIX-Like OS

Page 31: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

31

Combined Approaches

Example is Solaris thread creation is done in user

space scheduling and synchronization

in user space multiple ULT combined into a

number of KLT multiple threads within the same

application can run on multiple processors.

a thread that make a system call can be blocked, but the whole process cannot be blocked.

Page 32: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

32

Relationship Between Threads and Processes

Page 33: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

33

Other Thread Implementations Multiple process Multiple threads :

multiple processes running and each process have many threads. The threads can move from one process to another process.

There is one OS using this implementation, called TRIX. TRIX uses concepts of domain and threads.

Domain consists of address space and ports which messages can be sent and received between domain.

A thread is as the normal definition. Multiple threads can be executing in a single domain. But a thread from a domain can also execute in another domain. Thus the multiple domain multiple thread.

Kernel manages different address space related to the domain independently. There is no overhead in process creation when moving thread from one domain to another.

Multiple process Single threads : Here a thread can move among processes addresses spaces. An

OS called clouds is using this method. The kernel of clouds is called Ra. The processes can also be on different machines.

Page 34: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

34

Categories of Computer Systems 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 is executed on a different set of

data by the different processors

Page 35: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

35

Categories of Computer Systems Multiple Instruction Single Data (MISD) stream

A sequence of data is transmitted to a set of processors, each of which executes a different instruction sequence. Never implemented

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

instruction sequences on different data sets

Page 36: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

36

Page 37: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

37

Symmetric Multiprocessing

Kernel can execute on any processor Typically each processor does self-

scheduling form the pool of available process or threads

Page 38: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

38

Page 39: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

39

Multiprocessor Operating System Design Considerations Simultaneous concurrent processes or

threads Scheduling Synchronization Memory management Reliability and fault tolerance

Page 40: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

40

Microkernels

Small operating system core Contains only essential core operating systems

functions Many services traditionally included in the operating

system are now external subsystems Device drivers File systems Virtual memory manager Windowing system Security services

Page 41: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

41

Page 42: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

42

Benefits of a Microkernel Organization Uniform interface on request made by a process

Don’t distinguish between kernel-level and user-level services

All services are provided by means of message passing Extensibility

Allows the addition of new services Flexibility

New features added Existing features can be subtracted

Page 43: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

43

Benefits of a Microkernel Organization Portability

Changes needed to port the system to a new processor is changed in the microkernel - not in the other services

Reliability Modular design Small microkernel can be rigorously tested

Page 44: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

44

Benefits of Microkernel Organization Distributed system support

Message are sent without knowing what the target machine is

Object-oriented operating system Components are objects with clearly defined

interfaces that can be interconnected to form software

Page 45: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

45

Microkernel Design – Functions to include in Kernel Low-level memory management Interprocess communication I/O and interrupt management

Page 46: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

46

Microkernel Design - Function1. Low-level memory management

Mapping each virtual page to a physical page frame

Page 47: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

47

Microkernel Design

2. Interprocess communication Using Message –> header + body Port –> queue of messages

3. I/O and interrupt management Handle hardware interrupt using messages Microkernel recognizes interrupt, generate

messages for user-level processes to cater foir the interrupt.

Page 48: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

48

How OS Processes are supported How processes are named Are threads being used How are process represented How are process protected How IPC and synchronization are done How processes are related to each other

Page 49: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

49

Microsoft Windows Processes Implemented as objects An executable process may contain one or

more threads Both processes and thread objects have

built-in synchronization capabilities

Page 50: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

50

Page 51: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

51

Windows Process Object

Page 52: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

52

Windows Thread Object

Page 53: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

53

Windows 2000Thread States Ready Standby Running Waiting Transition Terminated

Page 54: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

54

Page 55: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

55

Solaris

Process includes the user’s address space, stack, and process control block

User-level threads Lightweight processes (LWP) Kernel threads

Page 56: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

56

Page 57: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

57

Page 58: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

58

Solaris Lightweight Data Structure Identifier Priority Signal mask Saved values of user-level registers Kernel stack Resource usage and profiling data Pointer to the corresponding kernel thread Pointer to the process structure

Page 59: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

59

Page 60: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

60

Linux Task Data Structure

State Scheduling information Identifiers Interprocess communication Links Times and timers File system Address space Processor-specific context

Page 61: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

61

Linux States of a Process

Running Interruptable Uninterruptable Stopped Zombie

Page 62: 1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.

62