0% found this document useful (0 votes)
7 views16 pages

OS Unit 2

BIM 8 sem operating system

Uploaded by

amangrg9845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

OS Unit 2

BIM 8 sem operating system

Uploaded by

amangrg9845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Operating System (Unit 2) Prepared By: Sujesh Manandhar

UNIT 2
Process Management
A process is a program in execution. It is the unit of work in modern time sharing
system. When the program is loaded into the memory it becomes process and it is
divided into four section: stack, heap, text and data. Although its main concern is to
execute user program, it also need to take care of various system tasks. A system
therefore consist of a collection of processes like operating system process executing
system code and user processes executing user code. All the process can execute
concurrently with the CPU multiplexed among them.
A process is more than a program code which is sometimes known as a text section.
Text section also includes the current activity represented by the value of program
counter and the content of processor’s register. Process generally also includes the
process stack which contains temporary data like function, parameter, return
address and local variables. It also contains data section which contains global
variable and includes heap also which is a memory that is dynamically allocated
during process run time. The structure of process is shown below:

Figure: Process in memory

For: BIM 8th Semester Page | 1


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Difference between program and process:


Program by itself is not a process. Program is a passive entity such as file containing
a set of instruction stored on the disk whereas process is an active entity with a
program counter specifying the next instruction to execute and a set of associated
resources. A program becomes process when an executable file is loaded into
memory.
Process State:
As a process executes it changes state. The state of the process is defined in part by
the current activity of that process. A process may be in one of the following states:
• New: this is the initial state in which the process is being created or started.
• Running: it is the state in which instruction are being executed.
• Waiting: the process is waiting for some event to occur such as I/O
completion or reception of signal. Process moves to this state if it need to wait
for some resources or waiting for a file to become available.
• Ready: here the process will be waiting to be assigned to a processor so that
they can run. Process may come into this state after the start state or while
running.
• Terminated: process moves to terminated state once it complete its execution
or is terminated by the operating system. Here it waits to be remove from main
memory.
It is important to realize that only one process can be running on any processor at
any instant, other may be ready and waiting.

Figure: Process State

For: BIM 8th Semester Page | 2


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Process Control Block:


Each process is represented in the operating system by a process control block also
known as task control block. It is the data structure maintained by the operating
system for every process. PCB simply serves as the repository for an information
that may vary from process to process. It contains many pieces of information
associated with a specific process including:
• Process state: it refers to current state of the process i.e. the state may be new,
ready, running and waiting, halted and so on.
• Program counter: the counter indicates the address of the next instruction to
be executed for this process.
• CPU register: it includes various CPU registers where process needs to be
stored for execution for running state. The register may vary in number and
type depending on a computer architecture. They includes accumulator, index
register, stack pointer, any condition code information. The state information
must be saved when interrupt occurs to allow the process to be continued
correctly afterward.
• 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 items like
value of the base and limit register, page table, segment etc. depending upon
the memory system used by the operating system.
• Accounting information: this information includes the amount of CPU time
and real time used, time limits, account number, job or process number and
so on.
• I/O status information: this information includes the list of I/O devices
allocated to the process, a list of open file and so on.

The architecture of PCB completely depends on operating system used and may
contains different information in different operating system. The structure of PCB is
shown below:

For: BIM 8th Semester Page | 3


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Figure: Process Control Block (PCB)

Figure: diagram showing CPU switch from process to process

For: BIM 8th Semester Page | 4


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Process scheduling:
The objective of multiprogramming is to have some process running at all times such
that CPU utilization is maximized. Similarly, the objective of time sharing system is
to switch CPU among the process so frequently that user can interact with each
program while it is running. To meet these objective, the process scheduler selects
an available process from a set of processes for program execution in CPU.
As process enters the system they are put into the job queues which consist of all the
process in the system. The process that are residing in main memory and are ready
to execute are kept on a list called ready queue. This queue is generally stored in
linked list and a ready queue headers contains pointer to the first and last PCB in the
list. Each PCB contains a pointer field that points to the next PCB in the ready queue.
When a process is allocated the CPU, it executes for a while then eventually quits or
interrupted or wait for the occurrence of particular event like completion of I/O
request. Thus the process may have to wait for disk. The list of process waiting for
a particular I/O device is called device queue.

