CSNB224 – AAG 2007 Process Description and Control Chapter 3.

87
CSNB224 – AAG 2007 Process Description and Control Chapter 3

Transcript of CSNB224 – AAG 2007 Process Description and Control Chapter 3.

CSNB224 – AAG 2007

Process Description and Control

Chapter 3

CSNB224 – AAG 2007

Process Description and Control

All multiprogramming OS are build around the concept of processes

A process is sometimes called a task

CSNB224 – AAG 2007

Major Requirements of an OS

OS must interleave the execution of several processes to maximize processor usage while providing reasonable response time

OS must allocate resources to processes while avoiding deadlock

OS must support inter process communication and user creation of processes

CSNB224 – AAG 2007

Process

A process is created for a program to be executed.

Also called a task

Execution of an individual programInvolves a sequence of instruction within that program.

Can characterize the behavior of a process by listing the sequence (trace of the process) of instructions that execute for that process.

CSNB224 – AAG 2007

Process Trace

Figure 3.1 shows a memory layout of three processes.

Assume no used of virtual memory.

All of the process are fully loaded in the main memory.

There is dispatcher program that switches the processor from one process to another.

CSNB224 – AAG 2007

Process Trace..

Figure 3.2 shows the trace of three processes during early part of their execution.

First 12 instructions executed in processes A and C are shown.

Process B executes four instructions and assume that the fourth instruction invokes and I/O operation for which the process must wait.

CSNB224 – AAG 2007

Process Trace..

Figure 3.3 shows traces from processor’s point of view.

It shows the interleaved traces resulting from the 52 instruction cycles.

Assume OS allowed a process to continue execution for a maximum of 6 instruction cycles, then the process will be interrupted, thus its prevent single process from monopolizing processor time.

CSNB224 – AAG 2007

Process Trace..

The first 6 instructions of process A are executed, followed by a time-out and execution of some code in the dispatcher, which execute 6 instructions before turning control to process B.

After 4 instructions are executed, process B request and I/O action for which it must wait.

CSNB224 – AAG 2007

Process Trace..

The processor stops executing process B and moves on to process C.

After time-out, the processor moves back to process A.

When the process times out, process B is still waiting for I/O operation to complete, so the dispatcher moves on to process C again.

CSNB224 – AAG 2007

Dispatcher

Is an OS program that moves the processor from one process to another

It prevents a single process from monopolizing processor time

It decides who goes next according to a scheduling algorithm (will be discussed on chap 9)

The CPU will always execute instructions from the dispatcher while switching from process A to process B

CSNB224 – AAG 2007

Process Creation

Reasons for process creation:Submission of a batch job

User logs on

Created to provide a service such as printing (ex: printing a file)

Process creates another process (Process Spawning)

CSNB224 – AAG 2007

Process Termination

General reasons for process termination:Batch job issues Halt instruction

User logs off

Process executes a service request to terminate (Quit an application)

Error and fault conditions

Parent process terminate

Parent ask to terminate the child

CSNB224 – AAG 2007

Process Termination..

Specific reasons for process termination:Normal completion

Time limit exceeded

Memory unavailable

Bounds violation

Protection errorexample write to read-only file

Arithmetic error

CSNB224 – AAG 2007

Process Termination..

Time overrun

I/O failure

Invalid instruction

Privileged instruction

Data misuse

Operating system intervention

Parent terminates so child processes terminate

Parent request

CSNB224 – AAG 2007

Two-State Process Model

Process may be in one of two statesRunning

Not-running

CSNB224 – AAG 2007

Two-State Process Model..

When OS creates a new process, it enters that process into Not Running State.

The existence of the process is known by the OS and is waiting for an opportunity to execute.

Running process will be interrupted from time to time and dispatcher will select a new process to run.

Process will moves from the Running state to Not Running state, another process will moves to the Running state.

CSNB224 – AAG 2007

Two-State Process Model..

Each process must be represented in some way so that OS can keep track of it.

There must be some information relating to each process, including current state and location of main memory.

Process that are not running must be kept in some sort of queue, waiting their turn to be execute.

Figure 3.4b suggests a structure to deploy two-state process model.

