Download process

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

RSTS/E wikipedia , lookup

Copland (operating system) wikipedia , lookup

Library (computing) wikipedia , lookup

Unix security wikipedia , lookup

Spring (operating system) wikipedia , lookup

CP/M wikipedia , lookup

Distributed operating system wikipedia , lookup

Burroughs MCP wikipedia , lookup

DNIX wikipedia , lookup

Paging wikipedia , lookup

Process management (computing) wikipedia , lookup

Transcript
Slide 6-1
Implementing
Processes, Threads,
and Resources
Copyright © 2004 Pearson Education, Inc.
6
Operating Systems: A Modern Perspective, Chapter 6
Announcements
• Extension til Friday 11 am for HW #1
• Previous lectures online
• Program Assignment #1 online later today,
due 2 weeks from today
• Homework Set #2 online later today, due a
week from today
• Read chapter 6
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-2
Slide 6-3
What is a Process?
• A software program consist
of a sequence of code
instructions and data
– for now, let a simple app = a
program
– CPU executes the instructions
line by line in fetch-execute
cycle from RAM
– code instructions operate on
data
– A program is a passive entity
Program P1
Code
Data
• A process is a program
actively executing from
main memory
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-4
Loading and Executing a Program
OS Loader
Disk
P1
binary
Code
P2
binary
Code
Process
Main
Memory Fetch Code
and Data
Program
P1
binary
CPU
Execution
Program
Counter (PC)
Code
Registers
ALU
Data
Data
Copyright © 2004 Pearson Education, Inc.
Data
Write Data
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-5
What is a Process?
• A process is a
program actively
Main
executing from main
Memory
memory
– has a Program
Counter (PC) and
execution state
associated with it
• CPU registers keep
state
• OS keeps process
state in memory
• it’s alive!
– has an address space
associated with it
• a limited set of
(virtual) addresses
that can be
accessed by the
executing code
Copyright © 2004 Pearson Education, Inc.
Program
P1
binary
Process
Fetch Code
and Data
Code
CPU
Execution
Program
Counter (PC)
Registers
ALU
Data
Write Data
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-6
What is a Process?
– 2 processes
may execute
the same
program code,
but they are
considered
separate
execution
sequences
Main
Memory
Program
P1
binary
Process
Fetch Code
and Data
Code
CPU
Execution
Program
Counter (PC)
Registers
ALU
Data
Write Data
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
How is a Process Structured in
Memory?
Main
Memory
Process
P1
Code
Data
Copyright © 2004 Pearson Education, Inc.
Slide 6-7
float f4=3.0;
global variables
main() {
char* ptr;
ptr = malloc();
foo1();
}
dynamically
allocated
variables
foo1() {
int a1;
....
}
Operating Systems: A Modern Perspective, Chapter 6
functions
local variables
How is a Process Structured in
Memory?
Main Memory
Process
P1
Code
Slide 6-8
float f4=3.0;
global variables
main() {
char* ptr;
ptr = malloc();
foo1();
}
dynamically
allocated
variables
Data
Heap
Stack
Copyright © 2004 Pearson Education, Inc.
foo1() {
int a1;
....
}
Operating Systems: A Modern Perspective, Chapter 6
functions
local variables
How is a Process Structured in
Memory? Run-time memory
max address
User stack
• Run-time memory
image
• Essentially code,
data, stack, and
heap
• Code and data
loaded from
executable file
• Stack grows
downward, heap
grows upward
Unallocated
Heap
Read/write .data, .bss
Read-only .init, .text, .rodata
address 0
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-9
Why Allocate Local Variables on a
Stack?
Slide 6-10
• Strawman approach: pre-allocate all local
variables a priori before a process starts
executing, just like global variables
• What’s wrong with this strawman?
– if a function is never called, then you’ve wasted
space allocating its local variables
– don’t know a priori how many instances of a
local variable to allocate if a function calls
itself, i.e. recursion
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Why Allocate Local Variables on a
Stack?
Slide 6-11
• So allocate local variables only on an asneeded basis
• A stack provides a simple way to allocate
local variables as needed
– When a function is called, allocate its local
variables on top of the stack - push them on the
stack
– when a function completes, deallocate these
local variables - pop them off the stack
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Why Allocate Dynamic Variables on
a Heap in the Process’s Address
Space?
• Strawman II: could ask the OS to allocate
dynamic variables anywhere in memory
– very complex keeping tracking of all the
different locations in memory
• Keeping the dynamic variables in one area
(the process’s heap) associated with the
process’s address space simplifies memory
management
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-12
A Process Executes in its Own
Address Space
Main Memory
Process
P1
Code
• OS tries to provide the illusion
or abstraction that the process
executes in its own address
space, on its own CPU
Data
Heap
Stack
Copyright © 2004 Pearson Education, Inc.
Slide 6-13
Operating Systems: A Modern Perspective, Chapter 6
Implementing the Process Abstraction
Pi CPU
Pj CPU
Pi Executable
Memory
Pj Executable
Memory
Pk CPU
…
Pk Executable
Memory
OS Address
Space
CPU
ALU
Control
Unit
Pi Address
Space
Pk Address
Space
…
Pj Address
Space
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Machine Executable Memory
OS interface
Slide 6-14
Slide 6-15
OS Process Management: External View
Application
Process
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
Process Manager Responsibilities
Slide 6-16
• 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
Slide 6-17
Process Manager Overview
Program
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-18
OS Process Management
Main Memory
Process
P1
Code
Data
Heap
Stack
Copyright © 2004 Pearson Education, Inc.
• OS keeps a Process Control Block
(PCB) for each process:
– Process state: new, running, waiting,
ready, terminated
– Program counter
– CPU registers
– CPU-scheduling information, e.g.
priority
– memory management info: value of
base and limit registers, page tables,
segment tables
– I/O info: open files, etc.
Operating Systems: A Modern Perspective, Chapter 6
Multiple Processes
Slide 6-19
Main Memory
Process
P1
Process
P2
Code
Code
Data
Heap
Data
Stack
Heap
Stack
Copyright © 2004 Pearson Education, Inc.
• Each process is in
memory
• Only one process at a
time executes on the
CPU
• OS provides the
mechanisms to switch
between processes
– this is called a context
switch
Operating Systems: A Modern Perspective, Chapter 6
Context Switching
Slide 6-20
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.
• Each time a
process is
switched out, its
context must be
saved, e.g. on the
stack
• Each time a
process is
switched in, its
context is restored
• This usually
requires copying
of registers
Operating Systems: A Modern Perspective, Chapter 6
Context Switches
• A context switch can occur because of
– a system call
– an I/O interrupt, e.g. disk has finished reading
– a timer interrupt
• this is how you implement multitasking
• Set a timer in the CPU for periodic interrupt, say every 1 ms
• On an interrupt, go to the timer interrupt handler, e.g. the
scheduler, and switch to another process in the ready queue
• Context switch time is pure overhead
– it is the price you pay for multiprogramming
– typically a few milliseconds
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-21
Multiple Processes: State Diagram
Request
Done
Running
Request
Schedule
Start
Allocate
Blocked
Copyright © 2004 Pearson Education, Inc.
Ready
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-22
Communicating Between Processes
Slide 6-23
• Inter-Process Communication or IPC
– would like two processes to share information between them
• shared file
• split a single application into multiple processes to speed up execution
by allowing overlapped I/O
• split an application into multiple processes for modularity of coding
– if address spaces are completely isolated from one another, then
how do we share data?
• Two types
– shared memory - OS provides constructs that allow
– message passing - OS provides constructs that allow
communication via buffers
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Communicating Between Processes
• Shared access to the same memory is
dangerous
– need to synchronize access
– Producer-Consumer example
•
•
•
•
producer writes data to the shared buffer
producer writes flag that data is changed
consumer checks flag in buffer
consumer reads data
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-24
Slide 6-25
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
How is a Process Structured in
Memory? Run-time memory
max address
User stack
• Run-time memory
image
• Essentially code,
data, stack, and
heap
• Code and data
loaded from
executable file
• Stack grows
downward, heap
grows upward
Unallocated
Heap
Read/write .data, .bss
Read-only .init, .text, .rodata
address 0
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-26
Slide 6-27
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
Process Manager Responsibilities
Slide 6-28
• 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-29
Slide 6-30
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-31
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-32
Slide 6-33
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-34
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()
Slide 6-35
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-36
• 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)
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Windows NT Process Descriptor
EPROCESS
KPROCESS
NT Kernel
NT Executive
Copyright © 2004 Pearson Education, Inc.
…
uint32
uint32
…
Byte
…
void
…
KernelTime;
UserTime;
state;
*UniqueProcessId;
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-37
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
Copyright © 2004 Pearson Education, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Slide 6-38
Windows NT Process Descriptor (3)









Slide 6-39
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-40
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-41
Slide 6-42
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-43
Slide 6-44
Simple State Diagram
Request
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-45
Slide 6-46
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, then be
blocked because that thing is not available.
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-47
Using the Model
Slide 6-48
• 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-49
Process Hierarchies
Slide 6-50
• 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
Slide 6-51
Process Manager Overview
Program
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-52
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-53
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