You are on page 1of 38

Module 4: Processes

• Process Concept
• Process Scheduling
• Operation on Processes
• Cooperating Processes
• Interprocess Communication

Operating System Concepts 4.1 Silberschatz and Galvin 1999


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.
• A process includes:
– program counter
– stack
– data section

Operating System Concepts 4.2 Silberschatz and Galvin 1999


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.

Operating System Concepts 4.3 Silberschatz and Galvin 1999


Diagram of Process State

Operating System Concepts 4.4 Silberschatz and Galvin 1999


Process Control Block (PCB)

Information associated with each process.


• Process state: may be new, ready, halted and so on
• Program counter: indicates the address of the next instruction to be executed
for this process

• CPU registers: The registers very in number and type, depending on the
computer architecture. Along with the program counter, the state information must
be saved when an interrupt occurs, to allow the process to be continued correctly
afterword.

• 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 the value
of base and limit registers, page tables or segment table depending on the
memory system used by the OS
• Accounting information: includes the amount of CPU and real time used,
time limit, process number etc.

Operating System Concepts
I/O status information 4.5 Silberschatz and Galvin 1999
Process Control Block (PCB)

Operating System Concepts 4.6 Silberschatz and Galvin 1999


CPU Switch From Process to Process

Operating System Concepts 4.7 Silberschatz and Galvin 1999


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 device queue.
• Process migration between the various queues.
• Once the process is assigned to the 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 form the CPU, as a result
of an interrupt and be put back in the ready queue.

Operating System Concepts 4.8 Silberschatz and Galvin 1999


Ready Queue And Various I/O Device Queues

Operating System Concepts 4.9 Silberschatz and Galvin 1999


Representation of Process Scheduling

Operating System Concepts 4.10 Silberschatz and Galvin 1999


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.

Operating System Concepts 4.11 Silberschatz and Galvin 1999


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

Operating System Concepts 4.12 Silberschatz and Galvin 1999


Schedulers (Cont.)

• Some system does not user long term scheduler. For example:
time-sharing system such as UNIX often have no long term
scheduler but simply put every new process in the memory for
the short term shceduler.
• These operating systems may introduce an additional,
intermediate level of scheduling.
• These medium term scheduler removes processes from memory
and thus reduces 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.

Operating System Concepts 4.13 Silberschatz and Galvin 1999


Addition of Medium Term Scheduling

Operating System Concepts 4.14 Silberschatz and Galvin 1999


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

Operating System Concepts 4.15 Silberschatz and Galvin 1999


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.

Operating System Concepts 4.16 Silberschatz and Galvin 1999


A Tree of Processes On A Typical UNIX System

Operating System Concepts 4.17 Silberschatz and Galvin 1999


Process Creation (Cont.)

• Address space
– The child process is a duplicate of the parent process.
– Child has a program loaded into it.
• UNIX examples
– fork system call creates new process
– execve system call used after a fork to replace the process’
memory space with a new program.

Operating System Concepts 4.18 Silberschatz and Galvin 1999


Fork

• In Unix, all processes are created with the system call fork().
This is without exception. What fork() does is the following:
• It creates a new process which is a copy of the calling process.
That means that it copies the caller's memory (code, globals,
heap and stack), registers, and open files. The calling process
returns from fork() with the pid of the newly created process
(which is called the "child" process. The calling process is called
the "parent" process). The newly created process, as it is a
duplicate of the parent, also returns from the fork() call (this is
because it is a duplicate -- it has the same memory and registers,
and thus the same stack pointer, frame pointer, and program
counter, and thus has to return from the fork() call). It returns
with a value of zero. This is how you know what process you're in
when fork() returns.

Operating System Concepts 4.19 Silberschatz and Galvin 1999


fork

main()
{
int i;
printf("simpfork: pid = %d\n", getpid());
i = fork();
printf("Did a fork. It returned %d. getpid = %d. getppid = %d\n", i,
getpid(), getppid());
}

Operating System Concepts 4.20 Silberschatz and Galvin 1999


fork

• When it is run, the following happens:


simpfork: pid = 914
Did a fork. It returned 915. getpid = 914. getppid = 381
Did a fork. It returned 0. getpid = 915. getppid = 914
• So, what is going on?
• When simpfork is executed, it has a pid of 914. Next it calls
fork() creating a duplicate process with a pid of 915. The parent
gains control of the CPU, and returns from fork() with a return
value of the 915 -- this is the child's pid. It prints out this return
value, its own pid, and the pid of csh, which is still 381.
• Then it exits. Next, the child gets the CPU and returns from
fork() with a value of 0. It prints out that value, its pid, and the pid
of the parent.

Operating System Concepts 4.21 Silberschatz and Galvin 1999


fork

• Note, there is no guarantee which process gains control of the


CPU first after a fork(). It could be the parent, and it could be the
child. When I executed simpfork a second time, the child got
control first:
simpfork: pid = 928
Did a fork. It returned 0. getpid = 929. getppid = 928
Did a fork. It returned 929. getpid = 928. getppid = 381
• (on some machines, it does appear that the child always gets
control first, but you should not rely on such a fact when writing
code).

Operating System Concepts 4.22 Silberschatz and Galvin 1999