CSNB224 – AAG 2007

Not-Running Process in a Queue

CSNB224 – AAG 2007

Two-State Process Model..

There is a single queue in which each entry is a pointer to a particular process.

The queue must consist of linked list of data blocks, in which each block represents one process.

The queue is first in first out (FIFO) list and the processor operates in Round robin.

CSNB224 – AAG 2007

Process

Queuing suggested in Figure 3.4b will be effective if all processes were always ready to execute

BUT it is inadequate because some process that are in Not Running state either

ready to execute

blocked because of waiting for I/O operation complete.

CSNB224 – AAG 2007

Process..

By using queuing on fig. 3.4b, the dispatcher has to scan the queue looking for the process that is not blocked and has been in queue the longest.

A way to tackle this situation is to split the Not Running state into two different states, which are:

Ready state: Ready to execute

Blocked state: waiting for I/O

Now, instead of two states we have three states Ready, Running, Blocked

CSNB224 – AAG 2007

Process..

For a good measure, there are another two additional states that will be useful for process management:

New state:OS performed the necessary actions to create the process

– Process ID

– Tables needed to manage the process

but has not yet committed to execute the process (not yet admitted)

– because resources are limited

CSNB224 – AAG 2007

Process..

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

CSNB224 – AAG 2007

A Five-State ModelRunning the process that is currently being executed.

Ready a process that is prepared to execute when given the opportunity.

Blocked a process that cannot execute until some

event occurs, such as the completion of I/O operation.

New a process that has just been created but not being admitted to the pool of executable process by the OS (not being loaded in the main memory).

Exit a process that has been released from the pool of executable processes by the OS, either because it halted or aborted for some reason.

CSNB224 – AAG 2007

Process Transitions

Figure 3.5 indicates the possible state transition as follows:

Null NewA new process is created to execute a program.

New ReadyOS will move the process from New to Ready state when it is prepared to take an additional process.

Ready RunningWhen it is time, the dispatcher selects a new process to run

CSNB224 – AAG 2007

Process Transitions..

Running ExitThe currently Running process is terminated by the OS if he process indicates that it has completed or if it aborts.

Running Readythe running process has expired his time slot

the running process gets interrupted because a higher priority process is in the ready state

CSNB224 – AAG 2007

Process Transitions..

Running BlockedWhen 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 ReadyWhen the event for which it was waiting occurs

CSNB224 – AAG 2007

Process Transitions..

Transitions that are not shown on the five state diagram:

Ready ExitParent may terminate the child process.

Parent terminates, all child processes associated also terminated.

Blocked ExitThe comments under the preceding item apply.

CSNB224 – AAG 2007

Process Transitions..

Figure 3.6 shows the movement of each process among the states from Figure 3.3.

CSNB224 – AAG 2007

Process Transitions..

Figure 3.7a suggests the way in which a queuing discipline might be implemented.

There are two queues:Ready and Blocked queue.

When the event occurs, OS must scan the entire block queue, searching for those processes waiting on that event.

But in large OS, there are hundreds or thousands of processes in that queue.

It is efficient to have a number of queues, one for each event. (Fig 3.7b)

CSNB224 – AAG 2007

Using Two Queues

CSNB224 – AAG 2007

Suspended Processes

So far, all the processes had to be in main memory.

Even with virtual memory, keeping too many processes in main memory will deteriorate the system’s performance.

Processor is faster than I/O so all processes could be waiting for I/O.

Thus even with multiprogramming, processor could be idle most of the time.

CSNB224 – AAG 2007

Suspended Processes..

Solutions for this problem are:1. Main memory could be expanded to

accommodate more processes.Disadvantage:– Cost

– Result for larger processes not more processes.

CSNB224 – AAG 2007

Suspended Processes..

2. SwappingSwap these processes to disk to free up more memory.

Blocked state becomes suspend state when swapped to disk.

The OS may need to suspend some processes, ie: to swap them out to disk.

There are 2 new states:– BLOCKED SUSPEND: blocked processes which

have been swapped out to disk

– READY SUSPEND: ready processes which have been swapped out to disk

CSNB224 – AAG 2007

One Suspend State

CSNB224 – AAG 2007

