The process - Systems and Computer Engineering
Download
Report
Transcript The process - Systems and Computer Engineering
Process Description and Control
Chapter 3
All multiprogramming OS are built around the concept of
processes.
A process is also called a task or a job.
1
Roadmap
How
are processes represented and
controlled by the OS?
Process
states and state transitions which
characterize the behaviour of processes.
Data structures used to manage processes.
Ways in which the OS uses these data structures
to control process execution.
Overview of process management in UNIX SVR4.
2
OS Requirements for Multiprogramming
Process Management
Process management: fundamental OS task
OS must:
interleave the execution of several processes to
maximize CPU usage while providing reasonable
response time
allocate resources to processes while
protecting processes
enabling synchronization of processes
avoiding deadlock
3
support inter-process communications to share data
and exchange information, and user creation of
processes
Concepts
4
Computer platforms consists of a collection
of hardware resources
Computer applications are developed to
perform some task
OS provides an interface for applications to
use hardware resources
OS provides a representation of resources
that can be requested and accessed by
application
The OS Manages
Execution of Applications
5
Resources are made available to multiple
applications
The processor or CPU is switched among
multiple applications
The processor and I/O devices can be used
efficiently
What is a “process”?
6
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, and an associated set of
system instructions/data
More than just source program and data
Process Image
A process is comprised of:
Executable
program (possibly shared)
A set of data
A number of attributes describing the context of
the process: process-specific context
Process
state
Processor status
Other resource data
Stacks
7
Process Elements
While a process is running, it has a number
of elements, including:
Identifier
State
Priority
Program
counter
Memory pointers
Context data
I/O status information
Accounting information (?)
...
8
Process Control Block (PCB)
Contains the process
elements
Created and managed by
the operating system
Allows support for
multiple processes
Process
switching
Interrupt handling for
process switching
9
Dispatcher (short-term scheduler):
Process Control and Scheduling
10
Is part of the OS that moves the processor
from one process to another
It prevents a single process from
monopolizing processor time
It decides which goes next and when
according to a scheduling algorithm (to be
discussed later)
The CPU will always execute instructions
from the dispatcher while switching from
process A to process B
Quiz 3-1
True or False
A
process consists of
Source
11
program
Data/stacks
Context/attributes
Give 2 attributes of a process
What are stacks for?
A PCB (Process Control Block) is allocated for
Each process
Multiple processes that are running at the same time
A few processes that shared the same program and
data
Roadmap
How
are processes represented and
controlled by the OS.
Process
states and state transitions which
characterize the behaviour of processes.
Data structures used to manage processes.
Ways in which the OS uses these data structures
to control process execution.
Discuss process management in UNIX SVR4.
12
Exercise
What
are some of the states for a Student
Registrar System?
What are some valid state transitions for a
Student Registrar System?
What are the similarities and differences between
a Student Registrar System and an OS?
13
Two-State Process Model: A Simple
Model
Process may be in one of two states
Running
Not-running
14
Process Queue and Dispatcher
… processes moved by the dispatcher of the OS to the CPU then back to
the queue until the task is competed
15
Process Birth and Death
Creation
New batch job
Interactive Login
Created by OS to
provide a service
Spawned by existing
process
Termination
Normal Completion
Memory unavailable
Protection error
Operator or OS
Intervention
See tables 3.1 and 3.2 for more
16
Process Creation
The OS builds a data structure to manage the
process
Traditionally, the OS creates all processes
But
it can be useful to let a running process to
create another one
fork() in Unix/Linux (in Lab 2 and assignment 1)
This action is called process spawning
Parent
Process is the original, creating process
Child Process is the new process
17
Process Creation
Assign a unique process identifier
Allocate space for the process image
Initialize process control block
many
default values (ex: state is New, inherited
values from Parent, …)
Set up appropriate linkages
Ex:
add new process to linked list used for the
scheduling queue
18
Create or expand other data structures
Process Termination
There must be some way that a process can
indicate completion.
This indication may be:
A
HALT instruction generating an interrupt alert to
the OS.
A user action (e.g. log off, quitting an application)
A fault or error
Parent process terminating
19
Process States
Let us expand to these states:
The
Running state
The
The
process that gets executed
Not Running state
The
Ready state
• any process that is ready to be executed
The
Blocked state
• when a process cannot execute until some event occurs
(ex: the completion of an I/O)
20
Other Useful States
The New state
OS
has performed the necessary initialization
actions to create the process
has
created a process identifier
has created data structures or tables needed to
manage the process
but
has not yet committed to execute the
process (not yet admitted)
21
because resources are not allocated or limited
Other Useful States
The Exit state
Termination
moves the process to this state
It is no longer eligible for execution
Tables and other info are temporarily
preserved for auxiliary program
Ex:
accounting program that cumulates resource
usage for billing the users
22
The process (and its tables) gets deleted
when the data is no more needed
Process Transitions
Ready --> Running
When
it is time, the dispatcher (scheduler)
selects a new process to run
Running --> Ready
the
running process has expired his time slot
the running process gets interrupted because a
higher priority process is in the ready state
How about I/O operations?
Is
the process still Ready?
What is the new state?
23
Process Transitions
Running --> Blocked
When
a process requests something for which
it must wait
a
service that the OS is not ready to perform
an access to a resource not yet available
initiates I/O and must wait for the result
waiting for a process to provide input (IPC)
Blocked --> Ready
When
24
the event for which it was waiting occurs
A Five-state Process Model
Ready to exit: A parent may terminate a child process
25
Quiz 3
What are the states when a process is not
in the Running state?
Describe the state transitions:
Running
Running Ready
Ready Blocked
Running Blocked
Blocked Running
Ready
26
Suspended Processes – The Need for
Swapping
Processor is faster than I/O, so all processes
could be waiting for I/O
Swap
these processes to disk to free up more
memory and use processor on more processes
Ready/Blocked state becomes suspend state
when swapped to disk
Two new states
Blocked
Suspend
Ready Suspend
27
New state transitions (mid-term scheduling)
Blocked --> Blocked Suspend
When
all processes are blocked, the OS will
make room to bring a ready process in memory
Blocked Suspend --> Ready Suspend
When
the event for which it as been waiting
occurs (state info is available to OS)
Ready Suspend --> Ready
when
“less” ready processes in main memory
Ready--> Ready Suspend (unlikely)
When
there are no blocked processes and
must free memory for adequate performance
28
A Seven-state Process Model
29
Reason for Process Suspension
Reason
Comment
Swapping
The OS needs to release sufficient main memory to
bring in a process that is ready to execute.
Other OS Reason
OS suspects process of causing a problem.
Interactive User
Request
e.g. debugging or in connection with the use of a
resource.
Timing
A process may be executed periodically (e.g., an
accounting or system monitoring process) and may
be suspended while waiting for the next time.
Parent Process
Request
A parent process may wish to suspend execution of
a descendent to examine or modify the suspended
process, or to coordinate the activity of various
descendants.
Table 3.3 Reasons for Process Suspension
30
Quiz 3-2
Identify
the 7 possible process states
Describe the following state transitions
Blocked Suspend
Blocked Suspend Ready Suspend
Ready Suspend Running
Blocked
31
Roadmap
How
are processes represented and
controlled by the OS.
Process
states and transitions which
characterize the behaviour of processes.
Data structures used to manage processes.
Ways in which the OS uses these data structures
to control process execution.
Discuss process management in UNIX SVR4.
32
Operating System Control Structures
For the OS is to manage processes and resources,
it must have information about the current status
of each process and resource.
Tables are constructed for each entity the
operating system manages
Memory tables (to be discussed later)
I/O tables (to be discussed later)
File tables (to be discussed later)
Process tables: For OS to manage processes, OS
needs to know details:
Current state
Process ID
Location in memory
etc
33
OS Control Tables
34
Process Image
User program
User data
Stack(s)
for
procedure calls and parameter passing
Process Control Block (execution context)
Data
needed (process attributes) by the OS to
control the process. This includes:
Process
identification information
Processor state information
Process control information
35
Location of the Process Image
Each process image is in virtual memory
may
not occupy a contiguous range of
addresses (depends on the memory
management scheme used)
both a private and shared memory address
spaces are used
36
The location if each process image is
pointed to by an entry in the Primary
Process Table
For the OS to manage the process, at least
part of its image must be loaded into main
memory
Process Identification (in the PCB)
Each process is assigned a unique numeric
identifier.
Many of the other tables controlled by the OS may
use process identifiers to cross-reference process
tables
A few other numeric identifiers are also used, e.g.,
User identifier
37
the user who is responsible for the job
Identifier of the process that created this process
Processor State Information (in PCB)
Contents of processor registers
User-visible
registers
Control and status registers
Stack pointers
Program status word (PSW)
contains
status information
Example: the EFLAGS register on Pentium
machines
38
Pentium II
EFLAGS Register
Also see Table 3.6
39
Process Control Information (in PCB)
The most important data structure in an
OS. It defines the state of the OS:
interprocess
may
hold flags and signals for IPC
process
Ex:
communication
privileges
access to certain memory locations...
memory
management
pointers
to segment/page tables assigned to this
process
resource
ownership and utilization
resource
history
40
in use: open files, I/O devices...
of usage (of CPU time, I/O...)
Process Control Information (in PCB)
Scheduling and state information
Process
state (i.e., running, ready, blocked...)
Priority of the process
Event for which the process is waiting (if
blocked)
Data structure information
may
hold pointers to other PCBs for process
queues, parent-child relationships and other
structures
41
Queues as linked lists of PCBs
42
Roadmap
How
are processes represented and
controlled by the OS.
Process
states which characterize the behaviour
of processes.
Data structures used to manage processes.
Ways in which the OS uses these data structures
to control process execution.
Discuss process management in UNIX SVR4.
43
Modes of Execution
To provide protection to PCBs (and other
OS data) most processors support at least
2 execution modes:
Privileged
mode (a.k.a. system mode, kernel
mode, supervisor mode, control mode )
manipulating
control registers, primitive I/O
instructions, memory management...
User
44
mode
For this the CPU provides a (or a few)
mode bit which may only be set by an
interrupt or trap or OS call
Switching Processes
Key for multiprogramming
Several design issues are raised regarding
process switching
What
events trigger a process switch?
We must distinguish between mode switching
and process switching.
What must the OS do to the various data
structures under its control to achieve a process
switch?
45
When to Switch a Process ?
A process switch may occur whenever the
OS has gained control of CPU, i.e., when:
Supervisor
Call
explicit
request by the program (ex: file open).
The process will probably be blocked
Trap
An
error resulted from the last instruction. It may
cause the process to be moved to the Exit state
Interrupt
the
cause is external to the execution of the
current instruction. Control is transferred to IH
46
When to Switch Processes?
Summary
Mechanism
Cause
Use
Interrupt
External to the execution of the
current instruction
Reaction to an asynchronous
external event
Trap
Associated with the execution
of the current instruction
Handling of an error or an
exception condition
Supervisor call
Explicit request
Call to an operating system
function
Table 3.8 Mechanisms for Interrupting the Execution of a Process
47
Mode Switching
It may happen that an interrupt does not
produce a process switch
The control can just return to the interrupted
program
Then only the processor state information
needs to be saved on stack
This is called mode switching (user to kernel
mode when going into IH)
What is the benefit?
Less
overhead: no need to update the PCB like
for process switching
48
Steps in Process (Context) Switching
49
Save context of processor including program
counter and other registers
Update the PCB of the running process with its new
state and other associate info
Move PCB to appropriate queue - ready, blocked
Select another process for execution
Update PCB of the selected process
Update memory-management data structures
Restore CPU context from that of the selected
process
Quiz 3-4
Identify the causes for process switching
Processing switching: Reorder the tasks:
Update
PCB of the selected process
Save context of processor including program counter
and other registers
Move PCB to appropriate queue - ready, blocked
Select another process for execution
Update the PCB of the running process with its new
state and other associate info
Restore CPU context from that of the selected process
Update memory-management data structures
50
Execution of the Operating System –
Is the OS a Process?
51
Up to now, by process we were referring to
“user process”
If the OS is just like any other collection of
programs, is the OS a process?
If so, how it is controlled?
The answer depends on the OS design.
Execution of the Operating System
52
Non-process Kernel
53
The concept of process applies only to user
programs
OS code is executed as a separate entity in
privileged mode
OS code never gets executed within a process
Execution within User Processes
54
Most OS code gets executed within the
context of a user process
On Interrupts, Traps, System calls: the CPU
switch to kernel mode to execute OS routine
within the context of user process (mode
switch)
Control passes to process switching functions
(outside processes) only when needed
Execution within User Processes
55
OS code and data are
in the shared address
space and are shared
by all user processes
Separate kernel stack
for calls/returns when
the process is in
kernel mode
Within a user process,
both user and OS
programs may execute
(more than 1)
Process-based Operating System
56
The OS is a collection of system processes
Major kernel functions are separate processes
Small amount of process switching functions is executed
outside of any process
Design that easily makes use of multiprocessors
Roadmap
How
are processes represented and
controlled by the OS.
Process
states which characterize the behaviour
of processes.
Data structures used to manage processes.
Ways in which the OS uses these data structures
to control process execution.
Discuss process management in UNIX SVR4.
57
Unix SVR4 System V Release 4
Uses the model of fig3.15b where most of the
OS executes in the user process
System Processes - Kernel mode only
User Processes
User mode to execute user programs and utilities
Kernel mode to execute instructions that belong to
the kernel.
58
UNIX Process State Transition Diagram
59
UNIX Process States
60
A Unix Process
A process in UNIX is a set of data structures
that provide the OS with all of the
information necessary to manage and
dispatch processes.
See Table 3.10 which organizes the elements
into three parts:
user-level
context,
register context, and
system-level context.
61
Process Creation
Process creation is by means of the kernel
system call - fork( ).
This causes the OS, in Kernel Mode, to:
1.
2.
3.
62
Allocate a slot in the process table for the new
process.
Assign a unique process ID to the child process.
Copy of process image of the parent, with the
exception of data and stacks.
Process Creation (cont’d)
4.
5.
6.
63
Increment the counters for any files owned by
the parent, to reflect that an additional process
now also owns those files.
Assign 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.
After Creation
After creating the process the Kernel can do
one of the following, as part of the dispatcher
routine:
Stay
in the parent process.
Transfer control to the child process
Transfer control to another process.
64
Review
The
key component for process representation and
control?
PCB
Identify
the elements that characterize the behaviour of
processes.
States and state transitions
Data
structures used to manage processes.
Control structure and various tables
Ways
in which the OS uses these data structures to
control process execution.
Process switching and different process models
Discuss
65
process management in UNIX SVR4.
Assignment 1