module 4: processes

32
Silberschatz and Galvin 1999 4.1 Operating System Concepts Module 4: Processes Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Upload: chase

Post on 06-Jan-2016

35 views

Category:

Documents


1 download

DESCRIPTION

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication. Process Concept. An operating system executes a variety of programs: Batch system – jobs Time-shared systems – user programs or tasks - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Module 4:  Processes

Silberschatz and Galvin 1999 4.1Operating System Concepts

Module 4: Processes

• Process Concept

• Process Scheduling

• Operation on Processes

• Cooperating Processes

• Interprocess Communication

Page 2: Module 4:  Processes

Silberschatz and Galvin 1999 4.2Operating System Concepts

Process Concept

• An operating system executes a variety of programs:

– Batch system – jobs

– Time-shared systems – user programs or tasks

• Process – a program (text section) in execution; process execution must progress in sequential fashion.

• A program is a passive entity while process is an active entity with a program counter specifying the next instruction to execute and a set of associated resources

• A process includes:

– Program counter ( represents current activity)

– Stack( contains temporary data like method parameters, return addresses, local variables)

– Data section (contains global variables)

Page 3: Module 4:  Processes

Silberschatz and Galvin 1999 4.3Operating System Concepts

Process State

• As a process executes, it changes state

– new: The process is being created.

– running: Instructions are being executed.

– waiting: The process is waiting for some event to occur(like I/O completion or reception of a signal).

– ready: The process is waiting to be assigned to a processor.

– terminated: The process has finished execution.

Page 4: Module 4:  Processes

Silberschatz and Galvin 1999 4.4Operating System Concepts

Diagram of Process State

•Only one process can be running on any processor at any time•Many processes may be ready and waiting

Page 5: Module 4:  Processes

Silberschatz and Galvin 1999 4.5Operating System Concepts

Process Control Block (PCB)

Each process is represented in the OS by a Process Control Block that contains the following information associated with each process:

• Process state

• Program counter

• CPU registers

• CPU scheduling information

• Memory-management information

• Accounting information

• I/O status information

Page 6: Module 4:  Processes

Silberschatz and Galvin 1999 4.6Operating System Concepts

Process Control Block (PCB)

Page 7: Module 4:  Processes

Silberschatz and Galvin 1999 4.7Operating System Concepts

CPU Switch From Process to Process

Page 8: Module 4:  Processes

Silberschatz and Galvin 1999 4.8Operating System Concepts

Process Scheduling

• The objective of multiprogramming is to have some process running all the time to maximize CPU utilization

• Time sharing switches CPU among processes so frequently that users can interact with each program while it is running

• A uniprocessor can have only one running process

• If more processes exist the rest must wait until the CPU is free and can be rescheduled

Page 9: Module 4:  Processes

Silberschatz and Galvin 1999 4.9Operating System Concepts

Process Scheduling Queues

• Job queue – Set of all processes in the system. When processes enter into the system they are put in job queue.

• Ready queue – Set of all processes residing in main memory,ready and waiting to execute. A ready queue header contains pointers to the first and final PCBs in the list.

• Device queues – Set of processes waiting for an I/O device. Each device has its own device queue.

• Process migration between the various queues.

• A new process is initially put in the ready queue where it waits for execution

• When the process issues an I/O request it is then placed in I/O queue

• A process could be removed forcibly and placed back in the ready queue

Page 10: Module 4:  Processes

Silberschatz and Galvin 1999 4.10Operating System Concepts

Ready Queue And Various I/O Device Queues

Page 11: Module 4:  Processes

Silberschatz and Galvin 1999 4.11Operating System Concepts

Representation of Process Scheduling

Page 12: Module 4:  Processes

Silberschatz and Galvin 1999 4.12Operating System Concepts

Schedulers

• A scheduler selects processes for scheduling purpose from various scheduling queues

• Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.

• Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.

Page 13: Module 4:  Processes

Silberschatz and Galvin 1999 4.13Operating System Concepts

Addition of Medium Term Scheduling

Page 14: Module 4:  Processes

Silberschatz and Galvin 1999 4.14Operating System Concepts

