Chapter 2-Process Management
Chapter 2-Process Management
Chapter Two
Process
Management
Lecture 2: Processes
1
Overview of the lecture
Process
Process Concept
Process Scheduling
Cooperating Processes
Interprocess Communication
2
An Analogy
Example
Various workers (males, females, skilled, unskilled, semi-skilled) are working on some job.
Question: How to organize them effectively to improve the performance ?
Sitting worker is similar to program.
• Does not use any tools and resources.
Working worker is similar to process
• Uses tools, interacts with other,
• If he/she is working with others, cooperation and synchronization is required. Otherwise accidents might occur.
• If he/she is working alone, no cooperation is needed.
To manage working workers, the manager should have some information about the working
worker.
• What kind of tools he/she is using.
• The nature of the job, and status of the job.
• ….
In computer system
• Operating System/OS is similar to manager
• The workers are similar to the programs reside on the disk.
• The working workers are processes
• CPU is an expensive tool which should not be kept idle. It is OK if some workers wait for expensive tool.
3
Process
• A program written in any language like C, C++ or Java is a passive entity, while a
Process is a program in execution.
• A process is an active entity. A program can be converted into a process if it is loaded
from the disk into main memory and begin its execution on processor.
• Process need resources to complete its tasks, the main resources required in
the lifetime of a process are memory, CPU, I/O devices etc.
• A process is composed of unit of activity characterized by the execution
of a sequence of instructions, a current state, and an associated set of system
instructions
4
Process Concept
• Early systems: One program at a time was executed and a single program has a
complete control.
• Modern OSs: allow multiple programs to be loaded in to memory and to be executed
concurrently.
• This requires firm control over execution of programs.
• The notion of process emerged to control the execution of programs.
• A process: Unit of work Program in execution
• OS consists of a collection of processes
• OS processes executes system code.
• User processes executes user code.
• By switching CPU between processes, the OS can make the computer more productive.
5
Process Concept
Process – A process has Multiple parts other than source code Process in Memory
• Text section: The source code text file, contain the logic of
program which is executed on behalf of process
• Program counter: Program counter is the Processor register(s)
which stores the address of next executable instruction.
Multiple PC are possible one for each flow of control during the
run time in multiprogramming environment.
• Stack is an area in RAM containing temporary data, Function
parameters, return addresses, local variables belonging to a
process
• Data section containing global variables declared in the process
• Heap containing memory dynamically allocated during run
time of the process
6
Process state: lifecycle of a process
• The lifetime of a process can be divided into states. As the process executes, it changes
its states.
• At any particular instance of time a process can be in one of the states: newly
created, waiting for processor, running, suspended and etc.
• OS manages the state transition and scheduling of the processes and facilitates
context switching
• There are different models for process state transition for example two state model
describe a process life in two states only, while a five state model describe the life of a
process in five different states.
• Some Unix based operating systems follow seven state model of process
7
Two-State Process Model
• Process may be in one of two
states
• Running
• Not-running
Not-Running
Process in a Queue
8
Process States: Five-state Model
As a process executes, it changes state
Each process can be in one of state
• New: just created, not yet admitted to
set of runnable processes
• Ready: ready to run
• Running: currently being run
• Blocked/Waiting: waiting for an event
(I/O)
• Exit/Terminate: completed/error exit
Paren Chil
t d
main( main(
) )
{ {
fork(); fork()
;
} pid= } pid=
…; …
UNIX: fork() system call
Chil
Paren d
void main(void) {
t pid=fork();
PID=345 void main(void) {
pid=fork();
if (pid==0)
6 if (pid==0)
PID=0
ChildProcess();
else ChildProcess();
ParentProcess(); else
} ParentProcess();
} }
Void ChildProcess(void) }
{ Void ChildProcess(void){
} }
Void ParentProcess(void) Void ParentProcess(void)
{ {
} }
Process Termination
• Processes terminate in one of ways:
• Normal Termination occurs by a return from main or when requested by an explicit call to exit or
_exit.
• Abnormal Termination occurs as the default action of a signal or when requested by abort.
• 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 the 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.
Cooperating Processes
Processes executing concurrently in the OS 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.
• Any process that does not share data with any other process is independent.
• 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.
• Advantages of process cooperation
• Information sharing
• Computation speed-up
• Break into several subtasks and run in parallel
• Modularity
• Constructing the system in modular fashion.
• Convenience
• User will have many tasks to work in parallel editing, compiling, printing
Inter-process Communication (IPC)
• When processes communicate with each other it is called "Inter-process communication" (IPC).
Processes frequently need to communicate, for instance in a shell pipeline, the output of the first
process need to pass to the second one, and so on to the other process.
• IPC facility provides a mechanism to allow processes to communicate and synchronize their
actions.
• There are two fundamental models for implementation of IPC:
1. Shared memory
2. Message passing
• The Shared-memory method requires communication processes to share some variables.
• The responsibility for providing communication rests with the programmer.
• Example: producer-consumer problem.
IPC(Cont…..)
(1) Shared memory:- a region of memory that is shared by cooperating processes is established.
Processes can then exchange information by reading and writing data to the shared region.
It allows maximum speed and convenience of communication, as it can be done at memory speeds
when within a computer.
It is faster than message passing, as message-passing systems are typically implemented using
system
calls and thus require the more time consuming task of kernel intervention.
In contrast, in shared-memory systems, system calls are required only to establish shared-memory
regions. Once shared memory is established, all accesses are treated as routine memory accesses, and
no assistance from the kernel is required.
(2) Message passing:- communication takes place by means of messages exchanged between the
cooperating processes.
it is useful for exchanging smaller amounts of data, because no conflicts need be avoided.
It is also easier to implement than is shared memory for intercomputer communication.
Both of the models just discussed are common in operating systems, and many systems implement
both.
Communication Models
Producer-Consumer Problem
producer-consumer problem (bounded-buffer problem) is a classical example of a multi- process
synchronization problem.
The problem describes two processes, the producer and the consumer, who share a common, fixed-size
buffer. The producer's job is to generate a piece of data, put it into the buffer and start again. At the
same time the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time.
The problem is to make sure that the producer won't try to add data into the buffer if it's full and that
the
consumer won't try to remove data from an empty buffer.
The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time
the consumer removes an item from the buffer, it notifies the producer who starts to fill the buffer
again.
In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the
producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached
by means of inter-process communication, typically using Semaphores. An inadequate solution
could result in a deadlock where both processes are waiting to be awakened. The problem can also
be generalized to have multiple producers and consumers.
Producer-Consumer Problem: Shared memory
}
Bounded-Buffer –
Consumer Process
item nextConsumed;
while (1) {
while (in ==
out)
; /* do nothing */
IPC: Message Passing
• Message system – processes communicate with each other without
resorting to shared variables.
• If P and Q want to communicate, a communication link exists between them.
• OS provides this facility.
• 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)
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?
• (How much is the buffer space ?)
• Is the size of a message that the link can accommodate fixed or
variable?
• Is a link unidirectional or bi-directional?
Message passing systems
1. Direct or Indirect communication
2. Synchronous or asynchronous communication
3. Automatic or explicit buffering
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.
• This exhibits both symmetry and asymmetry in addressing
• Symmetry: Both the sender and the receiver processes must name the other to
communicate.
• Asymmetry: Only sender names the recipient, the recipient is not required to name
the sender.
• The send and receive primitives are as follows.
• Send (P, message)– send a message to process P.
• Receive(id, message)– receive a message from any process.
• Disadvantages: Changing a name of the process creates problems.
Indirect Communication
• The messages are sent and received from mailboxes (also referred to
as ports).
• A mailbox is an object
• Process can place messages
• Process can remove messages.
• Two processes can communicate only if they have a shared mailbox.
• 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(cont…)
• Mailbox sharing
• P1, P2, and P3 share mailbox A.
• P1, sends; P2 and P3 receive.
• Who gets a 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.
• Properties of a link:
• A link is established if they have a shared mailbox
• A link may be associated with more than two boxes.
• Between a pair of processes they may be number of links
• A link may be either unidirectional or bi-directional.
• OS provides a facility
• To create a mailbox
• Send and receive messages through mailbox
• To destroy a mail box.
• The process that creates mailbox is a owner of that mailbox
• The ownership and send and receive privileges can be passed to other processes through
system calls.
Indirect Communication(cont…)
• 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.
• Example:
• Producer process:
repeat
….
Produce an item in nextp
…
send(consumer,next
p); until false;
• Consumer process
• repeat ….
receive(producer,
nextc);…. Consume
the item in nextc …
until false;
Synchronous or asynchronous
Message passing may be either blocking or non-blocking.
Blocking is considered synchronous
Non-blocking is considered asynchronous
send and receive primitives may be either blocking or non-blocking.
• Blocking send: The sending process is blocked until the message is received by the
receiving process
or by the mailbox.
• Non-blocking send: The sending process sends the message and resumes operation.
• Blocking receive: The receiver blocks until a message is available.
• Non-blocking receive: The receiver receives either a valid message or a null.
Automatic and explicit buffering
• A link has some capacity that determines the number of messages that can reside in it
temporarily.
• Queue of messages is attached to the link; implemented in one of three ways.
1. Zero capacity – 0 messages
Sender 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.
In non-zero capacity cases a
process does not know whether
a message has arrived after the
send operation.
The sender must communicate
explicitly with receiver to find
out whether the later received
the message.
Example: Suppose P sends a
message to Q and executes
only after the message has
arrived.
Process P:
send (Q. message) : send
message to process Q
Exception conditions
• When a failure occurs error recovery (exception handling) must take place.
• Process termination
• A sender or receiver process may terminate before a message is processed. (may be blocked
forever)
• A system will terminate the other process or notify it.
• Lost messages
• Messages may be lost over a network
• Timeouts; restarts.
• Scrambled messages
• Message may be scrambled on the way due to noise
• The OS will retransmit the message
• Error-checking codes (parity check) are used.
Reading Assignment:
Client-Server Communication
Sockets
Remote Procedure Calls
Remote Method Invocation
(Java)