3 process management

26
Processes 1. Process Concept 2. Process Scheduling 2. Process Scheduling 3. Operations on Processes 4. Interprocess Communication

Upload: loganathan-ramasamy

Post on 14-Dec-2014

1.054 views

Category:

Education


0 download

DESCRIPTION

Process Management

TRANSCRIPT

Page 1: 3 process management

Processes

1. Process Concept

2. Process Scheduling2. Process Scheduling

3. Operations on Processes

4. Interprocess Communication

Page 2: 3 process management

1. Process Concept• An operating system executes a variety of

programs:– Batch system – jobs– Time-shared systems – user programs or tasks

• The terms job and process are used almostinterchangeably

• 1.1 The Process– a program in execution; process execution must– a program in execution; process execution must

progress in sequential fashion– A process includes:

• program counter - current activity• Stack - temporary data(function, parameters, return

addresses, and local variables)• data section - global variables• heap - dynamically allocated memory during process run

time

2Loganathan R, CSE, HKBKCE

Page 3: 3 process management

1. Process Concept Contd…

• A program by itself is not aprocess

• A program is a passiveentity, a file containing a listof instructions stored ondisk (often called an

Process in Memory

3Loganathan R, CSE, HKBKCE

disk (often called anexecutable file)

• A process is an active entity,with a program counterspecifying the nextinstruction to execute and aset of associated resources

Page 4: 3 process management

1.2 Process State• As a process executes, it changes state, state of a process is defined

in part by the current activity of that process– new: The process is being created– running: Instructions are being executed– waiting: The process is waiting for some event to occur– ready: The process is waiting to be assigned to a processor– terminated: The process has finished execution

1. Process Concept Contd…

– terminated: The process has finished execution

4Loganathan R, CSE, HKBKCE

Page 5: 3 process management

1.3 Process Control Block (PCB) also called task control block containsinformation associated with each process

• Process state - new, ready, running, waiting, halted• Program counter - the address of the next instruction to be executed• CPU registers - accumulators, index registers, stack pointers, and general-

purpose registers• CPU scheduling information - process priority, pointers to scheduling queues

and scheduling parameters• Memory-management information - value of the base and limit registers,

the page tables, or the segment tables

1. Process Concept Contd…

the page tables, or the segment tables• Accounting information - the amount of CPU and real time used, time limits,

account numbers, job or process numbers• I/O status information - list of I/O devices allocated to the process, a list of

open files

1.4 Threads• A process is a program that performs a single thread of execution• Single thread of control allows the process to perform only one task at one

time• Multiple threads of execution perform more than one task at a time

5Loganathan R, CSE, HKBKCE

Page 6: 3 process management

Process ControlBlock (PCB)

1. Process Concept Contd…

CPU Switch From Process to Process

6Loganathan R, CSE, HKBKCE

Page 7: 3 process management

• Multiprogramming and Time Sharing switch theCPU among processes so frequently that users caninteract with each program

• Process Scheduler selects an available process(possibly from a set of several available processes)for program execution on the CPU

2.1 Scheduling Queues

2. Process Scheduling

2.1 Scheduling Queues• Job queue – set of all processes in the system• Ready queue – set of all processes residing in

main memory, ready and waiting to execute• Device queues – set of processes waiting for an

I/O device. Each device has its own device queue• Processes migrate among the various queues

7Loganathan R, CSE, HKBKCE

Page 8: 3 process management

Ready Queue And Various I/O Device Queues

2. Process Scheduling Contd…

8Loganathan R, CSE, HKBKCE

Page 9: 3 process management

2. Process Scheduling Contd…

• Representation of Process Scheduling is Queuing Diagram• Rectangle represents a queue, Circle represent the resource and arrow indicate the flow

of processes• New process is initially put in the ready queue and waits there until it is selected for

execution, or is dispatched• Once the process is allocated the CPU and is executing, the events may occur are:The process could issue an I/O request and then be placed in an I/O queueThe process could create a new sub process and wait for the sub process's TerminationThe process could be removed forcibly from the CPU, as a result of an interrupt, and be

put back in the ready queue

9Loganathan R, CSE, HKBKCE

Page 10: 3 process management

2.2 Schedulers• The selection processes from those queue is carried out by the

appropriate scheduler• Long-term scheduler (or job scheduler) – selects which processes should

be brought into the ready queue Executes much less frequently, minutes may separate the creation of one

new process and the next controls the degree of multiprogramming (the number of processes in

memory) take more time to decide which process should be selected for execution

2. Process Scheduling Contd…

take more time to decide which process should be selected for execution select a good process mix of I/O-bound(spends more of its time doing I/O)

and CPU-bound (spends more of its time doing computations) processes) Time-sharing systems (UNIX &Windows) have no long-term scheduler but

simply put every new process in memory for the short-term scheduler• Short-term scheduler (or CPU scheduler) – selects which process should

be executed next and allocates CPU select a new process for the CPU frequently i.e. executes at least once every

