1 process description and control chapter 3 all multiprogramming os are built around the concept of...
TRANSCRIPT
1
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.
2
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.
3
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
support inter-process communications to share data and exchange information, and user creation of processes
4
Concepts
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
5
The OS Manages Execution of Applications
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
6
What is a “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, and an associated set of system instructions/data
More than just source program and data
7
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
8
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 (?) ...
9
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
10
Dispatcher (short-term scheduler): Process Control and Scheduling
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
11
Quiz 3-1 True or False
A process consists of Source 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
12
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.
13
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?
14
Two-State Process Model: A Simple Model
Process may be in one of two states Running Not-running
15
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
16
Process Birth and Death
Creation Termination New batch job Normal CompletionInteractive Login Memory unavailableCreated by OS to provide a service
Protection error
Spawned by existing process
Operator or OS Intervention
See tables 3.1 and 3.2 for more
17
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
18
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 Create or expand other data structures
19
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
20
Process States
Let us expand to these states: The Running state
The process that gets executed The 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)
21
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) because resources are not allocated or limited
22
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 The process (and its tables) gets deleted
when the data is no more needed
23
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?
24
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 the event for which it was waiting occurs
25
A Five-state Process Model
Ready to exit: A parent may terminate a child process
26
Quiz 3
What are the states when a process is not in the Running state?
Describe the state transitions: Ready Running Running Ready Ready Blocked Running Blocked Blocked Running
27
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
28
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
29
A Seven-state Process Model
30
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
31
Quiz 3-2
Identify the 7 possible process states Describe the following state transitions
Blocked Blocked Suspend Blocked Suspend Ready Suspend Ready Suspend Running
32
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.
33
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
34
OS Control Tables
35
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
36
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
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
37
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
the user who is responsible for the job Identifier of the process that created this process
38
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
39
Pentium II EFLAGS Register
Also see Table 3.6
40
Process Control Information (in PCB) The most important data structure in an
OS. It defines the state of the OS: interprocess communication
may hold flags and signals for IPC process privileges
Ex: access to certain memory locations... memory management
pointers to segment/page tables assigned to this process
resource ownership and utilization resource in use: open files, I/O devices...
history of usage (of CPU time, I/O...)
41
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
42
Queues as linked lists of PCBs
43
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.
44
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 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
45
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?
46
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
47
When to Switch Processes?
Mechanism Cause Use
Interrupt External to the execution of the current instruction
Reaction to an asynchronousexternal event
Trap Associated with the execution of the current instruction
Handling of an error or anexception condition
Supervisor call Explicit request Call to an operating systemfunction
Table 3.8 Mechanisms for Interrupting the Execution of a Process
Summary
48
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
49
Steps in Process (Context) Switching 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
50
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
51
Execution of the Operating System – Is the OS a Process?
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.
52
Execution of the Operating System
53
Non-process Kernel
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
54
Execution within User Processes
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
55
Execution within User Processes
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)
56
Process-based Operating System
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
57
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.
58
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.
59
UNIX Process State Transition Diagram
60
UNIX Process States
61
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.
62
Process Creation
Process creation is by means of the kernel system call - fork( ).
This causes the OS, in Kernel Mode, to:1. Allocate a slot in the process table for the new
process.
2. Assign a unique process ID to the child process.
3. Copy of process image of the parent, with the exception of data and stacks.
63
Process Creation (cont’d)
4. Increment the counters for any files owned by the parent, to reflect that an additional process now also owns those files.
5. Assign the child process to the Ready to Run state.
6. Returns the ID number of the child to the parent process, and a 0 value to the child process.
64
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.
65
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 process management in UNIX SVR4. Assignment 1