Figure: queuing diagram representing of process scheduling

For: BIM 8th Semester Page | 5


Operating System (Unit 2) Prepared By: Sujesh Manandhar

The above figure shows the representation of process scheduling. Each rectangular
box represents a queue. Here two types of queues are present: the ready queue and a
set of device queue. The circle represents the resources that serve the queues and
arrow represents flow of process in the system.
A new process is initially put into ready queue and waits there until it is selected for
execution or dispatched. Once the process is allocated to CPU and is executing
following events could occurs:
• A process could issues an I/O request and then be placed on I/O queue.
• A process could create a new child process and for child termination
• The process could be removed forcibly from CPU due to interrupt and be put
back in the ready queue.
In the first two case process eventually switches from waiting state to the ready state
and is then put back into ready queue and continues until it is terminated. The address
space of the current process must be preserved as the space of the next task is
prepared for use.
Process Context Switch:
Switching the CPU to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as context
switch. Because of this CPU can resumes the process execution from the same point
at which it has been stopped. When a context switch occur the kernel saves the
context of old process in its PCB and load the saved context of the new process
scheduled to run. Context-switching is pure overhead because system does no useful
work while switching. Switching speed varies from machine to machine depending
upon memory speed, number of register and the existence of special instruction.
Context switch times are highly dependent on hardware

Process Address Space:


An address space is the range of valid address in the memory that are available for
the program or process i.e. the memory, process and program can access. It means a
space that is allocated in memory for a process. One process cannot access another
address space and same pointer address in different processes point to different
memory. The process has at least three segment of usable address.

For: BIM 8th Semester Page | 6


Operating System (Unit 2) Prepared By: Sujesh Manandhar

• A text segment that contains the executable image of the program


• A data segment contains the heap of dynamically allocated data
• A stack segment contains the function call stack.

Operation on Processes:
The process in most of the system can execute concurrently and they must be created
and deleted dynamically. The system must provide the mechanism for process
creation and termination. The mechanism involved in creating and terminating
process in UNIX system is shown below:
1. Process creation:
A process may create several new process during the course of execution. The
creating process is called parent process and the new process are called the
children of that process. Each of these process may in turn create other
processes forming a tree of process known as hierarchies of process.
Processes are identified according to the unique process identifier or pid,
which is typically an integer value. The pid provides a unique value for each
process in the system and it can be used as an index to access various attributes
of a process within the kernel.
The operating system can run many process at the same time but initially it
directly starts the one process called init (initial) process. The init process
which always has a pid 1 serve as the root parent process for all user process.
When a process creates a new process, the two possibilities for execution
exist:
• The parent continue to execute concurrently with its children
• The parent waits until some or all of its children have terminated.
There are also two address space possibility for the new process:
• The child process is duplicate of the parent process i.e. it has the same
program and data as the parent.
• The child process has a new program loaded into it.
When a process created a child process, that child process will need certain
resources (CPU time, memory etc.) to accomplish the task. Such resources
can be obtain directly from the operating system or it may be constrained to
the subset of resource of the parent process.

For: BIM 8th Semester Page | 7


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Following figure illustrate the typical process tree for Linux operating system,
showing the name of the process and pid. Once the system has started, the init
process can create various other process such as web or print server, ssh server
etc.

Figure: A tree of processes on a typical Linux system

In above figure, there are three children of init: kthreadd, sshd and login. The
kthreaded process is responsible for creating additional process that perform
the task in behalf of kernel. The sshd process is responsible for managing the
client that connected to system by using ssh (secure shell).
The login process is responsible for the managing the client that directly logs
on to the system. Here, a clients have logged in and is using a bash shell which
has been assigned pid 8416. Using the bash command line user has created
the process ps and emacs editor.

For: BIM 8th Semester Page | 8


