• No results found

4.2: Process Management

In document QRG_CS.pdf (Page 114-119)

Process:

A process is a program in execution. A process is more than the program code, which is sometimes known as the text section. Rather a process is also considered to be as an ‘active’

entity and a program as a ‘passive’ entity.

A process is sequence of instructions in context of process states. One process cannot affect state of another process directly

In addition to static program text (code) process comprises of following (i) Current value of program counter

(ii) The content of the processor registers

(iii) Process static (temporary data, sub-routine parameters return address etc)i.e stack.

(iv) Global variables.

(v) Open file tables.

Process state:

As a process executes, it changes state. The state of a process is defined by the current activity of that process.

Long-term scheduling adds the following state.

Held (New, Start, Born): A process that has been considered for loading into memory or for execution. Once a process leaves the held state, it does not return to it.

Medium-term scheduling adds the following state

Swapped out: A process that is running may go in swapped out state, it depends on scheduling algorithm.

The state diagram corresponding to these states is presented in Fig.4.2.1 Importance Of Scheduling

1. Scheduling can effect the performance of the system in a big way.

Following are the states in state diagram 4.2.1

New/Start: This is the state before the process is first created. It waits to be admitted in “ready”

state.

Ready: The process is waiting to be assigned to a processor.

Running: Instructions are being executed.

Waiting: The process is waiting for some input to be completed Stop: The process has finished execution

Swapped: Process blocked is placed in swapped state.

 A process that has halted its execution but a record of the process is still maintained by the operating system (on UNIX). Such process referred as zombie process.

Schedulers:

On multiprocessing systems, scheduling is used to determine which process is given to the control of the CPU. Scheduling may be divided into three phases: long-term, medium-term, and short-term.

 Long-term scheduler: controls the degree of multi programming- the number of processes in memory.

 Short term scheduler: Decides which process in ready state showed to be allocated CPU. It can also prempt the process in running state.

 Mid term Scheduler: It swaps out the blocked process from running state to swapped state.

Fig. 4.2.1.State diagram

exit I/O completion

Job scheduler long term scheduling

Short term scheduler

Medium-term scheduling Indefinite waiting is

Deadlock Waiting for I/O

New/Start

Ready

STOP Running

Waiting Swapped

out

The long-term scheduler must make a careful selection. In general, most processes can be described as either I/O bound or CPU bound.

An I/O bound process spends more of its time doing I/O operations than it spend doing computations.

A CPU bound process using more of its time doing computations

The long-term scheduler should select a good process mix of I/O bound and CPU bound processes.

If all processes are I/O bound, the ready queue will be almost always empty, and the short-term scheduler will have little to do.

If all processes are CPU bound, the I/O waiting queue will almost always be empty and device will go unused, and again the system will be unbalanced.

The system with the best performance will have a combination of CPU bound and I/O bound processes.

Context Switching

Switching the CPU to another process requires saving the content of the old process and loading the saved content for the new process. This task is known as a context switch. The context of process is represented in PCB of a process; it includes the value of the CPU register and the process state.

a. The context switching part of the scheduler ordinarily uses conventional load and store operation to save the register contents. This means that each context switch requires (n + m) b × k time unit,

to save the state of a processor with n general registers and m status registers, assuming b store operation are required to save a single register and each store instruction requires K instruction time unit.

b. Total time in context switching = time requires in saving the old process in its PCB + time requires in loading the saved state of the new process schedule to run.

The context of a process is represented in the PCB of a process; it includes the value of the CPU registers, the process state (figure 4.2.2) and memory management information.

Fig. 4.2.2 PCB

Each process is represented in operating system by a process control block (PCB) also called pointer process state

Process identifier PID program counter registers

memory limits list of open files

It contains many pieces of information associated with a specific process, including these:

- Process identifier (unique in the system)

- Process State: The state may be new, ready, running, waiting, stop and so on.

- Program Counter: The program counter indicates the address of the next instruction to be executed for this process.

- CPU Registers: Information about CPU registers.

- CPU Scheduling: It includes a process priority, pointers to scheduling queues etc.

 When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run. Context–switch time is pure overhead, because the system does not do useful work while switching.

 Most number of content switching is done by short term-scheduler.

Each subsequent selection involves one less choice. The total number of possibilities is computed by multiplying all the possibilities at each point, making the answer n!

Inter-process communication, concurrency and synchronization.

The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process is independent if it cannot affect or be affected by the other processes executing in the system.

A process is cooperating if it can affect or be affected by the other processes executing in the system. Clearly, any process that shares data with other processes is a cooperating process.

Process cooperation is used for information sharing, computation speedup, modularity and convenience.

Concurrent execution of cooperating processes requires mechanisms that allow processes to communicate with one another and to synchronize their actions.

Inter-process communication:

Cooperating processes can communicate in a shared memory environment and another way to achieve the same effect is for the operating system to provide the means for cooperating processes to communicate with each other via an interprocess communication (IPC) facility.

IPC provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space.

IPC is particularly useful in a distributed environment where the communicating processes may reside on different computers connected with a network. An example is a chat program used on the World Wide Web.

IPC is best provided by a message-passing system, and message passing systems can be defined in many ways. Different issues when designing message passing systems are as follows:

1. Message Passing system:

Communication among the user processes is accomplished for message passing facility provides at least the two operations:

i. Send (message) ii. Receive (message)

Messages sent by process can be either fixed or variable size. If process P and Q want to communicate, they must send message to and receive message from each other; a communication link must exist between them.

Here are several methods for logically implementing a link and the send( )/ receive( ) operations:

(i) direct or indirect communication

(ii) symmetric or asymmetric communication (iii) automatic or explicit buffering

(iv) send by copy or send by reference (v) fixed – sized or variable – sized message 2. Naming

Processes that want to communicate must have a way to refer to each other.

They can use either direct or indirect communication.

(i) Direct communication:

With direct communication, each process that works to communicate must explicitly name the recipient or sender of the communication. In this scheme, the send and receive primitives are defined as:

a) Send (P, message) – send a message to process P.

b) Receive (Q, message) – receive a message from process Q.

(ii) Indirect communication:

With indirect communication the message are sent to and received from mailboxes, or ports. A mailbox can be viewed abstractly as an object into which messages can be placed by processes and from which messages can be removed.

Two process can communicate only if they share a mailbox. The send and receive primitives are defined as follows:

a) Send (A, message) – send a message to mailbox A.

b) Receive (A, message) – receive a message from mailbox A.

3. Synchronization

Communication between processes takes place by calls to send( ) and receive( )primitives.

Message passing may be either blocking or non-blocking also known as synchronous and asynchronous.

I. Blocking Send: The sending process is blocked until the message is received by the receivingprocess or by the mailbox.

II. Non blocking receive: The receiver retrieves either a valid message or a null.

III. Non blocking send: The sending process sends the message and resumes operation IV. Blocking Receive : The receiver blocks until a message is available

4. Buffering:

Whether the communication is direct or indirect, message exchanged by communicating processes reside in a temporary queue. Basically, such a queue can be implemented in three ways:

I. Zero capacity: The queue has maximum length of 0; thus the link cannot have any message in it

II. Bounded capacity: The queue has finite length n; thus, atmost n messages can reside in it.

III. Unbounded capacity: Any number of messages can wait in it.

Example of IPC systems: POSIX shared memory, mach and windows XP

In document QRG_CS.pdf (Page 114-119)