+ All Categories
Home > Documents > L04 Threads

L04 Threads

Date post: 17-Jul-2016
Category:
Upload: crystalcry
View: 28 times
Download: 2 times
Share this document with a friend
Description:
Operating Systems Concepts Threads
52
Lecture 04 Threads Dr. Muhammed J. Al-Muhammed Operating Systems: Internals and Design Principles 1 Operating Systems
Transcript
Page 1: L04 Threads

Lecture 04Threads

Dr. Muhammed J. Al-Muhammed

Operating

Systems:Internals

and Design

Principles

1Operating Systems

Page 2: L04 Threads

Operating Systems:Internals and Design Principles

The basic idea is that the several components in any complex system will perform particular subfunctions that contribute to the overall function.

—THE SCIENCES OF THE ARTIFICIAL,

Herbert Simon2Operating Systems

Page 3: L04 Threads

Processes and Threads

Resource Ownership

Process includes a virtual address space to hold the process image the OS performs a

protection function to prevent unwanted interference between processes with respect to resources

Scheduling/Execution

∗Processes have two characteristics:

Follows an execution path that may be interleaved with other processes a process has an

execution state (Running, Ready, etc.) and a dispatching priority and is scheduled and dispatched by the OS3

Operating Systems

Page 4: L04 Threads

Processes and Threads

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

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

Multithreading - The ability of an OS to support multiple, concurrent paths of execution within a single process

4Operating Systems

Page 5: L04 Threads

Single Threaded Approaches

A single thread of execution per process, in which the concept of a thread is not recognized, is referred to as a single-threaded approach

MS-DOS is an example

5Operating Systems

Page 6: L04 Threads

Multithreaded Approaches

The right half of Figure 4.1 depicts multithreaded approaches

A Java run-time environment is an example of a system of one process with multiple threads

6Operating Systems

Page 7: L04 Threads

ProcessesThe unit of resource allocation and a unit of

protectionA virtual address space that holds the process

imageProtected access to:

processors other processes files I/O resources7

Operating Systems

Page 8: L04 Threads

One or More Threads

in a Process

• A thread execution state (Running, Ready, etc.)

• saved thread context when not running

• an execution stack• some per-thread static storage for local

variables• access to the memory and resources of

its process (all threads of a process share this)

Each thread has:

8Operating Systems

Page 9: L04 Threads

Threads vs. Processes

9Operating Systems

Contains registers values, priority, and thread related data.

• All threads of process share the resources and state of that process• if a thread alters data item in memory all can see the change. Example of

a thread opens a file with read privilege all other threads can read the file

Page 10: L04 Threads

Benefits of Threads

Takes less time to

create a new thread than a

process (studies show creation of a thread ten

times faster)

Less time to terminate a

thread than a process

Switching between two threads takes less time than

switching between

processes

Threads enhance

efficiency in communication

between programs:

communication between

processes requires kernel

intervention while

communication between

threads does not need

kernel invocation.10

Operating Systems

The key benefits of threads derive from the performance implications

Page 11: L04 Threads

Thread Use in a Single-User System

Foreground and background work: for example in a spreadsheet program, one thread could display menus and read user input, while another thread executes user commands and updates the spreadsheet

Asynchronous processing: For example, as a protection against power failure, one can design a word processor to write its RAM buffer to disk once every minute.

Speed of execution Modular program structure11

Operating Systems

Page 12: L04 Threads

Threads

Hence, most of the state information dealing with execution is maintained in thread-level data structures

Several actions may affect all threads

In an OS that supports threads, scheduling and dispatching is done on a thread basis

Suspending a process involves suspending all threads of the process: recall suspension involves swapping process from memory.

Termination of a process terminates all threads within the process

12Operating Systems

Page 13: L04 Threads

Thread Execution States

The key states for a thread are:

Running Ready Blocked

It does not make sense for a thread to have suspend states. Why? (see below panel for answer).

Thread operations associated with a change in thread state are:

Spawn Block Unblock Finish

13Operating Systems

Page 14: L04 Threads

RPC Using Single Thread

14Operating Systems

Page 15: L04 Threads

RPC Using One Thread per Server

15Operating Systems

Page 16: L04 Threads

Thread Synchronization