Operating System (Unit 2) Prepared By: Sujesh Manandhar

The following C program illustrate the creation and execution of child process in
UNIX system:
#include <stdio.h>
#include <sys/types.h>
int main (){
pid_t pid;

pid = fork() /* creating new process*/

if(pid<0){
fprintf(stderr, “Fork failed”);
return -1;
}
else if(pid==0){
/* child process */
execlp(“/bin/ls”, “ls”, NULL);
}
else{
/* parent process */
wait (NULL);
printf(“child complete”);
}
return 0;
}

For: BIM 8th Semester Page | 9


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Flow of execution:
Here we have two different process running copies of the same program. The new
process is created by fork() system call. This new process consist the copy of the
address of the parent process (main()) because of which parent process can
communicate with child process. The value of the pid for the child process is zero
while for the parent it is the integer value greater than zero.
The exec() or execlp() system call replace the process’s memory space with new
program and starts its execution. The child process then overlaps its address space
with the UNIX command /bin/ls using the execlp() system call. The parent waits for
the child process to complete with the wait() system call.
When the child process complete by invoking exit() system call, the parent process
resumes from the call to wait().

Figure: Process creation using fork() system call.

2. Process Termination:
A process terminates when it finishes executing its final statement and ask the
operating system to delete it by using exit() system call. At this point process
may return the status value to its parent process via the wait() system call. All
the resource that process consumed are deallocated by the operating system.
A parent process may terminated the execution of one of its children for a
variety of reasons such as:
• The child has exceeded its usage of some of the resources that it has
been allocated.
• The task assigned to the children is no longer required.
• The parent is exiting and the OS does not allow a child to continue if
its parent terminate.

For: BIM 8th Semester Page | 10


Operating System (Unit 2) Prepared By: Sujesh Manandhar

In some system, if parent terminate then child process are not allowed to exist
i.e. child process also have to terminate. This phenomenon is referred as
cascading termination which is normally initiated by operating system.
In Linux and UNIX system, process are terminated by using exit() system call
providing status as parameter. Exit may be called either directly as shown
below or indirectly (by a return statement in main()).
exit(1)
A process that has been terminated but whose parent has not yet called wait()
is known as zombie process. Once the process call wait(), the process
identifier of the zombie process and its entry in the process table are released.

Inter-Process Communication:
Process executing concurrently in the operating system may require to exchange
message between them. Such concurrently executing process may either be
independent process or cooperating process. Any process that does not shares the
data with any other process is independent process. Such process cannot affect or
be affected by the other processes executing in the system.
Any process that shares data with other processes is a cooperating process. Such
process can affect or be affected by other process executing in the system. There are
several reason for providing inter process communication such as for information
sharing, computation speedup, modularity, convenience.
Inter-process communication mechanism are required for cooperating process in
order to exchange data and information. There are two fundamental model for inter-
process communication:
• Shared memory
• Massage passing

1. Shared Memory:
In shared memory, a region of the memory that is shared by the cooperating
process is established. The process can then exchange information by reading
and writing data to the shared region. A shared memory region resides in the
address space of the process creating the shared memory segment. Other

For: BIM 8th Semester Page | 11


Operating System (Unit 2) Prepared By: Sujesh Manandhar

process that wish to communicate using this shared memory region must
attach address space of the process that have created the shared memory
region. Generally, operating system tries to prevent one process from
accessing other process’s space but for shared memory two process must have
to remove this restriction. They can then exchange information by reading and
writing data in the shared area.
System calls are requires only to establish shared-memory region. Once
shared memory is established, all access are treated as routine memory access
and assistance from kernel is not required. The form of the data and location
are determined by these process and are not under the operating system’s
control. Process are also responsible for ensuring that they are not writing to
same location simultaneously.

Figure: Shared Memory:


2. Message passing system:
In message passing system, communication takes place by means of message
exchange between the cooperating processes. Message passing is useful for
exchanging small amount of data. This mechanism allows processes to
communicate and to synchronize their actions without sharing the same
address space.

For: BIM 8th Semester Page | 12


