Post on 10-Feb-2016
description
Slide 6-1
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6
Slide 6-2
Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6
ImplementingProcesses, Threads,
and Resources
6
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
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()
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)
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
…
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
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
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
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
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
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
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)
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
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
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
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
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.
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
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
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
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
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
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