It is necessary to synchronize the activities of the various threads

all threads of a process share the same address space and other resources

any alteration of a resource by one thread affects the other threads in the same process16

Operating Systems

Page 17: L04 Threads

Types of Threads

User Level Thread (ULT) Kernel level

Thread (KLT)

17Operating Systems

Page 18: L04 Threads

User-Level Threads (ULTs)

All thread management is done by the application

The kernel is not aware of the existence of threads

18Operating Systems

Page 19: L04 Threads

Relationships Between ULTStates and Process States

Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States

19Operating Systems

Page 20: L04 Threads

Advantages of ULTs

Thread switching does not require kernel mode privileges

Scheduling can be application specific

ULTs can run on any OS

20Operating Systems

Page 21: L04 Threads

Disadvantages of ULTs

In a typical OS many system calls are blocking

as a result, when a ULT executes a system call, not only is that thread blocked, but all of the threads within the process are blocked

In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing (multi-processor or multi-core)

21Operating Systems

Page 22: L04 Threads

Overcoming ULT Disadvantages

(1) Jacketing -- converts a blocking system call into a non-blocking system call

(2) Writing an application as multiple processes rather than multiple threads

22Operating Systems

Page 23: L04 Threads

Kernel-Level Threads (KLTs)

Thread management is done by the kernel

no thread management is done by the application API to kernel

thread facilityMS Windows is

an example of this approach

23Operating Systems

Page 24: L04 Threads

Advantages of KLTs The kernel can simultaneously schedule

multiple threads from the same process on multiple processors

If one thread in a process is blocked, the kernel can schedule another thread of the same process

Kernel routines can be multithreaded24

Operating Systems

Page 25: L04 Threads

Disadvantage of KLTs The transfer of control from one thread

to another within the same process requires a mode switch to the kernel

25Operating Systems

Page 26: L04 Threads

Combined Approaches Thread creation is

done in the user space

Bulk of scheduling and synchronization of threads is by the application

Solaris is an example26

Operating Systems

Page 27: L04 Threads

Relationship Between Threads and Processes

Table 4.2 Relationship between Threads and Processes 27Operating Systems

Page 28: L04 Threads

Performance Effect of Multiple Cores

Figure 4.7 (a) Figure 4.7 (b) 28

Operating Systems

Page 29: L04 Threads

Windows Processes Windows process design is driven by the need to

provide support for a variety of OS environments. Processes supported by different OS environments differ in a number of ways, including the following: How processes are named Whether threads are provided within processes How processes are represented How process resources are protected What mechanisms are used for interprocess

communication and synchronization How processes are related to each other

Operating Systems

29

Page 30: L04 Threads

Windows ProcessesProcesses and services provided by the Windows Kernel are relatively simple and general purpose allowing each subsystem to emulate particular process structure and functionality.

•Important characteristics of windows process

• implemented as objects• created as new process or a copy of an

existing• an executable process may contain one or

more threads• both processes and thread objects have

built-in synchronization capabilities30

Operating Systems

Page 31: L04 Threads

Relationship Between Process and Resource

Figure 4.10 A Windows Process and Its Resources

31Operating Systems

G i v e n t o e a c h u s e r, a n d u s e d t o e v a l u a t e t h e a b i l i t y o f t h e u s e r t o a c c e s s s e c u r e d o b j e c t s o r d o s o m e r e s t r i c t e d f u n c t i o n .

Currently assigned to the process. These cannot directly be modified by the process.

These objects are known to the process.

Page 32: L04 Threads

Process and Thread Objects

Processes• an entity

corresponding to a user job or application that owns resources

Threads• a

dispatchable unit of work that executes sequentially and is interruptible

Windows makes use of two types of process-related objects:

32Operating Systems

Page 33: L04 Threads

Windows Process and

Thread Objects

33Operating Systems

Page 34: L04 Threads

Windows Process Object Attributes

Table 4.3 Windows Process Object Attributes

34Operating Systems

Page 35: L04 Threads

Windows Thread Object Attributes

Table 4.4 Windows Thread Object Attributes

35Operating Systems

Page 36: L04 Threads

Multithreaded Process

Achieves concurrency without the overhead

of using multiple processes

Threads within the same process can

exchange information through their common

