42
Operating Systems: A Modern Perspective, Chapter 6 Slide 6- 1 Copyright © 2004 Pearson Education, Inc. Implementing Processes, Threads, and Resources

Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Implementing Processes, Threads, and Resources

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-1

Copyright © 2004 Pearson Education, Inc.

ImplementingProcesses, Threads,

and Resources

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-2

Copyright © 2004 Pearson Education, Inc.

OS AddressSpace

OS AddressSpace

Implementing the Process Abstraction

ControlUnit

OS interface

Mac

hine

Exe

cuta

ble

Mem

ory

ALU

CPUPi Address

Space

Pi AddressSpace

Pi CPU

Pi ExecutableMemory

Pi ExecutableMemory

Pk AddressSpace

Pk AddressSpace

Pk CPU

Pk ExecutableMemory

Pk ExecutableMemory

Pj AddressSpace

Pj AddressSpace

Pj CPU

Pj ExecutableMemory

Pj ExecutableMemory

Ideal Abstraction:As if using anActual machine

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-3

Copyright © 2004 Pearson Education, Inc.

Modern process

“The value of the modern process model is that it enables the programmer to design software so that various parts of the computation can work together as a set of threads within a single modern process framework.”

Classic process can work together but they do not share a customized computational framework

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-4

Copyright © 2004 Pearson Education, Inc.

External View of the Process Manager

Hardware

ApplicationProgram

ApplicationProgram

Dev

ice

Mgr

Pro

cess

Mgr

Mem

ory

Mgr

Fil

e M

gr

UNIXD

evic

e M

gr

Pro

cess

Mgr

Mem

ory

Mgr

Fil

e M

gr

Windows

CreateThread()CreateProcess()CloseHandle()

WaitForSingleObject()

fork()

exec()wait()

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-5

Copyright © 2004 Pearson Education, Inc.

Process Manager Responsibilities

• Define & implement the essential characteristics of a process and thread– Algorithms to define the behavior– Data structures to preserve the state of the execution

• Define what “things” threads in the process can reference – the address space (most of the “things” are memory locations)

• Manage the resources used by the processes/threads• Tools to create/destroy/manipulate processes & threads• Tools to time-multiplex the CPU – Scheduling the

(Chapter 7)• Tools to allow threads to synchronization the operation

with one another (Chapters 8-9)• Mechanisms to handle deadlock (Chapter 10)• Mechanisms to handle protection (Chapter 14)

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-6

Copyright © 2004 Pearson Education, Inc.

Modern Processes and Threads

OS interface

…Pi CPU

Thrdj in Pi Thrdk in Pi

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-7

Copyright © 2004 Pearson Education, Inc.

Modern Threads

User Space Threads - underlying Os implements classic processes and the user space thread library executes on top of the OS abstract machine to multiprogram the threads. (Mach C and POSIX threads)

Kernel Threads - OS time-multiplexes the execution of threads instead of processes. Therefore when one thread blocks the other threads can still execute. (Windows)

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-8

Copyright © 2004 Pearson Education, Inc.

Resources

Any element of the abstract machine that a process can request and can cause the process to be blocked if not available.

If device is allocated to a process then it is configured into the abstract machine for process

Multiple resource managers - hardware devices, processor, abstract synch resources, primary memory, and files

Each resource manager must present a common behavior described by a general model

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-9

Copyright © 2004 Pearson Education, Inc.

Processes &Threads

Add

ress

Spa

ceA

ddre

ss S

pace

MapMap

Sta

ck

State

Pro

gram

Sta

tic d

ata

Res

ourc

es

Sta

ck

State

MapMap

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-10

Copyright © 2004 Pearson Education, Inc.

The Address Space

ProcessProcess

AddressSpace

AddressBinding

ExecutableMemory

Other objects

Files

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-11

Copyright © 2004 Pearson Education, Inc.

Building the Address Space

• Some parts are built into the environment– Files– System services

• Some parts are imported at runtime– Mailboxes– Network connections

• Memory addresses are created at compile (and run) time

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-12

Copyright © 2004 Pearson Education, Inc.

The OS

• Abstract machine interface– Host hardware instruction set and set of

functions exported by OS

• Unix and Windows most widely used

• Unix - POSIX interface (standard)

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-13

Copyright © 2004 Pearson Education, Inc.

Modern Process Framework

