You are on page 1of 33

Chapter 4: Processes

 Process Concept
 Process Scheduling
 Operations on Processes
 Cooperating Processes
 Interprocess Communication

OS Notes by Dr. Naveen Choudhary


Process Concept

 An operating system executes a variety of programs:


 Batch system – jobs
 Time-shared systems – 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.( program is passive entity,
whereas process is an active entity  life form, life )
 A process includes:
 Program code
 program counter
 Contents of processor registers
 Stack (process stack containing temporary data such as subroutine
parameters, return addresses and temporary variables )
 data section (containing global variables)

OS Notes by Dr. Naveen Choudhary


Use of A Base and Limit Register

OS Notes by Dr. Naveen Choudhary


Hardware Address Protection
300040 300040 +120900 = 420940

OS Notes by Dr. Naveen Choudhary


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.

OS Notes by Dr. Naveen Choudhary


Diagram of Process State

Interrupt  when time slice/quantum is over


Scheduler dispatch that is CPU is assigned
Note: only one process can be running on any processor at any
instant. Many process may be ready or waiting however

OS Notes by Dr. Naveen Choudhary


Process Control Block (PCB)
 Each process is represented in the O.S by a PCB (process/task
control block)
Information associated with each process.
 Process state (new, ready, running, waiting, halted)
 Program counter
 CPU registers (accumulator, index registers, stack pointer,
general purpose register )
 CPU scheduling information (process priority, pointer to next
PCB in queue )
 Memory-management information  (base register, limit
register, page table/segmet table pointer)
 Accounting information (amount of CPU or real time used,
time limits, jobs or process no. )
 I/O status information (list of i/o devices allocated to this
process, a list of open files)

OS Notes by Dr. Naveen Choudhary


Process Control Block (PCB)

Pointer : pointer to the next PCB in the list (ready queue)

OS Notes by Dr. Naveen Choudhary


CPU Switch From Process to Process

OS Notes by Dr. Naveen Choudhary


Process Scheduling Queues
The objective is to try to keep the CPU as busy as possible for maximum CPU
utilization & throughput – on a single CPU m/c only one process at a time can
be allocated CPU

 Job queue – set of all processes in the system ( even


processes which are ready to run but are still in
secondary memory)
 Ready queue – set of all processes residing in main
memory, ready and waiting to execute ie waiting for CPU
to be allocated
 Device queues – set of processes waiting for an I/O
