Implementing Processes, Threads, and Resources

Download Report

Transcript Implementing Processes, Threads, and Resources

Slide 6-1
Implementing
Processes, Threads,
and Resources
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Implementing the Process Abstraction
Pj CPU
Ideal
Abstraction: P Executable P Executable
i
j
As if using
Memory
Memory
an
OS interface
Actual
machine
CPU
ALU
Control
Unit
Pk CPU
…
Pk Executable
Memory
OS Address
Space
Pi Address
Space
Pk Address
Space
…
Pj Address
Space
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Machine Executable Memory
Pi CPU
Slide 6-2
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-3
External View of the Process Manager
Application
Program
Device Mgr
UNIX
Memory Mgr
File Mgr
exec()
Memory Mgr
File Mgr
Process Mgr
Device Mgr
wait()
CreateThread()
CloseHandle() CreateProcess()
WaitForSingleObject()
Process Mgr
fork()
Windows
Hardware
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-4
Process Manager Responsibilities
Slide 6-5
• 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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Modern Processes and Threads
Thrdj in Pi
Thrdk in Pi
…
Pi CPU
…
…
OS interface
…
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-6
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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-7
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-8
Slide 6-9
Processes &Threads
Stack
State
Copyright © 2004 Pearson Education, Inc.
Static data
Map
Operating Systems: A Modern Perspective, Chapter 6
Program
Map
Resources
Address Space
Stack
State
Slide 6-10
The Address Space
Address
Space
Address
Binding
Executable
Memory
Process
Files
Other objects
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-11
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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-12
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-13
Threads
• Active Element
• Threads
– Host process environment
– Thread-specific data (at least a stack)
– Thread ID
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-14
Slide 6-15
Tracing the Hardware Process
Hardware process progress
Machine is
Powered up
Bootstap
Process Interrupt
Loader Manager Handler P1
Load the kernel
Initialization
Execute a thread
Schedule
Pn
…
Service an interrupt
Copyright © 2004 Pearson Education, Inc.
P,2
Operating Systems: A Modern Perspective, Chapter 6
The Abstract Machine Interface
Slide 6-16
Application Program
Abstract Machine Instructions
Trap
Instruction
User Mode
Instructions
fork()
open()
OS
User Mode
Instructions
Copyright © 2004 Pearson Education, Inc.
Supervisor Mode
Instructions
Operating Systems: A Modern Perspective, Chapter 6
create()
Abstract Machine Instruction Set
ALU - 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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-17
Slide 6-18
Context Switching
Executable Memory
Initialization
Interrupt
1
Process
Manager
7
8
Interrupt
Handler
2
4
3
P1
9
5
P2
6
Pn
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Descriptors
Slide 6-19
• 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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Creating a Process in UNIX
pid = fork();
UNIX kernel
Process Table
…
Copyright © 2004 Pearson Education, Inc.
Process Descriptor
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-20
Linux Process Descriptor
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-21
Linux Process Descriptor
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-22
Linux Process Descriptor
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-23
Windows NT Process Descriptor
Slide 6-24
EPROCESS
KPROCESS
NT Kernel
NT Executive
…
uint32
uint32
…
Byte
…
void
…
KernelTime;
UserTime;
state;
*UniqueProcessId;
NT Kernel handles object management, interrupt handing, thread scheduling
NT Executive handles all other aspect of a process
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-25
Windows NT Process Descriptor (3)









Slide 6-26
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Windows NT Thread Descriptor
EPROCESS
KPROCESS
ETHREAD
KTHREAD
NT Kernel
NT Executive
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-27
Slide 6-28
Creating a Process in NT
CreateProcess(…);
Win32 Subsystem
ntCreateProcess(…);
…
ntCreateThread(…);
NT Executive
Handle Table
NT Kernel
…
Copyright © 2004 Pearson Education, Inc.
Process Descriptor
Operating Systems: A Modern Perspective, Chapter 6
Windows NT Handles
Application
Handle
User Space
Supervisor Space
Executive Object
Kernel
Object
NT Executive
NT Kernel
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-29
Thread Abstraction
Process 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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-30
State of a Process/Thread
Slide 6-31
State Variable - summary status of the process/thread which
is located in descriptor
Request
Simple State Diagram
Done
Running
Request
Schedule
Start
Allocate
Blocked
Copyright © 2004 Pearson Education, Inc.
Ready
Operating Systems: A Modern Perspective, Chapter 6
UNIX State Transition Diagram
Request
Wait by
parent
Done
Running
zombie
Sleeping
Schedule
Request
I/O Request
Start
Allocate
Runnable
I/O Complete
Uninterruptible
Sleep
Copyright © 2004 Pearson Education, Inc.
Resume
Traced or Stopped
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-32
Slide 6-33
Windows NT Thread States
CreateThread
Initialized
Activate
Dispatch
Exit
Wait
Running
Waiting
Ready
Wait Complete
Wait Complete
Transition
Preempt
Reinitialize
Select
Terminated
Dispatch
Standby
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-34
Resources
R = {Rj | 0  j < m} = resource types
C = {cj  0 |  RjR (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.
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-35
Using the Model
Slide 6-36
• There is a resource manager, Mgr(Rj) for every Rj
• Process pi can request units of Rj if it is currently running
pi can only request ni  cj units of reusable Rj
pi can request unbounded # of units of consumable Rj
•Mgr(Rj) can allocate units of Rj to pi
request
Mgr(Rj)
Process
allocate
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
A Generic Resource Manager
Resource Manager
Policy
Blocked Processes
ProcessProcess
Process
request()
Process
release()
Resource Pool
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-37
Process Hierarchies
Initial Process
System
Process
Copyright © 2004 Pearson Education, Inc.
System
Process
Other
Processes
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-38
Process Hierarchies
Slide 6-39
• Parent-child relationship may be significant:
parent controls children’s execution
Done
Request
Running
Yield
Request
Schedule
Suspend
Ready-Active
Activate
Allocate
Suspend
Blocked-Active
Suspend
Start
Ready-Suspended
Allocate
Blocked-Suspended
Activate
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Manager Overview
Program
Slide 6-40
Process
Abstract Computing Environment
File
Manager
Process
Deadlock
Description
Protection
Synchronization
Device
Manager
Devices
Copyright © 2004 Pearson Education, Inc.
Memory
Manager
Memory
Scheduler
CPU
Operating Systems: A Modern Perspective, Chapter 6
Resource
Resource
Resource
Manager
Manager
Manager
Other H/W
Slide 6-41
UNIX Organization
Process
Process
Libraries
Process
File
Manager
System Call Interface
Process
Deadlock
Description
Protection
Synchronization
Device
Manager
Memory
Manager
Scheduler
Monolithic Kernel
Devices
Copyright © 2004 Pearson Education, Inc.
Memory
CPU
Operating Systems: A Modern Perspective, Chapter 6
Resource
Resource
Resource
Manager
Manager
Manager
Other H/W
Slide 6-42
Windows NT Organization
T
Process
T
T
T
Process
Process
T
T
T
T
T
Libraries
Subsystem
Subsystem
Subsystem
User
NT Executive
NT Kernel
Hardware Abstraction Layer
Processor(s)
Copyright © 2004 Pearson Education, Inc.
Main Memory
Operating Systems: A Modern Perspective, Chapter 6
I/O Subsystem
Devices