Process Description and Control

Download Report

Transcript Process Description and Control

Operating Systems:
Internals and Design Principles, 6/E
William Stallings
Chapter 3
Process Description and Control
Patricia Roy
Manatee Community College, Venice, FL
©2008, Prentice Hall
Major Functions of an
Operating System
• Interleave the execution of multiple
processes, to maximize processor
utilization while providing reasonable
response time
• Allocate resources to processes
• Support interprocess communication and
user creation of processes
Process
• A program in execution
• An instance of a program running on a
computer
• The entity that can be assigned to and
executed on a processor
• A unit of activity characterized by
– the execution of a sequence of instructions
– a current state
– an associated set of system resources
Process Elements (I)
•
•
•
•
•
Identifier
State
Priority
How to describe a process?
Program counter
What elements are included in
Memory pointers: to code and data
it?
Process Elements (II)
• Context data: value of CPU registers
• I/O status information
– Outstanding I/O requests
– Assigned I/O devices and used files
• Accounting information
– Amount of processor time & clock time used
– Time limits
– Account numbers
Process Control Block
• Contains the process elements
• Created and managed by the operating
system
Process Control Block
• Allows support
for multiple
processes
Trace of the Process
• Sequence of instruction that execute for a
process
• Dispatcher switches the processor from
one process to another
Example Execution
Trace of Process
Combined Trace of Process
Two-State Process Model
• Process may be in one of two states
– Running
– Not-running
Queuing Diagram
Process Creation (I)
Example: Application Modularity
Example: Concurrent Server
Dispatcher
process
Request dispatched to a
worker process
Server
Worker
process
Process Creation (II)
• What does the OS do to create a process?
– Build data structures that are used to manage
the process;
– Allocate address space in main memory to
the process.
Process Termination
Process Termination
Queuing Diagram
Queuing
• Processes
– Not-running ready to execute
– Not-running block
– Dispatcher must scan list to find process notrunning, ready, and in queue the longest
A Five-State Model
•
•
•
•
•
Running
Ready
Blocked
New
Exit
Five-State Process Model
Process States
Using Two Queues
Multiple Blocked Queues
Suspended Processes
• Processor is faster than I/O so all
executable processes could be waiting for
I/O, while there are some new processes
waiting to be admitted
• Swap these processes to disk to free up
more memory to admit new processes
• Blocked state becomes suspend state
when swapped to disk
• Two new states
– Blocked/Suspend
– Ready/Suspend
Two Suspend States
Reason for Process
Suspension
Processes and Resources
In-Class Exercise
Running
1
Blocked
2
6
5
3
4
1: process blocks for input
2: scheduler picks another process
3: scheduler picks this process
4: input becomes available
5: process blocks for input
6: input becomes available
Ready
Which state
transition is
impossible?
Operating System Control
Structures
• Information about the current status of
each process and resource
• Tables are constructed for each entity the
operating system manages
• So, what control tables does an OS have
OS Control Tables
Memory Tables (1)
• Used to keep track of both main (real)
memory and secondary (virtual) memory
• Some of main memory reserved for OS
• The remainder is available for processes
• Processes are maintained on secondary
memory
Memory Tables (2)
• Allocation of main memory to processes
• Allocation of secondary memory to
processes
• Protection attributes for access to shared
memory regions
• Information needed to manage virtual
memory
I/O Tables
• Used by OS to manage I/O devices and
channels of the computer system
• I/O device is available or assigned
• Status of I/O operation
• Location in main memory being used as
the source or destination of the I/O
transfer
File Tables
•
•
•
•
•
Existence of files
Location on secondary memory
Current Status
Attributes
Sometimes this information is maintained
by a file management system, OS has little
or no knowledge of files
• In other OS, detailed file management is
by OS itself
Process Tables
• Manage processes
• What is a process composed of?
• Process image is the collection of
program, data, stack, and attributes
Process Stack
Figure 4-5. (a) Parameter passing in a procedure call: the
stack before the call to read. (b) The stack while the called
procedure is active.
Process Location
• Where are the processes located?
– Secondary memory, usually disk
– To manage and execute a process, at least a
small portion of its image must be maintained
in main memory
• OS must know the location of each page
of each process image, achieved by
process tables
Process Images
PCB: Process Control
Information
PCB: Process Control
Information
• Data Structuring
– A process may be linked to other process in a queue,
ring, or some other structure. For example, all
processes in a waiting state for a particular priority
level may be linked in a queue. A process may exhibit
a parent-child (creator-created) relationship with
another process. The process control block may
contain pointers to other processes to support these
structures.
Modes of Execution
• User mode
– Less-privileged mode
– User programs typically execute in this mode
• System mode, control mode, or kernel
mode
– More-privileged mode
– Kernel of the operating system
Process Creation
•
•
•
•
•
Assign a unique process identifier
Allocate space for the process
Initialize process control block
Set up appropriate linkages
Create or expand other data structures
When to Switch Process
• Clock interrupt
– process has executed for the maximum
allowable time slice
• I/O interrupt
• Memory fault
– memory address is in virtual memory so it
must be brought into main memory
When to Switch Process
• Trap
– error or exception occurred
– may cause process to be moved to Exit state
• Supervisor call (system call)
– I/O operation such as file open
– Lead to a transfer to an OS routine
How to Switch Process?
Process Switch (1)
• Save context of processor including
program counter and other registers
• Update the process control block of the
process that is currently in the Running
state
• Move process control block to appropriate
queue – ready; blocked; ready/suspend
Process Switch (2)
• Select another process for execution
• Update the process control block of the
process selected
• Update memory-management data
structures
– Depending on how address translation is
managed, memory management data
structures need to be updated
• Restore context of the selected process
Execution of the Operating
System
• Non-process Kernel
– Execute kernel outside of any process
– Operating system code is executed as a
separate entity that operates in privileged
mode
• Execution Within User Processes
– Operating system software within context of a
user process
Execution of the Operating
System
• Process-based operating system
– Implement the OS as a collection of system
processes
Execution of the Operating
System
OS Executes in User Space
• Process Switch
vs.
• Mode Switch
Process Switch (1)
• Save context of processor including
program counter and other registers
• Update the process control block of the
process that is currently in the Running
state
• Move process control block to appropriate
queue – ready; blocked; ready/suspend
Process Switch (2)
• Select another process for execution
• Update the process control block of the
process selected
• Update memory-management data
structures
• Restore context of the selected process
Mode Switch
• Save context of processor including
program counter and other registers
• Update the process control block of the
running process
• Mode changes to kernel mode, finishes the
OS routine
• Mode changes back, restore the context,
and continue the running process in user
mode
UNIX Process States
UNIX Process State
Transition Diagram
fork()
• Process creation in Unix is made by means of the
kernel system call, fork()
• When a process issues a fork request, the OS:
– Allocates a slot in the process table for the new process;
– Assigns a unique process ID to the child process;
– Makes a copy of the process image of the parent, with the
exception of any shared memory;
– Increments counters for any files owned by the parent, to reflect
that an additional process now also owns those files;
– Assigns the child process to the Ready to Run state;
– Returns the ID number of the child to the parent process, and a 0
value to the child process.
Reminder
• 13 students still haven’t formed project
teams
• Team needs to be formed by this Friday!
Appendix
Elements of a Process
Control Block
• How to assign a process an identifier?
• may be an index into the process table
• or process identifier  process table index
• useful for cross-reference by other tables,
inter-process communication
Elements of a Process
Control Block
Review Questions
• What does it mean to preempt a process?
• What is swapping and what is its purpose?
• For what types of entities does the OS
maintain tables of information for
management purposes?
• List three general categories of information
in a process control block.