implementing processes, threads, and resources

34

Upload: manchu

Post on 09-Jan-2016

29 views

Category:

Documents


4 download

DESCRIPTION

Chapter 6. Implementing Processes, Threads, and Resources. P i CPU. P j CPU. P k CPU. …. P i Executable Memory. P j Executable Memory. P k Executable Memory. P i Address Space. P k Address Space. P j Address Space. Implementing the Process Abstraction. OS interface. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Implementing Processes, Threads, and Resources
Page 2: Implementing Processes, Threads, and Resources

ImplementingProcesses, Threads,

and Resources

Chapter 6

Page 3: Implementing Processes, Threads, and Resources

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

Page 4: Implementing Processes, Threads, and Resources

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()

Page 5: Implementing Processes, Threads, and Resources

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)

Page 6: Implementing Processes, Threads, and Resources

Modern Processes and Threads

OS interface

…Pi CPU

Thrdj in Pi Thrdk in Pi

Page 7: Implementing Processes, Threads, and Resources

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

Page 8: Implementing Processes, Threads, and Resources

Process Address Space

• Process address space is the collection of addresses that a thread can reference.

• Normally, it refers to an executable memory location.

• However, due to the nature of a process, a process address space can also be associated with other machine abstract elements-such as, files, device registers and other objects.

Page 9: Implementing Processes, Threads, and Resources

Memory-Mapped-Resources

• 1. Most of the components in a computer system can be referenced by memory addresses. (One of the exception is the processor it self)

• 2. The address space provides a uniform mechanism by which a process can reference bytes in all memory-mapped resources.

• 3. Each resource manager is responsible for binding addresses with addressable elements of the resource.

Page 10: Implementing Processes, Threads, and Resources

The Address Space

ProcessProcess

AddressSpace

AddressBinding

ExecutableMemory

Other objects

Files

Page 11: Implementing Processes, Threads, and Resources

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

Page 12: Implementing Processes, Threads, and Resources

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

Page 13: Implementing Processes, Threads, and Resources

Responsibilities of Process Manager

• 1. Process Creation and Termination

• 2. Thread Creation and Termination

• 3. Process/Thread Synchronization

• 4. Resource Allocation

• 5. Resource Protection

• 6. Co-operate with Device Manager to Implement I/O

• 7. Co-operate with the Memory Manager to Implement Memory Space

Page 14: Implementing Processes, Threads, and Resources

The Abstract Machine Interface

User ModeInstructions

User ModeInstructions

Application Program

User ModeInstructions

Abstract Machine Instructions

TrapInstruction

Supervisor ModeInstructions

Supervisor ModeInstructions

fork()

create()open()

OS

Page 15: Implementing Processes, Threads, and Resources

Context Switching-Abstract Machine switching

ProcessManager

InterruptHandler

P1

P2

Pn

Executable Memory

Initialization1

23

45

7Interrupt

8

9

6

Page 16: Implementing Processes, Threads, and Resources

• OS Families-Example

POSIX Standard Description Release

POSIX.1-1988 system interfaces and headers SunOS 4.1

POSIX.1-1990 POSIX.1-1988 update Solaris 2.0

POSIX.1b-1993 real-time extensions Solaris 2.4

POSIX.1c-1996 threads extensions Solaris 2.6

POSIX.2-1992 shell and utilities Solaris 2.5

POSIX.2a-1992 interactive shell and utilities Solaris 2.5

Page 17: Implementing Processes, Threads, and Resources

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– etc. (see Table 6.1 and the source code of your

favorite OS)

Page 18: Implementing Processes, Threads, and Resources

EPROCESS

…void *UniqueProcessId;…

NT Executive

Windows NT Process Descriptor

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

NT Kernel

Page 19: Implementing Processes, Threads, and Resources

Windows NT Process Descriptor (2)

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

using this process

Page 20: Implementing Processes, Threads, and Resources

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

Page 21: Implementing Processes, Threads, and Resources

ETHREAD

Windows NT Thread Descriptor

EPROCESS

KPROCESS

NT KernelKTHREAD

NT Executive

Page 22: Implementing Processes, Threads, and Resources

Creating a Process in UNIX

pid = fork();

UNIX kernelUNIX kernel

Process Table

Process Descriptor

Page 23: Implementing Processes, Threads, and Resources

Creating a Process in NT

CreateProcess(…);

Win32 SubsystemWin32 Subsystem

ntCreateProcess(…);…ntCreateThread(…);

NT ExecutiveNT Executive

NT KernelNT Kernel…

Handle Table

Process Descriptor

Page 24: Implementing Processes, Threads, and Resources

Windows NT Handles

Application

KernelObject

Executive Object

User Space

Supervisor Space

NT Executive

NT Kernel

Handle

Page 25: Implementing Processes, Threads, and Resources

Simple State Diagram

ReadyBlocked

Running

Start

Schedule

Request

Done

Request

Allocate

Page 26: Implementing Processes, Threads, and Resources

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

Page 27: Implementing Processes, Threads, and Resources

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

Page 28: Implementing Processes, Threads, and Resources

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.

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

Page 29: Implementing Processes, Threads, and Resources

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

Page 30: Implementing Processes, Threads, and Resources

A Generic Resource Manager

ProcessProcess

Resource Manager

ProcessProcessProcessProcessProcessProcess

Blocked Processes

Resource Pool

request()

release()

Policy

Page 31: Implementing Processes, Threads, and Resources

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

Page 32: Implementing Processes, Threads, and Resources

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

Page 33: Implementing Processes, Threads, and Resources

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

Page 34: Implementing Processes, Threads, and Resources

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