Two Suspend States

Process transition: Check text book page 122 and 123

CSNB224 – AAG 2007

Characteristics of Suspended Process

1. The process is not immediately available for execution.

2. The process may or may not be waiting for an event

3. The process was placed in a suspended state by an agent; either itself, the OS, or the parent process. For the purpose of preventing its execution.

4. The process may not be removed from this state until the agent explicitly orders the removal.

CSNB224 – AAG 2007

Reasons for Process Suspension

CSNB224 – AAG 2007

Process Description

OS control events within the computer system.Schedules and dispatches processes for execution by the processor, allocates resources to processes, and responds to requests by user programs for basic services.

OS as an entity that manages the use of system resources by processes. (figure 3.9)

CSNB224 – AAG 2007

Process Description..

Figure 3.9 shows multiprogramming environment.

There are a number of processes (P1,…Pn) that have been created and exists in virtual memory at one time.

Each process during the course of its execution, need access to certain system resources, including the processor, I/O devices and main memory.

CSNB224 – AAG 2007

Process Description..

From the figure:Process P1 is running: at least part of it in the main memory and it has control of two I/O devices.

Process P2 also in main memory but is blocked waiting for an I/O device allocated for P1.

Process Pn has been swapped out and is therefore suspended.

CSNB224 – AAG 2007

Operating System Control Structures

For the OS to manage the 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

Figure 3.10 shows four different types of tables maintained by the OS:

Memory tables

I/O tables

File tables

Process tables

CSNB224 – AAG 2007

Memory Tables

Used to keep track both main and secondary memory.

Include the following information: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

CSNB224 – AAG 2007

I/O Tables

To manage the I/O devices and channels of the computer system.

At any time, I/O device is available or assigned to a particular process

If an I/O operation is in progress, the OS needs to know:

the status of the I/O

location in main memory being used as the source or destination of the I/O transfer

CSNB224 – AAG 2007

File Tables

The tables provide information about:Existence of files

Location on secondary memory

Current Status

Other attributes

Sometimes this information is maintained by a file-management system

CSNB224 – AAG 2007

Process Table

To manage process, the OS must know:Where process is located

Attributes necessary for its managementProcess ID

Process state

CSNB224 – AAG 2007

Process Location

Process includes set of programs to be executed consists of:

Data locations for local and global variables

Any defined constants

Stack

Process also will have a collection of attributes used by OS for process control.

Known as Process Control Block (PCB)

All of these elements of a process is called Process Image.

CSNB224 – AAG 2007

Process Location

Location of process image will depend on the memory management scheme being used.

Simplest case, the process image is maintained as a contiguous block of main memory.

This block is maintained in secondary memory

A small portion of process image must be maintained in main memory, so the OS can manage the process.

CSNB224 – AAG 2007

Process Location..

To execute the process, the entire process image or a portion of process must be loaded into main memory.

Thus the OS needs to know the location of each process on disk and in main memory.

CSNB224 – AAG 2007

Process Attributes

The Process Control Block information can be grouped into three general categories:1. Process identification

2. Processor state information

3. Process control information

CSNB224 – AAG 2007

Process Identification

A few numeric identifiers may be used Unique process identifier

Indexes (directly or indirectly) into the primary process table

User identifier The user who is responsible for the job

Identifier of the process that created the process

CSNB224 – AAG 2007

Processor State Information

Contents of processor register.User-visible registers

Control and status register

Stack pointersProgram status word (PSW)

Contains status information

Example: the EFLAGS register on Pentium machines

CSNB224 – AAG 2007

Process Control Information

Scheduling and state informationProcess state (ie: running, ready, blocked...)

Priority of the process

Event for which the process is waiting (if blocked)Data structuring information

may hold pointers to other PCBs for process queues, parent-child relationships and other structures

CSNB224 – AAG 2007

Process Control Information

Inter-process communicationmay hold flags and signals for IPC

Process privilegesEx: access to certain memory locations...

Memory managementpointers to segment/page tables assigned to this process

Resource ownership and utilizationresource in use: open files, I/O devices...

history of usage (of CPU time, I/O...)

CSNB224 – AAG 2007

Process Control Block