• Thread-based computation is executed within this framework

• Modern Process structure– Address space– Program– Data - shared by threads– Resources

• Threads share the resources that have been allocated to the process

– Process Id

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-14

Copyright © 2004 Pearson Education, Inc.

Threads

• Active Element

• Threads– Host process environment– Thread-specific data (at least a stack)– Thread ID

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-15

Copyright © 2004 Pearson Education, Inc.

Tracing the Hardware Process

Bootstap

LoaderProcessManager

InterruptHandler P1 P,2 Pn

Machine isPowered up

InitializationLoad the kernel

Service an interrupt

Har

dwar

e pr

oces

s pr

ogre

ss

Execute a thread

Schedule

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-16

Copyright © 2004 Pearson Education, Inc.

The Abstract Machine Interface

User ModeInstructions

User ModeInstructions

Application Program

User ModeInstructions

Abstract Machine Instructions

TrapInstruction

Supervisor ModeInstructions

Supervisor ModeInstructions

fork()

create()open()

OS

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-17

Copyright © 2004 Pearson Education, Inc.

Abstract Machine Instruction SetALU - load, store, add…Control Unit - branch, procedure_call…Trap - create_process(), open_file()…

Linux 2.4x - exports over 200 functions- 2.5 million lines of code

Windows NT/2000/XP- exports over 2,000 functions- over 25 million lines of code

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-18

Copyright © 2004 Pearson Education, Inc.

Context Switching

ProcessManager

InterruptHandler

P1

P2

Pn

Executable Memory

Initialization1

23

45

7Interrupt

8

9

6

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-19

Copyright © 2004 Pearson Education, Inc.

Process Descriptors

• OS creates/manages process abstraction

• Descriptor is data structure for each process– Register values– Logical state– Type & location of resources it holds– List of resources it needs– Security keys– Process ID, parent process

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-20

Copyright © 2004 Pearson Education, Inc.

Creating a Process in UNIX

pid = fork();

UNIX kernelUNIX kernel

Process Table

Process Descriptor

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-21

Copyright © 2004 Pearson Education, Inc.

Linux Process Descriptor

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-22

Copyright © 2004 Pearson Education, Inc.

Linux Process Descriptor

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-23

Copyright © 2004 Pearson Education, Inc.

Linux Process Descriptor

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-24

Copyright © 2004 Pearson Education, Inc.

EPROCESS

…void *UniqueProcessId;…

NT Executive

Windows NT Process Descriptor

KPROCESS…uint32 KernelTime;uint32 UserTime;…Byte state;

NT Kernel

NT Kernel handles object management, interrupt handing, thread scheduling

NT Executive handles all other aspect of a process

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-25

Copyright © 2004 Pearson Education, Inc.

Windows NT Process Descriptor (2)

Kernel process object includes: Pointer to the page directory Kernel & user time Process base priority Process state List of the Kernel thread descriptors that are

using this process

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-26

Copyright © 2004 Pearson Education, Inc.

Windows NT Process Descriptor (3)

Parent identification Exit status Creation and termination times. Memory status Security information executable image Process priority class used by the thread scheduler. A list of handles used by this process A pointer to Win32-specific information

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-27

Copyright © 2004 Pearson Education, Inc.

ETHREAD

Windows NT Thread Descriptor

EPROCESS

KPROCESS

NT KernelKTHREAD

NT Executive

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-28

Copyright © 2004 Pearson Education, Inc.

Creating a Process in NT

CreateProcess(…);

Win32 SubsystemWin32 Subsystem

ntCreateProcess(…);…ntCreateThread(…);

NT ExecutiveNT Executive

NT KernelNT Kernel…

Handle Table

Process Descriptor

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-29

Copyright © 2004 Pearson Education, Inc.

Windows NT Handles

Application

KernelObject

Executive Object

User Space

Supervisor Space

NT Executive

NT Kernel

Handle

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-30

Copyright © 2004 Pearson Education, Inc.

Thread AbstractionProcess Manager has algorithms to control threads and thread descriptor (data structure) to keep track of threads.

Management Tasks- Create/destroy thread- Allocate thread-specific resources- Manage thread context switching

Thread Descriptor- state- execution stats- process (reference to associated process)- list of related threads- stack (reference to stack)- thread-specific resources

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-31

Copyright © 2004 Pearson Education, Inc.

State of a Process/Thread