address space and have access to the

shared resources of the process

Threads in different processes can

exchange information through shared

memory that has been set up between the two

processes36Operating Systems

Page 37: L04 Threads

Thread States

Figure 4.12 Windows Thread States37Operating Systems

Page 38: L04 Threads

Symmetric Multiprocessing Support

(SMP)Threads of

any process

can run on any

processor

Soft Affinity

• the dispatcher tries to assign a ready thread to the same processor it last ran on

• helps reuse data still in that processor’s memory caches from the previous execution of the thread

Hard Affinity

• an application restricts thread execution to certain processors

38Operating Systems

Page 39: L04 Threads

Solaris Process

• includes the user’s address space, stack, and process control blockProcess

• a user-created unit of execution within a process

User-level Threads

• a mapping between ULTs and kernel threads

Lightweight Processes

(LWP)• fundamental entities that can be

scheduled and dispatched to run on one of the system processors

Kernel Threads

- makes use of four thread-related concepts:

39Operating Systems

Page 40: L04 Threads

Processes and Threads

in Solaris

Figure 4.13 Processes and Threads in Solaris [MCDO07]40

Operating Systems

Page 41: L04 Threads

Traditional Unix vs Solaris

Figure 4.14 Process Structure in Traditional UNIX and Solaris [LEWI96]

41Operating Systems

FYI

Page 42: L04 Threads

A Lightweight Process (LWP) Data Structure Includes:

An LWP identifier The priority of this LWP A signal mask Saved values of user-level registers The kernel stack for this LWP Resource usage and profiling data Pointer to the corresponding kernel thread Pointer to the process structure42

Operating Systems

Page 43: L04 Threads

Solaris Thread States

Figure 4.15 Solaris Thread States43

Operating Systems

Page 44: L04 Threads

Interrupts as Threads

Most operating systems contain two fundamental forms of concurrent activity:

Processes (threads)• cooperate with each other

and manage the use of shared data structures by primitives that enforce mutual exclusion and synchronize their execution

Interrupts• synchronized by

preventing their handling for a period of time

44Operating Systems

Page 45: L04 Threads

Solaris SolutionSolaris employs a set of kernel threads

to handle interrupts an interrupt thread has its own identifier,

priority, context, and stack the kernel controls access to data

structures and synchronizes among interrupt threads using mutual exclusion primitives

interrupt threads are assigned higher priorities than all other types of kernel threads

45Operating Systems

Page 46: L04 Threads

Linux Tasks

A process, or task, in Linux is

represented by a task_struct data

structure

This structure contains

information in a number of categories

46Operating Systems

Page 47: L04 Threads

Linux Process/Thread Model

Figure 4.16 Linux Process/Thread Model47Operating Systems

Page 48: L04 Threads

Linux ThreadsLinux

does not recognize

a distinction between threads

and processes

User-level threads

are mapped

into kernel-level

processes

A new process is created

by copying

the attributes

of the current process

The new process can be

cloned so that it shares

resources

The clone()

call creates

separate stack

spaces for each

process

48Operating Systems

Page 49: L04 Threads

Linux Clone ()

Flags

49Operating Systems

Page 50: L04 Threads

Mac OS X Grand Central Dispatch (GCD)

Provides a pool of available threads Designers can designate portions of

applications, called blocks, that can be dispatched independently and run concurrently

Concurrency is based on the number of cores available and the thread capacity of the system

50Operating Systems

Page 51: L04 Threads

Block A simple extension to a language A block defines a self-contained unit of

work Enables the programmer to encapsulate

complex functions Scheduled and dispatched by queues Dispatched on a first-in-first-out basis Can be associated with an event source,

such as a timer, network socket, or file descriptor51

Operating Systems

Page 52: L04 Threads

Summary

User-level threads created and managed by a threads library that runs in the user space of a

process a mode switch is not required to switch from one thread to another only a single user-level thread within a process can execute at a time if one thread blocks, the entire process is blocked

Kernel-level threads threads within a process that are maintained by the kernel a mode switch is required to switch from one thread to another multiple threads within the same process can execute in parallel on a

multiprocessor blocking of a thread does not block the entire process

Process/related to resource ownership Thread/related to program execution

52Operating Systems


Recommended