Operating System (Unit 2) Prepared By: Sujesh Manandhar

It is particularly useful in a distributed environment, where the


communicating processes may reside on different computer connected by a
network. A massage passing facility provides at least two operations: send
(message) and receive (message). Message sent by a process can either be
fixed or variable in size. This system are typically implemented using system
calls and thus require more time-consuming task of kernel intervention.

Figure: Message Passing System

For the two or more process to communicate, the communication link must
exist between them. Here link are implemented logically and their methods
are illustrated below:
• Direct or indirect communication:
Under direct communication, each process that wants to communicate
must explicitly name the recipient or sender of the communication. This
scheme exhibits either symmetry in addressing where both the sender
and the receiver process must name the other to communicate or
asymmetry in address where only sender name the recipient but
recipient is not required to name the sender. Both scheme are shown
below:

For: BIM 8th Semester Page | 13


Operating System (Unit 2) Prepared By: Sujesh Manandhar

For symmetry:
Send (P, message) – send a message to process P.
Receiver (Q, message) – receive a message from process Q.
For asymmetry:
send (P, message) – send a message to process P.
receiver (id, message) – receive a message from any process. Id
specifies the name of the process with which communication has taken
place.
Here link is established automatically and associated with exactly two
process with exactly one link.

With the indirect communication the message are sent to receive from
mail box or ports. Each mail-box have a unique identification and can
be view abstractly as an object into which message can be placed and
removed by process. Two process can communicate if they have shared
mail-box. The send () and receive () primitive are shown below:
send (A, message) – send a message to mailbox A
receive (A, message) – receives a message form mailbox B.
Here, a link may be associated with more than two processes but for
two process to communicate they must share common mail box.

• Synchronization:
Message passing may be either blocking or non-blocking also known
as synchronous and asynchronous. In Blocking send: the sending
process is blocked until the message is received by the receiving
process or by the mailbox. In non-blocking send: the sending process
sends the message and resumes operation. In Blocking receive: the
receiver block until a message is available. In Non-blocking receive the
receiver retrieves either a valid message or a null.
When both send () and receive () are blocking there is a rendezvous
point between the sender and the receiver.

• Buffering:
Whether the communication is direct or indirect, message exchanged
by communicating process reside in a temporary queue. Such queues
can be implemented in three ways:

For: BIM 8th Semester Page | 14


Operating System (Unit 2) Prepared By: Sujesh Manandhar

o Zero capacity: the queue has maximum length of zero therefore,


link cannot have any message waiting for it. The sender must
block until receiver receives the message. This case is sometimes
refer as no buffering or automatic buffering.
o Bounded capacity: the queue has finite length n and at most n
message can reside in it. If the queue is not full when a new
message is sent then message are kept in queue and sender a can
continue execution without waiting. If the link is full sender must
block until space is available in the queue.
o Unbounded capacity: the queue’s length is potentially infinite,
thus any number of message can wait in it. The sender never
block.

Scheduler:
A process migrate among the various scheduling queues throughout its life time. The
operating system must select process from this queues for the process to execute.
This selection process is carried out by scheduler. Hence, the work of scheduler is
to select the appropriate process from the queue for execution.
In a batch system, more process are submitted than that can be executed. Such
process are spooled or kept in mass storage for later execution. In this case, the long-
term scheduler or job scheduler selects processes from this pool and loads them into
the memory for execution. Another scheduler known as short term scheduler or CPU
scheduler selects the process that are ready to execute and allocated the CPU to one
of them.
Time sharing system have introduce an additional intermediate level of scheduling
known as medium term scheduler. Here the process is swapped out with other
process and swapped in later by medium term scheduler. The running process are
removed first (swap out) and placed in separate queue and later on it is reintroduces
in the memory and its execution can be continued where it is left off. This scheme is
known as swapping.

For: BIM 8th Semester Page | 15


Operating System (Unit 2) Prepared By: Sujesh Manandhar

Figure: Medium Term Scheduling

Note: For further material scan following QR code:

For: BIM 8th Semester Page | 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy