52
8/7/2019 Threads(chp3,4)-Lec1 http://slidepdf.com/reader/full/threadschp34-lec1 1/52 Chapter 3 Processes Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings

Threads(chp3,4)-Lec1

Embed Size (px)

Citation preview

Page 1: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 1/52

Chapter 3

Processes

Dave Bremer 

Otago Polytechnic, N.Z.©2008, Prentice Hall

Operating Systems:

Internals and Design Principles, 6/E 

William Stallings

Page 2: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 2/52

Requirements of an

Operating System� Fundamental Task: Process Management 

� The Operating System must

± Interleave the execution of multiple processes± Allocate resources to processes, and protect

the resources of each process from other 

processes,

± Enable processes to share and exchange

information,

± Enable synchronization among processes.

Page 3: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 3/52

Concepts

± Computer platforms consists of a collection of 

hardware resources

± Computer applications are developed to

perform some task

± OS provides an interface for applications to

use

± OS provides a representation of resourcesthat can be requested and accessed by

application

Page 4: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 4/52

The OS Manages

Execution of Applications� Resources are made available to multiple

applications

� The processor is switched among multipleapplication

� The processor and I/O devices can be

used efficiently

Page 5: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 5/52

What is a ³  process´ ?

� A program in exec ution

� An instance of a program running on acomputer 

� The entity that can be assigned to andexecuted on a processor 

� A unit of activity characterized by the

execution of a sequence of instructions, acurrent state, and an associated set of system instructions

Page 6: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 6/52

Process Elements

� A process is comprised of:

± Program code (possibly shared)

± A set of data

± A number of attributes describing the state of the process

Page 7: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 7/52

Process Elements

� While the process is running it has anumber of elements including

± Identifier 

± State

± Priority

± Program counter 

± Memory pointers± Context data

± I/O status information

± Accounting information

Page 8: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 8/52

Process Control Block

� Contains the process

elements

� Created and manage by

the operating system

� Allows support for 

multiple processes

Page 9: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 9/52

Trace of the Process

� The behavior of an individual process is

shown by listing the sequence of 

instructions that are executed

� This list is called a Trace

� Dispatcher  is a small program which

switches the processor from one process

to another 

Page 10: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 10/52

Process Execution

� Consider three

processes being

executed

� All are in memory

(plus the dispatcher)

� Lets ignore virtual

memory for this.

Page 11: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 11/52

Trace from the

processes point of view:� Each process runs to completion

Page 12: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 12/52

Trace from Processors

point of view

TimeoutI/O

TimeoutTimeout

Page 13: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 13/52

Chapter 4

Threads

Dave Bremer 

Otago Polytechnic, N.Z.©2008, Prentice Hall

Operating Systems:

Internals and Design Principles, 6/E 

William Stallings

Page 14: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 14/52

Processes and Threads

� Processes have two characteristics:

± Resource ownership - process includes a

virtual address space to hold the process

image

± Scheduling/execution - follows an execution

path that may be interleaved with other 

processes� These two characteristics are treated

independently by the operating system

Page 15: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 15/52

Processes and Threads

� The unit of dispatching is referred to as a

thread or lightweight process

� The unit of resource ownership is referredto as a process or task 

Page 16: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 16/52

Multithreading

� The ability of an

OS to support

multiple,

concurrent paths

of execution within

a single process.

Page 17: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 17/52

Single Thread

Approaches� MS-DOS supports a

single user process

and a single thread.

� Some UNIX, support

multiple user 

processes but only

support one threadper process

Page 18: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 18/52

Multithreading

� Java run-time

environment is a

single process with

multiple threads

� Multiple processes

and threads are found

in Windows, Solaris,and many modern

versions of UNIX

Page 19: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 19/52

Processes

� A virtual address space which holds the

process image

� Protected access to± Processors,

± Other processes,

± Files,

± I/O resources

Page 20: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 20/52

One or More Threads in

Process� Each thread has

± An 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 itsprocess (all threads of a process share this)

Page 21: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 21/52

One view«

� One way to view a thread is as an

independent program cou

nter operating w ithin a process.

Page 22: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 22/52

Threads vs. processes

Page 23: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 23/52

Benefits of Threads

� Takes less time to create a new thread

than a process

� Less time to terminate a thread than aprocess

� Switching between two threads takes less

time that switching processes

� Threads can communicate with each other 

± without invoking the kernel

Page 24: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 24/52

Thread use in a

Single-User System� Foreground and background work

� Asynchronous processing

� Speed of execution� Modular program structure

Page 25: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 25/52

Threads

� Several actions that affect all of the

threads in a process

± The OS must manage these at the process

level.

� Examples:

± Suspending a process involves suspending all

threads of the process

± Termination of a process, terminates all

threads within the process

Page 26: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 26/52

Activities similar 

to Processes� Threads have execution states and may

synchronize with one another.

± Similar to processes

� We look at these two aspects of thread

functionality in turn.

± States

± Synchronisation

Page 27: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 27/52

Thread Execution States

� States associated with a change in thread

state

± Spawn (another thread)

± Block

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

threads

± Unblock± Finish (thread)

� Deallocate register context and stacks

Page 28: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 28/52

Example:

Remote Procedure Call� Consider:

± A program that performs two remote

procedure calls (RPCs)

± to two different hosts

± to obtain a combined result.

Page 29: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 29/52

RPC

Using Single Thread

Page 30: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 30/52

RPC Using

One Thread per Server 

Page 31: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 31/52

Multithreading

on a Uniprocessor 

Page 32: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 32/52

Adobe PageMaker 

Page 33: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 33/52

Categories of 

Thread Implementation� User Level Thread (ULT)

� Kernel level Thread (KLT) also called:± kernel-supported threads

± lightweight processes.

Page 34: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 34/52

User-Level Threads

� All thread

management is done

by the application

� The kernel is not

aware of the

existence of threads

Page 35: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 35/52

Advantages of ULT

� Thread switching does not require kernel

mode privileges.

� This saves the overhead of two modeswitches (user to kernel; kernel back to

user)

� Scheduling can be application specific.

� ULTs can run on any OS.

Page 36: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 36/52

Disadvantage of ULT

� Many system calls are blocking. As a

result, not only is that thread blocked, but

also all of the threads within the process

are blocked.

� A multithreaded application cannot take

advantage of multiprocessing. As one

process is assigned to one processor asingle thread within a process can execute

at a time.

Page 37: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 37/52

Jacketing

� Away to overcome the problem of blocking

threads is to use a technique referred to

as jacketing. The purpose of jacketing

is to convert a blocking system call into

a nonblocking system call.

Page 38: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 38/52

Relationships between ULT

Thread and Process States

Page 39: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 39/52

Kernel-Level Threads

� Kernel maintains context

information for the

process and the threads

± No thread management

done by application

� Scheduling is done on a

thread basis� Windows is an example

of this approach

Page 40: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 40/52

Advantages of KLT

� 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 themselves can be

multithreaded.

Page 41: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 41/52

Disadvantage of KLT

� The transfer of control from one thread to

another within the same process requires

a mode switch to the kernel

Page 42: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 42/52

Combined Approaches

� Thread creation done in

the user space

� Bulk of scheduling andsynchronization of 

threads by the

application

� Example is Solaris

Page 43: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 43/52

Relationship Between

Thread and Processes

Page 44: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 44/52

Roadmap

� Threads: Resource ownership and

execution

� Symmetric multiprocessing (SMP).� Microkernel

� Case Studies of threads and SMP:

± Windows± Solaris

± Linux

Page 45: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 45/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-processcommunication and synchronization

± How processes are related to each other 

Page 46: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 46/52

Windows Processes

� Processes and services provided by the

Windows Kernel are relatively simple and

general purpose

± Implemented as objects

± An executable process may contain one or 

more threads

± Both processes and thread objects have built-in synchronization capabilities

Page 47: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 47/52

Relationship between

Process andR

esources

Page 48: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 48/52

Windows Process Object

Page 49: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 49/52

Windows Thread Object

Page 50: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 50/52

Thread States

Page 51: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 51/52

Linux Tasks

� A process, or task, in Linux is represented

by a task_struct data structure

� This contains a number of categoriesincluding:

± State

± Scheduling information

± Identifiers

± Interprocess communication

± And others

Page 52: Threads(chp3,4)-Lec1

8/7/2019 Threads(chp3,4)-Lec1

http://slidepdf.com/reader/full/threadschp34-lec1 52/52

Linux

Process/Thread Model