Implementing Processes, Threads, and Resources

24
Slide 6- 1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

description

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 of Implementing Processes, Threads, and Resources

Page 1: Implementing Processes, Threads, and Resources

Slide 6-1

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Page 2: Implementing Processes, Threads, and Resources

Slide 6-2

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

ImplementingProcesses, Threads,

and Resources

6

Page 3: Implementing Processes, Threads, and Resources

Slide 6-3

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

OS AddressSpace

Implementing the Process Abstraction

ControlUnit

OS interface

Mac

hine

Exe

cuta

ble

Mem

ory

ALU

CPUPi Address

Space

Pi CPU

Pi ExecutableMemory

Pk AddressSpace

Pk CPU

Pk ExecutableMemory

Pj AddressSpace

Pj CPU

Pj ExecutableMemory

Page 4: Implementing Processes, Threads, and Resources

Slide 6-4

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

External View of the Process Manager

Hardware

ApplicationProgram

Dev

ice

Mgr

Proc

ess M

gr

Mem

ory

Mgr

File

Mgr

UNIXD

evic

e M

gr

Proc

ess M

gr

Mem

ory

Mgr

File

Mgr

Windows

CreateThread()CreateProcess()CloseHandle()

WaitForSingleObject()

fork()

exec()wait()

Page 5: Implementing Processes, Threads, and Resources

Slide 6-5

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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

Slide 6-6

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Modern Processes and Threads

OS interface

…Pi CPU

Thrdj in Pi Thrdk in Pi

Page 7: Implementing Processes, Threads, and Resources

Slide 6-7

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Processes &Threads

Add

ress

Spa

ce

Map

Stac

k

State

Prog

ram

Stat

ic d

ata

Res

ourc

es

Stac

k

State

Map

Page 8: Implementing Processes, Threads, and Resources

Slide 6-8

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

The Address Space

Process

AddressSpace

AddressBinding

ExecutableMemory

Other objects

Files

Page 9: Implementing Processes, Threads, and Resources

Slide 6-9

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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 10: Implementing Processes, Threads, and Resources

Slide 6-10

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Tracing the Hardware Process

BootstapLoader

ProcessManager

InterruptHandler P1 P,2 Pn

Machine isPowered up

InitializationLoad the kernel

Service an interrupt

Har

dwar

e pr

oces

s pro

gres

s

Execute a threadSchedule

Page 11: Implementing Processes, Threads, and Resources

Slide 6-11

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

The Abstract Machine Interface

User ModeInstructions

Application Program

User ModeInstructions

Abstract Machine Instructions

TrapInstruction

Supervisor ModeInstructions

fork()

create()open()

OS

Page 12: Implementing Processes, Threads, and Resources

Slide 6-12

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Context Switching

ProcessManager

InterruptHandler

P1

P2

Pn

Executable Memory

Initialization1

23

45

7Interrupt

8

9

6

Page 13: Implementing Processes, Threads, and Resources

Slide 6-13

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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 14: Implementing Processes, Threads, and Resources

Slide 6-14

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Creating a Process in UNIX

pid = fork();

UNIX kernel

Process Table

Process Descriptor

Page 15: Implementing Processes, Threads, and Resources

Slide 6-15

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Simple State Diagram

ReadyBlocked

Running

Start

Schedule

Request

Done

Request

Allocate

Page 16: Implementing Processes, Threads, and Resources

Slide 6-16

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

UNIX State Transition Diagram

Runnable

UninterruptibleSleep

Running

Start

Schedule

Request

Done

I/O RequestAllocate

zombie

Wait byparent

Sleeping

Traced or Stopped

Request

I/O Complete Resume

Page 17: Implementing Processes, Threads, and Resources

Slide 6-17

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Windows NT Thread States

Initialized

CreateThread

Ready

Activate

Sele

ct

Standby

Running

Terminated

Waiting

Transition

Reinitialize

Exit

Pree

mpt

Dispatch

WaitWait Complete

Wait Complete

Dispatch

Page 18: Implementing Processes, Threads, and Resources

Slide 6-18

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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 resourceConsumable 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 19: Implementing Processes, Threads, and Resources

Slide 6-19

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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

Mgr(Rj)Process

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 20: Implementing Processes, Threads, and Resources

Slide 6-20

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

A Generic Resource Manager

Process

Resource Manager

ProcessProcessProcess

Blocked Processes

Resource Pool

request()

release()

Policy

Page 21: Implementing Processes, Threads, and Resources

Slide 6-21

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

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 22: Implementing Processes, Threads, and Resources

Slide 6-22

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Process Manager Overview

Program Process

Abstract Computing Environment

FileManager

MemoryManager

DeviceManager

ProtectionDeadlock

Synchronization

ProcessDescription

CPU Other H/W

SchedulerResourceManagerResourceManagerResourceManager

MemoryDevices

Page 23: Implementing Processes, Threads, and Resources

Slide 6-23

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

UNIX Organization

System Call Interface

FileManager

MemoryManager

DeviceManager

ProtectionDeadlock

Synchronization

ProcessDescription

CPU Other H/W

SchedulerResourceManagerResourceManagerResourceManager

MemoryDevices

Libraries ProcessProcess

Process

Monolithic Kernel

Page 24: Implementing Processes, Threads, and Resources

Slide 6-24

Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6

Windows NT Organization

Processor(s) Main Memory Devices

Libraries

ProcessProcess

Process

SubsystemUser

Subsystem Subsystem

Hardware Abstraction LayerNT Kernel

NT ExecutiveI/O Subsystem

TT

TT

TT T T

T