device to become free ( as it may be busy with some
other processes
 In the life time of the process, the process may go thru
various queues

OS Notes by Dr. Naveen Choudhary


Ready Queue And Various I/O Device Queues

OS Notes by Dr. Naveen Choudhary


Representation of Process Scheduling

Note : The process could be removed forcibly from the CPU, as a result of an
interrupt and put back in the ready queue.
Resources that
QUEUES
serve the queue
OS Notes by Dr. Naveen Choudhary
Schedulers

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

Job queue
Ready CPU
queue

OS Notes by Dr. Naveen Choudhary


Schedulers (Cont.)
 Short-term scheduler is invoked very frequently (milliseconds) so 
(must be fast). { as the process will run for a short time (interactive )
then i/o will occur or time slice will expire }

 Long-term scheduler is invoked very infrequently (seconds, minutes) 


(may be slow). { as new process are not created so frequently }

 The long-term scheduler controls the degree of multiprogramming. 


The degree of multiprogramming is stable if the rate of creation of
processes is equal to the rate of termination of the process so as to
keep the multiprogramming stable. The long term scheduler should be
invoked whenever some process terminates or leaves the system

 L.T.S should select a good mix of i/o bound and CPU bound process so
as to make efficient and optimal use of all the i/o devices and CPU

 Processes can be described as either:


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

OS Notes by Dr. Naveen Choudhary


Addition of Medium Term Scheduling
(In time sharing system generally long term scheduler is absent and in place a
medium term scheduler is used)

The key idea behind medium term scheduler is that some times it can be
advantageous to remove process from memory (and from active contention of
CPU) and thus to decrease the degree of multiprogramming. At some later time
the process can be reintroduced into memory and its execution can be continued
where it left off. This scheme is called swapping. Swapping (medium term
scheduler) may be necessary to improve the process mix or because a change in
memory requirement has overcommitted available memory, requiring memory to
be freed.

OS Notes by Dr. Naveen Choudhary


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.
 Context-switch time is a pure overhead and the system
does no useful work while switching.
 Time dependent on hardware support.

OS Notes by Dr. Naveen Choudhary


CPU Switch From Process to Process

OS Notes by Dr. Naveen Choudhary


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.
 {1. some O.S pass resources to child process explicitly
2. restricting a child process to a subset of the parent
resources prevents any processes from overloading the
system by creating too many sub processes
 Parent and child share no resources.
 Execution ( 2 possibilities exist )
 Parent and children execute concurrently.
 Parent waits until some or all of it’s childrens terminate.

OS Notes by Dr. Naveen Choudhary


Process Creation (Cont.)

 There are also two possibilities in terms of address space


of the new processes
 Child process is duplicate of the parent processes.
 Child processes has a program loaded into it.
 UNIX examples
 fork system call creates new process (duplicate of parent )
 exec system call used after a fork to replace the process’
memory space with a new program.
 Wait()  If the parent has nothing to do while child runs ( or
till child completes ) then parent can issue a wait system call
to move itself off the ready queue until the termination of the
child

OS Notes by Dr. Naveen Choudhary


Process Termination
 When a processes terminates, all the resources of the process,
including physical or virtual memory, open files and i/o buffers are de
allocated by O.S
 When Process executes last statement and asks the operating system
to delete it by implictly/explict call to the exit system call
 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.
 User could kill another process using the kill system call (kill
generates a signal to abort a process but the process can ignore it
(in most of the cases )
 The wait system call returns the process identifier of a terminated
child, so that the parent can tell which of it’s possibly many children
has terminated
 If the parent terminates, however all the children are terminated by
the O.S. Without a parent, Unix does not know to whom to report
the activities of the child

OS Notes by Dr. Naveen Choudhary


Cooperating Processes
 Independent process cannot affect or be affected by the
execution of another process. ( any process that does not
share any data (temporary or persistent with any other
processes is independent )
 Cooperating process can affect or be affected by the
execution of another process ( any process that share
data with other processes is co – operating )
 Advantages of process cooperation
 Information sharing
 Computation speed-up (big program can be divided in many
small program and they can run concurrently and can share
data whenever required
 Modularity (logically breaking down a program in smaller
ones make the processes/program more modular
 Convenience

OS Notes by Dr. Naveen Choudhary


Producer-Consumer Problem
To be co-operative (share data ) the process need to
communicate  IPC (inter process communication )

 A good example of co-operative process is  producer


consumer problem
 Paradigm for cooperating processes, producer process
produces information that is consumed by a consumer
process.
 Example :
 producer = print program produces character, consumer = printer driver
 Producer = compiler produces assembly code, consumer = assembler
 Producer = assembler produces object code, consumer = loader
 The producer and consumer must be synchronized, so
that the consumer does not try to consume an item that
has not yet been produced. In this situation, consumer
must wait until an item is produced
 Producer consumer need to share some memory (buffer )
for information exchange these can be of two types
 unbounded-buffer places no practical limit on the size of the
buffer.
 bounded-buffer assumes that there is a fixed buffer size.

OS Notes by Dr. Naveen Choudhary


Bounded-Buffer – Shared-Memory Solution
 Shared data
#define BUFFER_SIZE 10
Typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0; //rear
int out = 0; //front
 // Solution is correct, but can only use BUFFER_SIZE-1
elements

OS Notes by Dr. Naveen Choudhary


Bounded-Buffer – Producer Process

item nextProduced;

while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}

OS Notes by Dr. Naveen Choudhary


Bounded-Buffer – Consumer Process

item nextConsumed;

while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}

OS Notes by Dr. Naveen Choudhary


Interprocess Communication (IPC)
1) shared memory (as in producer consumer problem)
2) Message system as discussed below

 Mechanism for processes to communicate and to


synchronize their actions  definition of IPC.
 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 of communication link
 physical (e.g., shared memory, hardware bus (link))

OS Notes by Dr. Naveen Choudhary


Direct Communication
 Processes must name each other explicitly (symmetric addressing ):
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
 Asymmetric addressing : only sender need to name the
receiver, the receiver is not required to name the sender
{ send (p, messages), receive (id, message ), id will be
automatically set the identity of the message sender }
 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.

OS Notes by Dr. Naveen Choudhary


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. (many
members may be members of mailbox)
 Each pair of processes may share several communication
links. (some process may be sharing more than one
mailbox)
 Link may be unidirectional or bi-directional.

OS Notes by Dr. Naveen Choudhary


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

OS Notes by Dr. Naveen Choudhary


Synchronization

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

OS Notes by Dr. Naveen Choudhary


Buffering

 Queue of messages attached to the link; implemented in


one of three ways.
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous) sender will
block till the receiver receives the message
2. Bounded capacity – finite length of n messages
Sender must wait if link full.
3. Unbounded capacity – infinite length
Sender never waits.

OS Notes by Dr. Naveen Choudhary


Other IPC Issues

 Process termination  if sender/receiver terminates there


should be some way for the process (receiver/sender) at
the other end to come to know about the termination
(soln) Ack, multiple time outs
 Lost messages  (soln) Ack, time out (time out should
be intelligently fixed otherwise duplicate may appear at
the receiver and to solve this problem we will require
duplicate filtering
 Scrambled message  use error checking codes like
checksums, parity or CRC (cyclic redundancy check ) and
then tell the sender to resend if error is detected

OS Notes by Dr. Naveen Choudhary


OS Notes by Dr. Naveen Choudhary

You might also like