Schedulers (Cont.)

• Short-term scheduler is invoked very frequently (milliseconds) (must be fast).

• Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow).

• The long-term scheduler controls the degree of multiprogramming.

• If the degree of multiprogramming is stable then the average rate of process creation must be equal to the average departure rate of processes leaving the system.

• Processes can be described as either:

– I/O-bound process – spends more time doing I/O than computations.

– CPU-bound process – spends more time doing computations.

Page 15: Module 4:  Processes

Silberschatz and Galvin 1999 4.15Operating System Concepts

Context Switch

• When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.

• The context of a process is represented in the PCB of a process and it includes the value of the CPU registers, process state and memory management information.

• 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 overhead; the system does no useful work while switching.

• Time dependent on hardware support which means the speed of context switching varies according to hardware.

Page 16: Module 4:  Processes

Silberschatz and Galvin 1999 4.16Operating System Concepts

Process Creation

• Parent process creates children processes, which, in turn create other processes, forming a tree of processes.

• Resource sharing

– Parent and children share all resources.

– Children share subset of parent’s resources.

– Parent and child share no resources.

• Execution

– Parent and children execute concurrently.

– Parent waits until children terminate.

Page 17: Module 4:  Processes

Silberschatz and Galvin 1999 4.17Operating System Concepts

Process Creation (Cont.)

• Address space

– Child duplicate of parent.

– Child has a program loaded into it.

• UNIX examples

– fork system call creates new process

– execlp system call used after a fork to replace the process’ memory space with a new program.

E.g.: int pid = fork(); // create a child process

execlp(“/bin/ls”, “ls” , NULL); //replace the memory space with a new program

Page 18: Module 4:  Processes

Silberschatz and Galvin 1999 4.18Operating System Concepts

A Tree of Processes On A Typical UNIX System

Page 19: Module 4:  Processes

Silberschatz and Galvin 1999 4.19Operating System Concepts

Process Termination

• Process executes last statement and asks the operating system to decide it (exit).

– Output data from child to parent (via wait).

– Process’ resources are deallocated by operating system.

• Parent may terminate execution of children processes (abort).

– Child has exceeded allocated resources.

– Task assigned to child is no longer required.

– Parent is exiting. Operating system does not allow child to continue if its

parent terminates. Cascading termination.

Page 20: Module 4:  Processes

Silberschatz and Galvin 1999 4.20Operating System Concepts

Cooperating Processes

• Independent process cannot affect or be affected by the execution of another process. That is the process doesn’t share any data with any other processes.

• Cooperating process can affect or be affected by the execution of another process. It shares data with other processes.

• Advantages of process cooperation

– Information sharing (allowing concurrent access to resources)

– Computation speed-up (break task into subtasks and execute )

– Modularity (Dividing system function into separate processes)

– Convenience (allowing users to use the system in convenience)

Page 21: Module 4:  Processes

Silberschatz and Galvin 1999 4.21Operating System Concepts

Producer-Consumer Problem

• Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.

• To allow the producer and consumer process to run concurrently a buffer is needed to be maintained that can be filled by the producer process and consumed by the consumer process.

• The producer & consumer must be synchronized so that the consumer doesn’t consume an item that has not been produced yet.

– unbounded-buffer places no practical limit on the size of the buffer. Consumer has to wait for new items while producer can always produce new items

– bounded-buffer assumes that there is a fixed buffer size. Consumer can always consume items if the buffer is non empty and the producer has to wait to produce a new item if the buffer is full.

Page 22: Module 4:  Processes

Silberschatz and Galvin 1999 4.22Operating System Concepts

Bounded-Buffer – Shared-Memory Solution

• Shared data

#define BUFFER_SIZE 10

typedef struct{

...

}item;

item buffer[BUFFER_SIZE];

int in = 0;

int out = 0;

• in points to the next free position in the buffer

• out points to the first full position in the buffer

• The buffer is empty when in==out

• The buffer is full when ((in+1)%BUFFER_SIZE)==out

Page 23: Module 4:  Processes

Silberschatz and Galvin 1999 4.23Operating System Concepts

