660 likes | 931 Views
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. Roadmap. How are processes represented and controlled by the OS?
E N D
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.
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.
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
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
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
What is a “process”? • Aprogram 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
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 (?) • ...
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
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 whichgoes next and whenaccording 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 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.
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?
Two-State Process Model: A Simple Model • Process may be in one of two states • Running • Not-running
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
Process Birth and Death See tables 3.1 and 3.2 for more
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
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
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
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)
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
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
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?
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
A Five-state Process Model Ready to exit: A parent may terminate a child process
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
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
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
Reason for Process Suspension Table 3.3 Reasons for Process Suspension
Quiz 3-2 • Identify the 7 possible process states • Describe the following state transitions • Blocked Blocked Suspend • Blocked Suspend Ready Suspend • Ready Suspend Running
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.
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
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
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
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
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
Pentium II EFLAGS Register Also see Table 3.6
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...)
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
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.
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
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?
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
When to Switch Processes? Summary Table 3.8 Mechanisms for Interrupting the Execution of a Process
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
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
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