4-Process and IPC
4-Process and IPC
Process States:
As a process executes, it changes state.
The state of a process is defined in part by the current activity of that
process.
Each process may be in one of the following states:
New: The process is being created.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur (such as an
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.
Process Control Block
Each process is represented in the operating system by a process control
block (PCB)-also called a task control block.
A PCB defines a process to the operating system.
It contains the entire information about a process.
Some of the information a PCB contans are:
Process state: The state may be new, ready, running, waiting, halted,
and SO on.
Program counter: The counter indicates the address of the next
instruction to be executed for this process.
CPU registers: The registers vary in number and type, depending on
the computer architecture.
CPU-scheduling information: This information includes a process
priority, pointers to scheduling queues, and any other scheduling
parameters.
Memory-management information: This information may include
such information as the value of the base and limit registers, the page
tables, or the segment tables, depending on the memory system used
by the operating system.
Accounting information: This information includes the amount of
CPU and real time used, time limits, account numbers, job or process
numbers, and so on.
Status information: The information includes the list of I/O devices
allocated to this process, a list of open files, and so on.
Process Scheduling
The objective of multiprogramming is to have some process running at all
times, so as to maximize CPU utilization.
Scheduling Queues
There are 3 types of scheduling queues .They are :
1. Job Queue
2. Ready Queue
3. Device Queue
As processes enter the system, they are put into a job queue.
The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue.
The list of processes waiting for an I/O device is kept in a device queue for
that particular device.
A new process is initially put in the ready queue. It waits in the ready queue until
it is selected for execution (or dispatched).
Once the process is assigned tothe CPU and is executing, one of several events
could occur:
The process could issue an I/O request, and then be placed in an I/O queue.
The process could create a new subprocess and wait for its termination.
The process could be removed forcibly from the CPU, as a result of an interrupt,
and be put back in the ready Queue.
A common representation of process scheduling is a queueing diagram
Schedulers
A process migrates between the various scheduling queues throughout its
lifetime.
The operating system must select, for scheduling purposes, processes from
these queues in some fashion.
The selection process is carried out by the appropriate scheduler. There are
three different types of schedulers.They are:
Context Switch
Switching the CPU to another process requires saving the state of the old
process and loading the saved state for the new process.
This task is known as a context switch.
Context-switch time is pure overhead, because the system does no useful work
while switching.
Its speed varies from machine to machine, depending on the memory speed,
the number of registers that must be copied, and the existence of special
instructions.
Operations on Processes
1. Process Creation
A process may create several new processes, during the course of execution.
The creating process is called a parent process, whereas the new processes
are called the children of that process.
When a process creates a new process, two possibilities exist in terms of
execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have
terminated.
There are also two possibilities in terms of the address space of the new
process:
1. The child process is a duplicate of the parent process.
2. The child process has a program loaded into it.
In UNIX, each process is identified by its process identifier, which is a
unique integer. A new process is created by the fork system call.
2. Process Termination
#define
BUFFER_SIZE 10
typedef struct {
...
} item;
item
buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
The shared buffer is implemented as a circular array with two logical
pointers: in and out. The variable 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) % BUFFERSIZE) == out.
Producer Process
While (1)
{
While (((in + 1) % BUFFER_SIZE) == out);
/* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer process
while (1)
{
while (in == out);
/* do nothing */
nextConsumed = buffer [out];
out = (out + 1) % BUFFER_SIZE
}
Interprocess Communication
2. Indirect Communication
A link has some capacity that determines the number of message that
can reside in it temporarily. This property can be viewed as a queue of
messages attached to the link.
There are three ways that such a queue can be implemented.
Zero capacity: Queue length of maximum is 0. No message is waiting
in a queue. The sender must wait until the recipient receives the
message. (message system with no buffering)
Bounded capacity: The queue has finite length n. Thus at most n
messages can reside in it.
Unbounded capacity: The queue has potentially infinite length. Thus
any number of messages can wait in it. The sender is never delayed
4. Synchronization
Message passing may be either blocking or non-blocking.
1. Blocking Send - The sender blocks itself till the message sent
by it is received by the receiver.
2. Non-blocking Send - The sender does not block itself after
sending the message but continues with its normal operation.
3. Blocking Receive - The receiver blocks itself until it receives
the message.
4. Non-blocking Receive – The receiver does not block itself.