chapter 4: processes process concept process scheduling operations on processes cooperating...
TRANSCRIPT
Chapter 4: Processes
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems
Process Concept
An operating system executes a variety of programs: Operating System’s code User programs or tasks
Textbook uses the terms job and process almost interchangeably.
Process – a program in execution; process execution must progress in sequential fashion.
A process includes: program counter CPU registers Stack, containing temporary variables data section, containing global variables
A C++ source file on disk is not a process Several processes can be associated with the same program
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. ready: The process is waiting to be assigned to a processor. terminated: The process has finished execution.
Diagram of Process State
Process Control Block (PCB)
Information associated with each process.
Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information
Process Control Block (PCB)
Address of the next
instruction to be executed
General memory management
information such as base and limit
registers
new, ready, running, waiting,
terminated
CPU Switch From Process to Process
Process Scheduling
Some systems allow execution of only a single process at a time. Such are called uni-programming systems.
Others allow more than one process, concurrent execution of many processes. These are called multi-programming (or multi-processing, but not multi-processor) system.
In a multiprogramming system, the CPU switches automatically among processes running each for tens or hundreds of milliseconds. Hence, users can interact with each program.
Process 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 queue. Processes that reside in main memory that are ready to
execute are put on the ready queue. If a process issues an I/O request, it is placed in the
corresponding I/O queue. When a process terminates, it is removed from all queues
and its PCB is deleted.
Ready Queue And Various I/O Device Queues
Representation of Process Scheduling
Process Schedulers
The selection of processes from the queues is managed by schedulers.
Long-term scheduler (or job scheduler) – selects processes from a mass-storage (i.e., hard disk) device where they are spooled and loads them into memory for execution.
Short-term scheduler (or CPU scheduler) – selects which ready process that is already loaded into memory should be executed next and allocates CPU.
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. This corresponds to the number of processes in memory.
I/O-bound process – spends more time doing I/O than computations, many short CPU bursts.
CPU-bound process – spends more time doing computations; few very long CPU bursts.
Schedulers (Cont.)
Long-term scheduler should pick a relatively good mix of I/O- and CPU-bound processes so that the system resources are better utilized.
If all processes are I/O-bound, the ready queue will almost always be empty.
If all processes are CPU-bound, I/O queue will be almost always empty.
A balanced combination should be selected for system efficiency.
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.
Switching involves copying registers, local and global data, file buffers and other information to the PCB.
Context-switch time is overhead; the system does no useful work while switching.
Time is dependent on hardware support.
Process Creation
Parent process create 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.
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 – terminating the children processes by a terminated parent process.
Cooperating Processes
Independent process cannot affect or be affected by the execution of another process.
Cooperating process can affect or be affected by the execution of another process
Advantages of process cooperation Information sharing – e.g. a shared database Computation speed-up Modularity – system can be constructed in modular form Convenience – different system tools may be used in
parallel printing, editing, compiling etc.
Producer-Consumer Problem
Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.
Buffer is defined to be a shared memory space Producer places the produced item into a buffer and
consumer takes its consumption from the buffer. When buffer is full, producer must wait until consumer
consumes at least an item; likewise, when buffer is empty, consumer must wait until producer places at least an item into the buffer.
unbounded-buffer places no practical limit on the size of the buffer.
bounded-buffer assumes that there is a fixed buffer size.
Bounded-Buffer – Shared-Memory Solution
Producerprocess
memory
Consumerprocess
buffer
Producer-Consumer Problem
Shared variables:
const int N=?; // buffer size
int buffer[N]; // kind of items kept in the buffer
int in=0, out=0; // in & out indexes to buffer.
0
1
2
3
N-1
xy
z
out
in
Buffer is empty when:in = out
Buffer is full when:(in + 1) %N == out
Producer – Consumer Processes
int nextp; // local variable of item produced do{
….produce an item in nextp;….while ((in + 1)% N == out); //buffer fullbuffer [in] = nextp;in = (in + 1) % N;
} while (true);
int nextc; // local variable of item consumed do{
while (in == out); //buffer emptynextc = buffer [out];out = (out + 1) % N; ….Consume the item in nextc;….
} while (true);
Producer code: Consumer code:
REMARK: At most (N-1) items can be available in the buffer
Producer – Consumer Processes(alternative solution)
int nextp; int nextc; do do{ { … while (!full[out]) /* do nothing*/; produce an item in nextp nextc = buffer[out]; … full[out] = 0; while (full[in]) /* do nothing*/; out = (out + 1) % N; buffer[in] = nextp; … full[in] = 1; consume the item in nextc in = (in + 1) % N; …} while(true); } while(true);
Producer code: Consumer code:
REMARK: At most N items can be available in the buffer
Shared variables:
const int N = ?;
int full[N] = {0}; // the buffer is initially empty
int buffer[N], in=0, out=0;
Interprocess Communication (IPC)
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 receive(message)
If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive
Implementation Questions
How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of
communicating processes? What is the capacity of a link? Is the size of a message that the link can accommodate
fixed or variable? Is a link unidirectional or bi-directional?
Direct Communication
Processes must name each other 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.
Direct CommunicationProducer-Consumer problem
int nextp; int nextc; do do{ { … receive(producer,nextc); produce an item in nextp … … consume the item in nextc send (consumer,nextp); …} while(true); } while(true);
Producer code: Consumer code:
The main disadvantage is limited modularity in direct communication. The names of producer and receiver should be correctly set in every time they are used
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.
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.
Indirect Communication
Operations create a new mailbox send and receive messages through mailbox destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Indirect Communication
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. Sender is
notified who the receiver was.
Buffering
Queue of messages attached to the link; implemented in one of three ways.
1. Zero capacity – 0 messagesSender must wait for receiver.
2. Bounded capacity – finite length of n messagesSender must wait if link full.
3. Unbounded capacity – infinite length Sender never waits.
Acknowledgement
After receiving a message, in order to inform sender of the receipt of the message, receiver sends an acknowledgement message.
Process P sends a message to Process Q
send (Q, message) receive (P, message)receive (Q, message) send (Q, “Acknowledgement”)
Exception Conditions
Message system is useful in distributed environments since the probability of error during communication is larger than that of a single machine environment.
In a single machine environment, shared memory system is generally used.
If a communication error occurs, error recovery or exception condition handling must take place.
Possible errors are: A process terminates Lost messages Scrambled messages
Exception Conditions – Terminated Process
In these cases, the operating system should either terminate P or notify P that Q has terminated.
P Q
waiting
Receiver process (P) waits a message from Q that has terminated. Then P will be blocked forever.
P Q
sender
P sends a message to a terminated process. If P requires an acknowledgement from Q for further processing, P will be blocked forever.
A sender or receiver process may terminate before a message is processed. This situation will leave message that will never be received or processes waiting for messages that will never be sent.
Exception Conditions – Lost Messages
P send a message to Q, which is lost somewhere in the link due to link failure.
In such cases, The OS is responsible for detecting
and responding by notifying the sending process that the message is lost, or
The sending process is responsible for detecting and should re-transmit the message.
P Q
The message is lost due to hardware or communication line failure.
The most common method for detecting lost messages is to use timeouts. For instance, if the acknowledgement signal does not arrive at the sending process in a specified time interval, it is assumed that the signal is lost and resent.
Exception Conditions – Scrambled messages
Because of the noise in communication channels, the delivered message may be scrambled (modified) in on the way. Error checking codes are commonly used to detect this type of error.