Process Termination

• Process executes last statement and asks the operating system


to terminate by using exit system call.
– The process may return output to its parent process(via
wait).
– Process’ resources are deallocated by operating system.
• Termination may occur under additional causes.
• 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.

Operating System Concepts 4.23 Silberschatz and Galvin 1999


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 : Since several users may be interested in the
same piece of information, we must provide and environment to allow
concurrent access to these types of resources.
– Computation speed-up: If we want a particular task to run faster, we
must break it into subtasks, each of which will be executing in parallel with the
others.
– Modularity: We may want to construct the system in a modular fashion,
dividing the system functions into separate processes.
– Convenience: Even an individual user may have many tasks on which to
work at one time.

Operating System Concepts 4.24 Silberschatz and Galvin 1999


Continue..

• Cooperating processes require an interprocess


communication (IPC) mechanism that will allow them to
exchange data and information.
• Two fundamental models:
– Shared memory
– Message passing
• In shared memory model, 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.
• In the message passing model, communication takes place by
means of message exchanged between the cooperating
processes.

Operating System Concepts 4.25 Silberschatz and Galvin 1999


Communications Models

Message Passing Shared Memory

4.26 Silberschatz and Galvin 1999


Shared memory system

• Interprocess communication using shared memory requires


communicating processes to established a region of shared
memory.
• Typically, a shared-memory region resides in the address space
of the process creating the shared memory segment.
• Other processes that wish to communicate using this scheme
must attach it to their address space.
• Normally, OS tries to prevent one process from accessing
another process’s memory
• Shared memory requires that two or more processes agree to
remove this restriction.
• They can then read or write data from the shared memory.

Operating System Concepts 4.27 Silberschatz and Galvin 1999


Producer-Consumer Problem

• Paradigm for cooperating processes, producer process produces


information that is consumed by a consumer process.
• For example: a compiler may produce assembly code, which is
consumed by an assembler. Again, the assembler may produce
object module which is consumed by the loader.
• To allow producer and consumer to run concurrently, we must
have available buffer of items that be filled by the producer and
emptied by the consumer.
– unbounded-buffer places no practical limit on the size of the
buffer.
– bounded-buffer assumes that there is a fixed buffer size.
• The buffer may either be provided by the operating system
through the use of an IPC facility or by explicitly coded by the
application programmer with the use of shared memory.

Operating System Concepts 4.28 Silberschatz and Galvin 1999


Bounded-Buffer – Shared-Memory Solution

• Shared data
var n;
type item = … ;
var buffer. array [0..n–1] of item;
in, out: 0..n–1;
• Producer process
repeat

produce an item in nextp

while in+1 mod n = out do no-op;
buffer [in] :=nextp;
in :=in+1 mod n;
until false;

Operating System Concepts 4.29 Silberschatz and Galvin 1999


Bounded-Buffer (Cont.)

• Consumer process

repeat
while in = out do no-op;
nextc := buffer [out];
out := out+1 mod n;

consume the item in nextc

until false;
• Solution is correct, but can only fill up n–1 buffer.

Operating System Concepts 4.30 Silberschatz and Galvin 1999


Message passing system

• processes communicate with each other without resorting to shared


variables.
– Communication among the user processes is accomplished
through the passing message.
• IPC facility provides at least 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

Operating System Concepts 4.31 Silberschatz and Galvin 1999


Logical implementation

• Direct or indirect communication


• Synchronous or asynchronous communicaiton
• Automatic or explicit buffering

Operating System Concepts 4.32 Silberschatz and Galvin 1999


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 between every pair of
processes that want to communicate. The processes need
to know only each other’s identity to communicate.
– 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 scheme is symmetric in addressing since both the sender
and receiver process must name to other to communicate.
• In asymmetric addressing only sender names the recipient; the
recipient is not required to name the sender.
Operating System Concepts 4.33 Silberschatz and Galvin 1999
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.
• Operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox

Operating System Concepts 4.34 Silberschatz and Galvin 1999


Indirect Communication (Continued)

• The messages are sent to and received from mailboxs or ports.


• A mailbox can be viewed abstractly as an object into which
messages can be places by processes and from which
messages can be removed.
• Each mailbox has a unique identification
• Two processes can communicate if they have a shared mailbox.
• The send() and receive() primitives are defined as follows:

send(A, message)---Send a message to mailbox A


receive(A, message)---Receive a message from mailbox A.

Operating System Concepts 4.35 Silberschatz and Galvin 1999


• Communication link has the following properties:
– A pair of link is established between a pair of processes
only if both members of the pair have a shared mail box
– A link may be associated with more than two processes
– Between each pair of communicating processes there may
be a number of different link, with each link corresponding to
one mail box.
• 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
Operating System Concepts 4.36 Silberschatz and Galvin 1999
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).
2. Bounded capacity – finite length of n messages
Sender must wait if link full.
3. Unbounded capacity – infinite length
Sender never waits.

Operating System Concepts 4.37 Silberschatz and Galvin 1999


Assignment….

• Write a C program that illustrates the creation of child


process using fork system call. One process finds sum of
even series and other process finds sum of odd series

Operating System Concepts 4.38 Silberschatz and Galvin 1999

You might also like