100 milliseconds Must be fast. i.e. If it takes 10 milliseconds to decide to execute a process for

100 milliseconds, then 10/(100 + 10) = 9 percent of the CPU is being wastedsimply for scheduling the work

10Loganathan R, CSE, HKBKCE

Page 11: 3 process management

Medium Term Scheduler• Intermediate level of scheduling, removes processes from memory (and

from active contention for the CPU) to reduce the degree ofmultiprogramming

• The process can be reintroduced later into memory, and its executioncan be continued where it left off(Swapping)

2. Process Scheduling Contd…

11Loganathan R, CSE, HKBKCE

Page 12: 3 process management

2.3 Context Switch

• When CPU switches to another process, thesystem must save the state(PCB) of the oldprocess and load the saved state for the newprocess is known as context switch

• Context-switch time is overhead; the system

2. Process Scheduling Contd…

• Context-switch time is overhead; the systemdoes no useful work while switching

• Time dependent on hardware support– Sun UltraSPARC provide multiple sets of registers,

which requires changing the pointer to the currentregister set

12Loganathan R, CSE, HKBKCE

Page 13: 3 process management

3.1 Process Creation• Parent process create children processes via create-process

systemcall, which, in turn create other processes, forming a tree ofprocesses

• Processes are identified according to a unique process identifier(orpid), which is typically an integer number

3. Operations on Processes

13Loganathan R, CSE, HKBKCE

A tree of process on aSolaris system

Page 14: 3 process management

• When a Process creates new process,• Resource sharing

• Parent and children share all resources• Children share subset of parent’s resources• Parent and child share no resources

• Execution possibilities

3. Operations on Processes Contd…

• Execution possibilities• Parent and children execute concurrently• Parent waits until children terminate

• Address space possibilities• Child duplicate of parent• Child has a program loaded into it

14Loganathan R, CSE, HKBKCE

Page 15: 3 process management

Process Creation UNIX examples• fork system call creates new process• exec system call used after a fork to replace the process’s memory

space with a new program

3. Operations on Processes Contd…

int main(){pid_t pid;

pid = fork();if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");exit(-1);

15Loganathan R, CSE, HKBKCE

exit(-1);}else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);}else { /* parent process *//* parent wait for the child to complete */

wait (NULL);printf ("Child Complete");exit(0);

}}

C Program Forking Separate Process

Process Creation

Page 16: 3 process management

3.2 Process Termination• Process executes last statement and asks the

operating system to delete it (using exit())– Output data from child to parent (via wait())– Process’s resources are deallocated by operating

system

• Parent may terminate execution of children

3. Operations on Processes Contd…

• Parent may terminate execution of childrenprocesses (abort)– Child has exceeded allocated resources– Task assigned to child is no longer required– If parent is exiting

• Some operating system do not allow child to continue if itsparent terminates

– All children terminated - cascading termination

16Loganathan R, CSE, HKBKCE

Page 17: 3 process management

• Processes executing concurrently in the operating system may beeither independent processes or cooperating processes

• Independent process cannot affect or be affected by theexecution of another process

• Cooperating process can affect or be affected by the execution ofanother process

• Advantages of process cooperation• Information sharing

4. Interprocess Communication

• Information sharing• Computation speed-up – break into subtasks, each of which will be

executing in parallel• Modularity - dividing the system functions into separate processes or

threads• Convenience - individual user may work on many tasks at the same

time

• Cooperating processes require an interprocess communication(IPC) mechanism to exchange data and information

17Loganathan R, CSE, HKBKCE

Page 18: 3 process management

• Two fundamental models of Interprocess communication• Shared memory• a region of memory that is shared by cooperating processes is established then

exchange information takes place by reading and writing data to the shared region

• Message passing• communication takes place by means of messages exchanged between the

cooperating processes

4. Interprocess Communication Contd…

Mes

sage

pas

sin

g

mem

ory

18Loganathan R, CSE, HKBKCE

Mes

sage

pas

sin

g

Shar

edm

emo

ry

Page 19: 3 process management

4.1 Shared memory Systems• A shared-memory region resides in the address space of the process

creating the shared-memory• Other processes that wish to communicate using this shared-memory

segment must attach it to their address space• The form of the data and the location are determined by these

processes and are not under the operating system's control• Example : Producer-Consumer Problem - Paradigm for cooperating

processes, producer process produces information that is consumed by

4. Interprocess Communication Contd…

processes, producer process produces information that is consumed bya consumer process• Example : compiler may produce assembly code, which is consumed by an

assembler, in turn, may produce object modules, which are consumed by theloader

• To allow producer and consumer processes to run concurrently, wemust have available a buffer of items that can be filled by the producerand emptied by the consumer

• unbounded-buffer places no practical limit on the size of the buffer (customermay wait)

• bounded-buffer assumes that there is a fixed buffer size(both waits)

19Loganathan R, CSE, HKBKCE

Page 20: 3 process management

Bounded-Buffer – Shared-Memory Solution