State Variable - summary status of the process/thread which is located in descriptor

ReadyBlocked

Running

Start

Schedule

Request

Done

Request

Allocate

Simple State Diagram

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-32

Copyright © 2004 Pearson Education, Inc.

UNIX State Transition Diagram

Runnable

UninterruptibleSleep

Running

Start

Schedule

Request

Done

I/O Request

Allocate

zombie

Wait byparent

Sleeping

Traced or Stopped

Request

I/O Complete Resume

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-33

Copyright © 2004 Pearson Education, Inc.

Windows NT Thread States

Initialized

CreateThread

Ready

Activate

Sele

ct

Standby

Running

Terminated

Waiting

Transition

Reinitialize

Exit

Pre

empt

Dispatch

WaitWait Complete

Wait Complete

Dispatch

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-34

Copyright © 2004 Pearson Education, Inc.

Resources

Resource: Anything that a process can request and then become blocked because that thing is not available.

Resource Descriptors- Internal resource name- Total Units- Available Units- List of available units- List of Blocked processes

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-35

Copyright © 2004 Pearson Education, Inc.

Resources

R = {Rj | 0 j < m} = resource typesC = {cj 0 | RjR (0 j < m)} = units of Rj available

Reusable resource: After a unit of the resource has been allocated, it must ultimately be released back to the system. E.g., CPU, primary memory, disk space, … The maximum value for cj is the number of units of that resource

Consumable resource: There is no need to release a resource after it has been acquired. E.g., a message, input data, … Notice that cj is unbounded.

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-36

Copyright © 2004 Pearson Education, Inc.

Using the Model• There is a resource manager, Mgr(Rj) for every Rj

Mgr(Rj)ProcessProcess

pi can only request ni cj units of reusable Rj

pi can request unbounded # of units of consumable Rj

• Process pi can request units of Rj if it is currently running

request

•Mgr(Rj) can allocate units of Rj to pi

allocate

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-37

Copyright © 2004 Pearson Education, Inc.

A Generic Resource Manager

ProcessProcess

Resource Manager

ProcessProcessProcessProcessProcessProcess

Blocked Processes

Resource Pool

request()

release()

Policy

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-38

Copyright © 2004 Pearson Education, Inc.

Process Hierarchies

Initial Process

System Process

System Process

Other Processes

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-39

Copyright © 2004 Pearson Education, Inc.

Process Hierarchies• Parent-child relationship may be significant:

parent controls children’s execution

Ready-Active

Blocked-Active

Running

StartSchedule

RequestDone

Request

AllocateReady-Suspended

Blocked-Suspended

SuspendYield

AllocateSuspend

Suspend

Activate

Activate

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-40

Copyright © 2004 Pearson Education, Inc.

Process Manager Overview

ProgramProgram ProcessProcess

Abstract Computing Environment

FileManager

MemoryManager

DeviceManager

ProtectionProtectionDeadlockDeadlock

SynchronizationSynchronization

ProcessDescription

ProcessDescription

CPUCPU Other H/WOther H/W

SchedulerScheduler ResourceManager

ResourceManagerResource

Manager

ResourceManagerResource

Manager

ResourceManager

MemoryMemoryDevicesDevices

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-41

Copyright © 2004 Pearson Education, Inc.

UNIX Organization

System Call InterfaceSystem Call Interface

FileManager

MemoryManager

DeviceManager

ProtectionProtectionDeadlockDeadlock

SynchronizationSynchronization

ProcessDescription

ProcessDescription

CPUCPU Other H/WOther H/W

SchedulerScheduler ResourceManager

ResourceManagerResource

Manager

ResourceManagerResource

Manager

ResourceManager

MemoryMemoryDevicesDevices

LibrariesLibraries ProcessProcess

ProcessProcess

ProcessProcess

Monolithic Kernel

Operating Systems: A Modern Perspective, Chapter 6

Slide 6-42

Copyright © 2004 Pearson Education, Inc.

Windows NT Organization

Processor(s) Main Memory Devices

LibrariesLibraries

ProcessProcess

ProcessProcess

ProcessProcess

SubsystemSubsystemUser

SubsystemSubsystem SubsystemSubsystem

Hardware Abstraction LayerHardware Abstraction LayerNT Kernel

NT ExecutiveI/O SubsystemI/O Subsystem

TT

TT

TT T T

T