Bounded-Buffer (Cont.)

• Producer process: It has the local variable nextProduced in which the new item to be produced is stored.

while(1)

{

/* produce an item in nextProduced */

while(((in+1)%BUFFER_SIZE) == out)

; /* do nothing */

buffer[in] = nextProduced;

in = (in+1)%BUFFER_SIZE;

)

Page 24: Module 4:  Processes

Silberschatz and Galvin 1999 4.24Operating System Concepts

Bounded-Buffer (Cont.)

• Consumer process: It has the local variable nextConsumed in which the to be consumed is stored.

while(1)

{

while(in == out)

;// do nothing

nextConsumed = buffer[out];

out = (out+1)%BUFFER_SIZE;

/* consume the item in nextConsumed */

}

• This scheme allows at most BUFFER_SIZE-1 items in the buffer at the same time.

Page 25: Module 4:  Processes

Silberschatz and Galvin 1999 4.25Operating System Concepts

Interprocess Communication (IPC)

• Mechanism for processes to communicate and to synchronize their actions without sharing same address space.

• Message system – processes communicate with each other without resorting to shared variables through message passing.

• IPC facility provides two operations:

– send(message) – message size fixed or variable

– receive(message)

• If P and Q wish to communicate, they need to:

– establish a communication link between them

– exchange messages via send/receive

• Implementation of communication link

– physical (e.g., shared memory, hardware bus)

– logical (e.g., logical properties)

Page 26: Module 4:  Processes

Silberschatz and Galvin 1999 4.26Operating System Concepts

Implementation Schemes

• Direct or indirect communication

• Symmetric or asymmetric communication

• Automatic or explicit buffering

• Send by copy or send by reference

• Fixed-sized or variable-sized messages

Page 27: Module 4:  Processes

Silberschatz and Galvin 1999 4.27Operating System Concepts

Direct Communication(Symmetric)

• Processes must name sender & receiver explicitly:

– 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.

– 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.

Page 28: Module 4:  Processes

Silberschatz and Galvin 1999 4.28Operating System Concepts

Direct Communication(Asymmetric)

• The sender names the recipient; the recipient is not required to name the sender.

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

– receive(id, message) – receive a message from any process; the variable id is set to the name of the process with which communication has taken place.

• Disadvantage of the symmetric and asymmetric schemes:

– Limited modularity of the resulting process definitions.

– Changing the name of a process may necessitate examining all other process definitions.

Page 29: Module 4:  Processes

Silberschatz and Galvin 1999 4.29Operating System Concepts

Indirect Communication

• Messages are directed and received from mailboxes (also referred to as ports).

– Each mailbox has a unique id.

– Processes can communicate only if they share a mailbox.

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

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

• Properties of communication link

– Link established only if processes share a common mailbox

– A link may be associated with many processes.

– Each pair of processes may share several communication links.

– Link may be unidirectional or bi-directional.

Page 30: Module 4:  Processes

Silberschatz and Galvin 1999 4.30Operating System Concepts

Indirect Communication (Continued)

• Mailbox sharing

– P1, P2, and P3 share mailbox A.

– P1, sends; P2 and P3 receive.

– Who gets the message?

• Solutions

– 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. (That is either P2 or P3 will receive the message but not both) Sender is notified who the receiver was.

Page 31: Module 4:  Processes

Silberschatz and Galvin 1999 4.31Operating System Concepts

Synchronization

• Communication between processes take place by calls to send and receive primitives.

• Different types of message passing :

– Blocking send: the sender process is blocked until it is received by the receiving process or mailbox

– Non Blocking send: the sender process sends the message and resumes operation

– Blocking receive: The receiver blocks until a message is available

– Non Blocking receive: The receiver retrieves either a valid message or a null

Page 32: Module 4:  Processes

Silberschatz and Galvin 1999 4.32Operating System Concepts

Buffering

• Queue of messages attached to the link; implemented in one of three ways.

1. Zero capacity – 0 messagesSender must wait for receiver (rendezvous).

2. Bounded capacity – finite length of n messages, sender must wait if link full.

3. Unbounded capacity – infinite length ,sender never waits.