lecture5
TRANSCRIPT
1
Process Description and Control
2
Requirements of an Operating System
• Interleave the execution of multiple processes to maximize processor utilization while providing reasonable response time
• Allocate resources to processes
• Support inter process communication and user creation of processes
3
Manage Execution of Applications
• Resources made available to multiple applications
• Processor is switched among multiple applications
• The processor and I/O devices can be used efficiently
4
Process
• A program in execution– Sometimes also called a task
• 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
5
What is a process?
• Components of a process include
– the program to be executed (i.e. code)– data on which the program will execute– Links to resources used by the program such as
memory and file(s)– information on the process’ execution status
• Is a process the same as a program?
6
Processes vs. Programs
• No!, it is both more and less.
• more—a program is just part of a process’ context.
• A single program can be executed by two different users – i.e. the same program can be part of >1 process
• less—a program may actually create several processes.– e.g. Compilation in Unix
7
Programming: uni- vs multi-
• Some systems allow the execution of only one process at a time (e.g. early PC's). These are called uniprogramming systems.
• Others allow more than one process to share the CPU over time: multi-programming systems
• NOT multiprocessor systems!– Still only 1 CPU
• In a multiprogramming system, the OS switches the CPU from process to process running each for some “time quantum”
• The CPU is actually running one and only one process at a time.
8
• Multitasking can be conveniently described in terms of multiple processes running in (pseudo) parallel
9
Execution model
• Operating system designers have developed a model that makes multiple process software relatively easy to understand
• In this model, each runnable piece of software on the computer—even components of the operating system itself—is organized into one or more sequential processes
• Instructions are executed sequentially in each process• Each process can be visualized as a block of code
with a pointer identifying the next instruction to be executed.
10
Sharing the CPU
• How can several processes share one CPU?
• The operating system takes care of this by making sure:– each process gets a chance to run—scheduling.– they do not modify each other’s state—protection.
• Note that these are two issues that come up over and over whenever we think about sharing resources…
11
• The O/S has to multiplex resources to the processes
• a number of processes have been created• each process during the course of its execution
needs access to system resources: CPU, main memory, I/O devices
12
• A process image consists of three components1.an executable program
2.the associated data needed by the program
3.the execution context of the process, which contains all information the O/S needs to manage the process (ID, state, CPU registers, stack, etc.)
13
14
Process Elements
• Identifier• State• Priority• Program counter• Memory pointers• Context data• I/O status information• Accounting information
15
Process Control Block
• Contains the process elements
• Created and manage by the operating system
• Allows support for multiple processes
16
The Process Control Block (PCB)• The Process Control Block
(PCB) Typical process image implementation
• is included in the context, along with the stack
• is a “snapshot” that contains all necessary and sufficient data to restart a process where it left off (ID, state, CPU registers, etc.)
• is one entry in the operating system’s process table (array or linked list)
17
Process Control Block
18
Trace of Process
• Sequence of instruction that execute for a process
• Dispatcher switches the processor from one process to another
• The dispatcher is a routine program in kernel memory space
19
Example ExecutionMain MemoryAddress
Dispatcher
Process A
Process B
Process C
Program Counter0
100
5000
8000
8000
12000
Figure 3.2 Snapshot of Example Execution (Figure 3.4)at Instruction Cycle 13
20
Trace of Processes
21
22
A Dispatcher
23
Two-State Process Model
• Process may be in one of two states– Running
– Not-running
24
Not-Running Process in a Queue• How does the O/S keep track of processes and
states? • By keeping a queue of pointers to the process
control blocks • The queue can be implemented as a linked list if
each PCB contains a pointer to the next PCB
25
Process Creation• Some events that lead to process creation
• The system boots– when a system is initialized, several background
processes or “daemons” are started (email, logon, etc.)
• A user requests to run an application– by typing a command in the CLI shell or double-
clicking in the GUI shell, the user can launch a new process
26
Process Creation
• An existing process spawns a child process
– for example, a server process (print, file) may create a new process for each request it handles
– The init daemon waits for user login and spawns a shell
• A batch system takes on the next job in line
27
Process Hierarchies
• Parent creates a child process, child processes can create their own processes
• Forms a hierarchy– UNIX calls this a "process group"
• Windows has no concept of process hierarchy– all processes are created equal
28
Process Creation
29
Process Termination• Some events that lead to process termination (exit)• Regular completion, with or without error code
– The process voluntarily executes an exit(err) system call to indicate to the O/S that it has finished
• Fatal error (uncatchable or uncaught)– Service errors: no memory left for allocation, I/O error, etc.– Total time limit exceeded
– Arithmetic error, out-of-bounds memory access, etc.
• Killed by another process via the kernel– The process receives a SIGKILL signal– In some systems the parent takes down its children with it
30
Process Pause / Dispatch
• Some events that lead to process pause / dispatch• I/O wait
– a process invokes an I/O system call that blocks waiting for the I/O device: the O/S puts the process in “Not Running” mode and dispatches another process to the CPU
• Preemptive timeout– the process receives a timer interrupt and relinquishes
control back to the O/S dispatcher: the O/S puts the process in “Not Running” mode and dispatches another process to the CPU
– Not to be confused with “total time limit exceeded”, which leads to process termination
31
Problem with the two-state model
• Some “Not Running” processes are blocked (waiting for I/O, etc.)
• The O/S wastes time scanning the queue for ready →solution: divide “Not Running” into “Ready” and “Blocked”
32
Process States• How does the O/S keep track of three process
states?
• Queuing diagram of a three-state Blocked/Ready process model
• by keeping an extra queue for blocked processes
33
• To further reduce scanning, blocked processes can be placed in separate queues depending on the event type
34
Representation of Process Scheduling
35
36
37
38
Process States
• Two independent concepts ×two values each – Whether a process is waiting on an event (is “Blocked”) or
not– Whether a process has been swapped out of main memory
(is “Suspended”) or not
• =Four combined states– “Ready”: the process is in memory and available for
execution– “Blocked”: the process is in main memory awaiting an event– “Suspended Blocked”: the process is in secondary memory
and awaiting an event– “Suspended Ready”: the process is in secondary memory but
is available for execution as soon as it is loaded into memory
39
Process Suspension
• Note: Release of memory by swapping is not the only motivation for suspending processes.
• Various background processes may also be turned off and on, depending on CPU load, suspicion of a problem, some periodical timer or by user request.
40
Operating System Control Structures
• The O/S has to multiplex resources to the processes
• To do this, the O/S must keep information about the current status of each process and resource
• Tables are constructed for each entity the operating system manages
41
Memory Tables
• 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
42
I/O Tables
• 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
43
File Tables
• Existence of files
• Location on secondary memory
• Current Status
• Attributes
• Sometimes this information is maintained by a file management system
44
Process Table
• Where process is located
• Attributes in the process control block– Program– Data– Stack
45
Memory
Devices
Files
Processes
Process 1
Memory Tables
ProcessImage
Process1
ProcessImage
Processn
I/O Tables
File Tables
Figure 3.11 General Structure of Operating System Control Tables
Primary Process Table
Process 2
Process 3
Process n
46
Process Control Block
• Process identification– Identifiers
• Numeric identifiers that may be stored with the process control block include
– Identifier of this process
– Identifier of the process that created this process (parent process)
– User identifier
47
Process Control Block
• Processor State Information– User-Visible Registers
• A user-visible register is one that may be referenced by means of the machine language that the processor executes while in user mode. Typically, there are from 8 to 32 of these registers, although some RISC implementations have over 100.
48
Process Control Block
• Processor State Information– Control and Status Registers
These are a variety of processor registers that are employed to control the operation of the processor. These include
• Program counter: Contains the address of the next instruction to be fetched
• Condition codes: Result of the most recent arithmetic or logical operation (e.g., sign, zero, carry, equal, overflow)
• Status information: Includes interrupt enabled/disabled flags, execution mode
49
Process Control Block
• Processor State Information– Stack Pointers
• Each process has one or more last-in-first-out (LIFO) system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls. The stack pointer points to the top of the stack.
50
Process Control Block
• Process Control Information– Scheduling and State Information
This is information that is needed by the operating system to perform its scheduling function. Typical items of information:•Process state: defines the readiness of the process to be scheduled for execution (e.g., running, ready, waiting, halted).•Priority: One or more fields may be used to describe the scheduling priority of the process. In some systems, several values are required (e.g., default, current, highest-allowable)•Scheduling-related information: This will depend on the scheduling algorithm used. Examples are the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running.•Event: Identity of event the process is awaiting before it can be resumed
51
Process Control Block
• 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.
52
Process Control Block
• Process Control Information– Interprocess Communication
• Various flags, signals, and messages may be associated with communication between two independent processes. Some or all of this information may be maintained in the process control block.
– Process Privileges• Processes are granted privileges in terms of the memory
that may be accessed and the types of instructions that may be executed. In addition, privileges may apply to the use of system utilities and services.
53
Process Control Block
• Process Control Information– Memory Management
• This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process.
– Resource Ownership and Utilization• Resources controlled by the process may be indicated,
such as opened files. A history of utilization of the processor or other resources may also be included; this information may be needed by the scheduler.
54
55
56
57
58
59
60
61
62
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
– Process executes in privileged mode when executing operating system code
63
UNIX Process States