• Shared buffer is implemented as a circular arraywith two logical pointers: in and out

• The buffer is empty when in == out; the buffer isfull when ((in + 1) % BUFFER_SIZE) == out.

#define BUFFER_SIZE 10

4. Interprocess Communication Contd…

#define BUFFER_SIZE 10

typedef struct {

. . .

} item;

item buffer[BUFFER_SIZE];

int in = 0;

int out = 0;

20Loganathan R, CSE, HKBKCE

Page 21: 3 process management

Code for the producer and consumer processes• Producer process

item nextProduced;while (true) {

/* produce an item in nextProduced */while ( ( ( in + 1) % BUFFER-SIZE) == out); /* do nothing */buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;

4. Interprocess Communication Contd…

in = (in + 1) % BUFFER_SIZE;}

• Consumer processitem nextConsumed;while (true) {

while (in == out); //do nothingnextConsumed = buffer[out];out = (out + 1) % BUFFEFLSIZE;/* consume the item in nextConsumed */

}

21Loganathan R, CSE, HKBKCE

Page 22: 3 process management

4.2 Message-Passing Systems• Mechanism for processes to communicate and to

synchronize their actions• Message system – processes communicate with each

other without resorting to shared variables• IPC facility provides two operations:

– send(message) – message size fixed or variable

4. Interprocess Communication Contd…

– send(message) – message size fixed or variable– receive(message)

• If ProcessP and Q wish to communicate, they need to:– establish a communication link(Not Phsical) between them– exchange messages via send/receive

• Implementation of communication link– physical (e.g., shared memory, hardware bus or network)– logical (e.g., logical properties)

22Loganathan R, CSE, HKBKCE

Page 23: 3 process management

• Methods for logically implementing a link and send() / receive () operations• Direct or indirect communication• Synchronous or asynchronous communication• Automatic or explicit buffering

4.2.1 Naming - Processes must name each other explicitly in Direct

Communication• send (P, message) – send a message to process P• receive(Q, message) – receive a message from process Q

• Properties of communication link• Links are established automatically

4. Interprocess Communication Contd…

• Links are established automatically• A link is associated with exactly one pair of communicating processes• Between each pair there exists exactly one link• The link may be unidirectional, but is usually bi-directional

• In Symmetry addressing - both the sender process and the receiver processmust name the other i.e. send(P, message) and receive (Q, message)

• In asymmetry addressing - only the sender names the recipient• send(P, message)—Send a message to process P• receive(id, message)—-Receive a message from any process; id will be the

name of the sender process (disadvantage – Changing id of a processnecessitates all references to the old id) 23

Loganathan R, CSE, HKBKCE

Page 24: 3 process management

• Messages are sent to and received from mailboxes or ports• Each mailbox has a unique id• Processes can communicate only if they share a mailbox• Primitives : send(A, message and receive(A, message) where A is a mailbox

• Properties of communication link• Link established only if processes share a common mailbox• A link may be associated with more than two processes• Each pair of processes may share several communication links• Link may be unidirectional or bi-directional

• Mailbox sharing• P , P , and P share mailbox A and P , sends to A; Who gets the message?

4. Interprocess Communication Contd…

• P1, P2, and P3 share mailbox A and P1, sends to A; Who gets the message?• Allow a link to be associated with at most two processes• Allow only one process at a time to execute a receive operation• Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

The system may define an algorithm(RR) for selection• A mailbox may be owned either by a process or the OS• If the mailbox is owned by a process, it is distinguished as owner (only receive) & user (only send)

• A mailbox owned by OS has an existence of its own and allows a process to– create a mailbox , send and receive messages through mailbox and destroy a mailbox

• The process that creates a new mailbox is the owner and it can only receive messages,ownership and receiving privilege may be passed to other processes through appropriatesystem calls

24Loganathan R, CSE, HKBKCE

Page 25: 3 process management

4.2.2 Synchronization• Different design options for implementing send() and

receive () primitives

• Message passing may be either blocking or non-blockingalso known as synchronous or asynchronous.

• Blocking is considered synchronous– Blocking send - sender block until the message is received

4. Interprocess Communication Contd…

– Blocking send - sender block until the message is received

– Blocking receive - receiver block until a message is available

– rendezvous between the sender and the receiver

• Non-blocking is considered asynchronous– Non-blocking send - sender send the message and continue

– Non-blocking receive - receiver receive a valid message ornull

25Loganathan R, CSE, HKBKCE

Page 26: 3 process management

4.2.3 Buffering

• Messages exchanged by communicating processesreside in a temporary queue

• Queue of messages attached to the link is implementedin one of three ways

– Zero capacity – The queue length is zero, no messages can

4. Interprocess Communication Contd…

– Zero capacity – The queue length is zero, no messages canwait. Sender must wait for receiver (rendezvous)

– Bounded capacity – finite length of n messages. Sender mustwait if link full

– Unbounded capacity – infinite length Sender never waits

26Loganathan R, CSE, HKBKCE