Process identificationIdentifiers

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

CSNB224 – AAG 2007

Process Control Block

Processor State InformationUser-Visible Registers

A user-visible register is one that may be referenced by means of the machine language that the processor executes. Typically, there are from 8 to 32 of these registers, although some RISC implementations have over 100.

CSNB224 – AAG 2007

Process Control Block

Processor State InformationControl 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

CSNB224 – AAG 2007

Process Control Block

Processor State InformationStack 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.

CSNB224 – AAG 2007

Process Control Block

Process Control InformationScheduling and State InformationThis 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

CSNB224 – AAG 2007

Process Control Block

Process Control InformationData 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.

CSNB224 – AAG 2007

Process Control Block

Process Control InformationInterprocess 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 PrivilegesProcesses 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.

CSNB224 – AAG 2007

Process Control Block

Process Control InformationMemory Management

This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process.

Resource Ownership and UtilizationResources 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.

CSNB224 – AAG 2007

Modes of Execution

To provide protection to PCB (and other OS data) most processors support at least 2 execution modes:

1. User modeLess-privileged mode

User programs typically execute in this mode

2. System mode, control mode, or kernel modeMore-privileged mode

Kernel of the operating system

CSNB224 – AAG 2007

Modes of Execution

Reasons using two modes because it is necessary to protect the OS and other OS tables such as PCB form interference by user program.

In the kernel mode, the software has complete control of the processor and all its instruction, registers and memory.

This level of control is not necessary and for safety is not desirable for user programs.

CSNB224 – AAG 2007

Process Creation

Steps involved in creating the process:1. Assign a unique process identifier

2. Allocate space for the process

3. Initialize the process control block

4. Set the appropriate linkages

5. Create or expand other data structuresEx: maintain an accounting file

CSNB224 – AAG 2007

Process Switching

Process switch from one state to another.Eg: A running process is interrupted and the OS assigns other process to the Running state and turns control over the process.

Several design issues raised:1. What events trigger a process switch?

2. Must recognize the distinction between mode switching and process switching?

3. What must the OS do to the various data structures under its control to achieve process switch?

CSNB224 – AAG 2007

When to Switch a Process

A process switch may occur whenever the OS has gained control of CPU. i.e. when:

Supervisor Callexplicit request by the program (ex: file open). The process will probably be blocked

TrapAn 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 Interrupt Handler

CSNB224 – AAG 2007

When to Switch a Process

Example of interrupt:Clock interrupt

process has executed for the maximum allowable time slice and is transferred to Ready state.

I/O interruptfirst move the processes that where waiting for this event to the ready (or ready suspend) state

then resume the running process or choose a process of higher priority

CSNB224 – AAG 2007

When to Switch a Process

Memory faultmemory address is in virtual memory so it must be brought into main memory

Thus move this process to Blocked state (waiting for I/O to complete)

CSNB224 – AAG 2007

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 (ref. Chap 1)

This is called mode switching (user to kernel mode when going into Interrupt Handler)

Less overhead: no need to update the PCB like for process switching

CSNB224 – AAG 2007

Change of Process State

Steps involved in process switching:1. Save context of processor including program

counter and other registers

2. Update the process control block of the process that is currently running

3. Move process control block to appropriate queue - ready, blocked

4. Select another process for execution

5. Update PCB of the selected process

CSNB224 – AAG 2007

Change of Process State

6. Update memory-management structures.

7. Restore CPU context from that of the selected process.

CSNB224 – AAG 2007

Execution of the OS

Up to now, the process we were referring to is “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.

Figure 3.14 illustrates a range of approaches that are found in various contemporary OS.

CSNB224 – AAG 2007

Non-process Kernel

The concept of process applies only to user programsOS code is executed as a separate entity in privilege modeOS code never gets executed within a process

CSNB224 – AAG 2007

Execution within User Process

Virtually all OS code gets executed within the context of a user processOn 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

CSNB224 – AAG 2007

Execution within User Process

• Check figure 3.15.

• 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)

CSNB224 – AAG 2007

Execution of the Operating System

Process-Based Operating Systemmajor kernel functions are separate processes

Useful in multi-processor or multi-computer environment