You are on page 1of 60

Operating Systems (Linux Programming and Administration) Lab

Paper code : ETCS-352

Maharaja Agrasen
Institute Of Technology
PSP Area,Sector-22,Rohini,New Delhi -110086

Submitted To : Submitted By :

Ms. Shallu Juneja Jatin Malhotra


(Assistant Professor) ( Enrollment Number : 41914802718 )
(Computer Science Department) ( Branch : Computer science )
( Semester : 6h )
( Batch : 6C7 )
MAHARAJA AGRASEN INSTITUTE OF TECHNOLOGY, DELHI

Mission

The Institute shall endeavor to incorporate the following basic missions in the teaching methodology:

Engineering Hardware – Software Symbiosis

Practical exercises in all Engineering and Management disciplines shall be carried out by Hardware equipment
as well as the related software enabling deeper understanding of basic concepts and encouraging inquisitive
nature.

Life – Long Learning

The Institute strives to match technological advancements and encourage students to keep updating their
knowledge for enhancing their skills and inculcating their habit of continuous learning.

Liberalization and Globalization

The Institute endeavors to enhance technical and management skills of students so that they are intellectually
capable and competent professionals with Industrial Aptitude to face the challenges of globalization.

Diversification

The Engineering, Technology and Management disciplines have diverse fields of studies with different
attributes. The aim is to create a synergy of the above attributes by encouraging analytical thinking.

Digitization of Learning Processes

The Institute provides seamless opportunities for innovative learning in all Engineering and Management
disciplines through digitization of learning processes using analysis, synthesis, simulation, graphics, tutorials
and related tools to create a platform for multi-disciplinary approach.

Entrepreneurship

The Institute strives to develop potential Engineers and Managers by enhancing their skills and research
capabilities so that they become successful entrepreneurs and responsible citizens.

Vision

To nurture young minds in a learning environment of high academic value and imbibe spiritual and ethical
values with technological and management competence.
Department of Computer Science & Engineering
An NBA Accredited Program

Mission

To foster an open, multidisciplinary and highly collaborative research environment to produce world-class
engineers capable of providing innovative solutions to real-life problems and fulfil societal needs.

Vision

To be centre of excellence in education, research and technology transfer in the field of computer engineering
and promote entrepreneurship and ethical values.
Course Outcomes
LIST OF EXPERIMENTS (As prescribed by
G.G.S.I.P.U)

Paper Code: ETCS-352 L T/P C


Paper: Operating Systems (Linux Programming and Administration) Lab 02 1

List of Experiments:

1. Write a program to implement CPU scheduling for first come first serve.
2. Write a program to implement CPU scheduling for shortest job first.
3. Write a program to perform priority scheduling.
4. Write a program to implement CPU scheduling for Round Robin.
5. Write a program for page replacement policy using a) LRU b) FIFO c) Optimal.
6. Write a program to implement first fit, best fit and worst fit algorithm for memory
management.
7. Write a program to implement reader/writer problem using semaphore.
8. Write a program to implement Banker‟s algorithm for deadlock avoidance.

NOTE: - At least 8 Experiments out of the list must be done in the semester.

18 | P a g e
AIT/CSE
M
LIST OF EXPERIMENTS (Beyond the syllabus)

Paper Code: ETCS-352 L T/P C


Paper: Operating Systems (Linux Programming and Administration) Lab 0 2 1
List of Experiments:

1. Installation of Linux operating system.

2. Introduction of Basics Files, directories, system Commands of Linux.

3. i) Write a script to find the greatest of three numbers (numbers passed as command line
parameters) ii) Write a script to check whether the given no. is even / odd iii) Write a script
to check whether the given number is prime or not iv) Write a script to check whether the
given input is a number or a string v) Write a script to compute no. of characters and words
in each line of given file

4. i) Write a script to calculate the average of n numbers ii) Write a script to print the
Fibonacci series up to n terms iii) Write a script to calculate the factorial of a given number
iv) Write a script to calculate the sum of digits of the given number v) Write a script to
check whether the given string is a palindrome

19 | P a g e
AIT/CSE
M
Index

S.No. Aim of Experiment Date of Marks Marks Marks Marks Marks Total
Conduct (R1) (R3) (R3) (R4) (R5) Marks

(3) (3) (3) (3) (3) (15)

1. Write a program 25
to implement CPU March
scheduling for first 2021
come first serve.

2. Write a program 8 April


to implement CPU 2021
scheduling for
shortest job first.

3. Write a program 15 April


to perform priority 2021
scheduling.

4. Write a program 22 April


to implement CPU 2021
scheduling for
Round Robin.

5. Write a program 29 April


to implement first 2021
fit, best fit and
worst fit algorithm
for memory
management.

6. Write a program 20 May


for page 2021
replacement
policy using a)
LRU b) FIFO c)
Optimal.

7. Write a program 27 May


to implement 2021
reader/writer
problem using
semaphore.

8. Write a program 27 May


to implement 2021
Banker’s
algorithm for
deadlock
avoidance.
INTRODUCTION TO OPERATING SYSTEM

Introduction

An operating system (OS) is the software component of a computer system that is responsible
for the management and coordination of activities and the sharing of the resources of the
computer. The OS acts as a host for application programs that are run on the machine.

As a host, one of the purposes of an OS is to handle the details of the operation of the
hardware. This relieves application programs from having to manage these details and makes
it easier to write applications. Almost all computers use an OS of some type. OSs offer a
number of services to application programs and users.

Applications access these services through application programming interfaces (APIs) or


system calls. By using these interfaces, the application can request a service from the OS,
pass parameters, and receive the results of the operation. Users may also interact with the OS
by typing commands or using a graphical user interface (GUI).

Common contemporary OSs include Microsoft Windows, Mac OS X, and Linux. Microsoft
Windows has a significant majority of market share in the desktop and notebook computer
markets, while the server and embedded device markets are split amongst several OSs

Services Offered by an Operating System

An Operating System provides services to both the users and to the programs.
● It provides programs an environment to execute.
● It provides users the services to execute the programs in a convenient manner.
Following are a few common services provided by an operating system −
● Program execution
● I/O operations
● File System manipulation
● Communication
● Error Detection
● Resource Allocation
● Protection

Program execution
Operating systems handle many kinds of activities from user programs to system programs
like printer spooler, name servers, file server, etc. Each of these activities is encapsulated as
a process.
A process includes the complete execution context (code to execute, data to manipulate,
registers, OS resources in use). Following are the major activities of an operating system
with respect to program management −
● Loads a program into memory.
● Executes the program.
● Handles program's execution.
● Provides a mechanism for process synchronization.
● Provides a mechanism for process communication.
● Provides a mechanism for deadlock handling.

I/O Operation
An I/O subsystem comprises I/O devices and their corresponding driver software. Drivers
hide the peculiarities of specific hardware devices from the users.
An Operating System manages the communication between user and device drivers.
● I/O operation means read or write operation with any file or any specific I/O device.
● Operating system provides access to the required I/O device when required.

File system manipulation


A file represents a collection of related information. Computers can store files on the disk
(secondary storage), for long-term storage purpose. Examples of storage media include
magnetic tape, magnetic disk and optical disk drives like CD, DVD. Each of these media has
its own properties like speed, capacity, data transfer rate and data access methods.
A file system is normally organized into directories for easy navigation and usage. These
directories may contain files and other directions. Following are the major activities of an
operating system with respect to file management −
● Program needs to read a file or write a file.
● The operating system gives the permission to the program for operation on file.
● Permission varies from read-only, read-write, denied and so on.
● Operating System provides an interface to the user to create/delete files.
● Operating System provides an interface to the user to create/delete directories.
● Operating System provides an interface to create the backup of a file system.

Communication
In case of distributed systems which are a collection of processors that do not share memory,
peripheral devices, or a clock, the operating system manages communications between all
the processes. Multiple processes communicate with one another through communication
lines in the network.
The OS handles routing and connection strategies, and the problems of contention and
security. Following are the major activities of an operating system with respect to
communication −
● Two processes often require data to be transferred between them
● Both the processes can be on one computer or on different computers, but are
connected through a computer network.
● Communication may be implemented by two methods, either by Shared Memory or
by Message Passing.

Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices or in the
memory hardware. Following are the major activities of an operating system with respect to
error handling −
● The OS constantly checks for possible errors.
● The OS takes an appropriate action to ensure correct and consistent computing.

Resource Management
In case of multi-user or multi-tasking environments, resources such as main memory, CPU
cycles and files storage are to be allocated to each user or job. Following are the major
activities of an operating system with respect to resource management −
● The OS manages all kinds of resources using schedulers.
● CPU scheduling algorithms are used for better utilization of CPU.

Protection
Considering a computer system having multiple users and concurrent execution of multiple
processes, the various processes must be protected from each other's activities.
Protection refers to a mechanism or a way to control the access of programs, processes, or
users to the resources defined by a computer system. Following are the major activities of an
operating system with respect to protection −
● The OS ensures that all access to system resources is controlled.
● The OS ensures that external I/O devices are protected from invalid access attempts.
● The OS provides authentication features for each user by means of passwords.
Linux V/S Windows

Parameter Linux Windows

Access Users can access the source code of Usually, users cannot access the
the kernel in Linux and can alter source code. However, members of
the kernel according to need. some groups can have access to it.

Variety Linux has several distributions that Windows have fewer options to
are highly customizable. customize.

Command- The command line is usually Windows also has a command line,
Line referred to as Terminal, which is but it is not as effective as a
the most useful tool of the Linux comparison to the Linux terminal.
system. It is used for Most users prefer the GUI options for
administration and daily tasks. For daily tasks.
the end-users, it does not look so
effective.

Installation The Linux installation process is a Windows OS is easy to install and set
bit complicated to set up as it up on a machine; it requires fewer
requires many user inputs. It takes user input options during installation.
less time than Windows to install. However, it takes more time to install
as compared to Linux.

Ease of use The Linux OS is meant to be for Windows comes with simple and rich
the technical user because you GUI options, so it is easy to use it. It
must have some exposure to can be simply used by technical as
various Linux commands. Users well as non-technical users. The
may take more time to be a handy troubleshooting process is also much
user of Linux. The troubleshooting easier than Linux.
process is also complicated as
compared to Windows.

Written in Linux is written in assembly Windows is written in C++ and


language and C. assembly language.

Reliability Linux is highly reliable and secure. Windows is not as reliable as Linux.
It has well-established system However, now Windows has
security, process management, and improved reliability but still has some
uptime. security weaknesses and system
instabilities.

Support Linux has a good support as it has Windows also provides good support
a huge community of user forums to its users. It provides free as well as
and online search. paid support. It has an easily
accessible online forum.

Update Linux provides full control to its Windows updates are annoying. The
users on updates. A user can install updates will come at any time and
the update whenever needed. Also, take too much time to install.
it takes less time to install an Sometimes, you power on your
update. machine, and updates are
automatically getting started.
Unfortunately, the user does not have
much control over updates.

Security Linux OS is more secure than Windows is less secure than Linux.
Windows. It is hard for the hackers Attackers primarily target Windows
and attackers to find a loophole in for malware and virus. Windows is
it. So, Linux is hard to break into. most vulnerable without anti-virus.

License Linux is distributed under the Windows is distributed under a


GPL(GNU General Public License) Proprietary commercial software
license. license.
Experiment 1 25 March 2020

Aim: Write a program to implement CPU scheduling for first come first serve.

Language Used: C++

Theory :

First in, first-out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready
queue. In this, the process that comes first will be executed first and the next process starts
only after the previous one gets fully executed.

Source Code :

// FCFS scheduling
#include<iostream>
using namespace std;

// Find the waiting time for all processes


void findWaitingTime(int processes[], int n, int bt[], int wt[])
{
// Waiting time for first process is 0
wt[0] = 0;

// Calculating waiting time


for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

// Calculate average time


void findavgTime( int processes[], int n, int bt[])
{
int wt[n], total_wt = 0;

//Find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//Display processes along with all details


cout << "Processes "<< " Burst time "<< " Waiting time " <<endl ;

// Calculate total waiting time


for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "<< wt[i] <<endl;
}

cout << "Average waiting time = "<< (float)total_wt / (float)n;

// Driver code
int main()
{
// Process Id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {24, 3, 3};

findavgTime(processes, n, burst_time);
return 0;
}

Output :
Viva Voce

Q1 What is CPU Utilization?

CPU utilization refers to a computer's usage of processing resources or the amount of work
handled by a CPU. Actual CPU utilization varies depending on the amount and type of
managed computing tasks. Certain tasks require heavy CPU time, while others require less
because of non- CPU resource requirements.

Q2 What are the different job scheduling in operating systems?

● First-Come-First-Serve Scheduling, FCFS. ...


● Shortest-Job-First Scheduling, SJF. ...
● Priority Scheduling. ...
● Round Robin Scheduling. ...

Q3 Which scheduling algorithm allocates the CPU first to the process that requests the
CPU first?

First Come First Serve Scheduling allocates the CPU first to the process that requests the
CPU first.

Q4 The FCFS algorithm is particularly troublesome for .

Multiprogramming Systems

Q5 Which of the following statements are true?


i) Shortest remaining time first scheduling may cause starvation
ii) Preemptive scheduling may cause starvation
iii) Round-robin is better than FCFS in terms of response time

All options are true.


Experiment 2 (a) 8 April 2020

Aim: Write a program to implement CPU scheduling for the shortest job first
(Non-preemptive).

Language Used: C++

Theory :

The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting
process with the smallest execution time to execute next. It is a non-preemptive algorithm.In
non-preemptive scheduling, once the CPU cycle is allocated to process, the process holds it
till it reaches a waiting state or terminated.

Source Code :

// SJF - Non Preemptive Scheduling


#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
cout<<"Enter number of process:";
cin>>n;

cout<<"\nEnter Burst Time:\n";


for(i=0;i<n;i++)
{
cout<<"p"<<i+1<<":";
cin>>bt[i];
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

cout<<"\nProcesst Burst Time \tWaiting Time\tTurnaround Time";


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
cout<<"\np"<<p[i]<<"\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}

avg_tat=(float)total/n;
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat<<endl;
}
Output :
Experiment 2 (b) 8 April 2020

Aim : Write a program to implement CPU scheduling for the shortest job first (Preemptive).

Language Used : C++

Theory :

In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process
with the shortest burst time begins execution. If a process with even a shorter burst time
arrives, the current process is removed or preempted from execution, and the shorter job is
allocated to the CPU cycle.

Source Code :

// SJF (SRTF) - Preemptive Scheduling


#include <iostream>
using namespace std;
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
cout<<"\nEnter the Total Number of Processes:\t";
cin>>limit;
cout<<"\nEnter Details of "<<limit<<" Processes\n";
for(i = 0; i < limit; i++)
{
cout<<"\nEnter Arrival Time:\t";
cin>>arrival_time[i];
cout<<"Enter Burst Time:\t";
cin>>burst_time[i];
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] &&
burst_time[i] > 0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
cout<<"\n\nAverage Waiting Time:\t"<<average_waiting_time<<"\n";
cout<<"Average Turnaround Time:\t"<<average_turnaround_time<<"\n";
return 0;
}

Output :
Viva Voce

Q1 What is the real difficulty with SJF in short term scheduling?


The real difficulty with SJF in short term scheduling is knowing the length of the next CPU
request

Q2 Preemptive Shortest Job First scheduling is sometimes called


SRTN scheduling – Shortest Remaining Time Next

Q3 Which scheduling algorithms give minimum average waiting time?


SJF gives a minimum average waiting time for a given set of processes.

Q4 State true or false: Scheduling is allowing a job to use the processor?


True. A scheduling system allows one process to use the CPU while another is waiting for
I/O, thereby making full use of otherwise lost CPU cycles.

Q5 What do you mean by Average turnaround time and average waiting time.
Turnaround Time: The interval from the time of submission of a process to the time of
completion is the turnaround time. Turnaround time is the sum of the periods spent waiting to
get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting Time: sum of the period spent waiting in the ready queue.
Experiment 3 (a) 15 April 2020

Aim : Write a program to perform priority scheduling (Non-preemptive).

Language Used : C++

Theory :

Priority Scheduling is a method of scheduling processes that is based on priority. In this


algorithm, the scheduler selects the tasks to work as per the priority. In this type of scheduling
method, the CPU has been allocated to a specific process. The process that keeps the CPU
busy, will release the CPU either by switching context or terminating.

Source Code :

// Priority - Non Preemptive Scheduling


#include<iostream>

using namespace std;

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:";
cin>>n;

cout<<"\nEnter Burst Time and Priority\n";


for(i=0;i<n;i++)
{
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1;
}

//sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n;
total=0;

cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}

avg_tat=total/n;
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;

return 0; }
Output :
Experiment 3 (b) 15 April 2020

Aim : Write a program to perform priority scheduling (Preemptive).

Language Used : C++

Theory :

In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the
lower priority task is still running. The lower priority task holds for some time and resumes
when the higher priority task finishes its execution.

Source Code :

// Priority - Preemptive Scheduling


#include<iostream>

using namespace std;


int main()
{
int a[10],b[10],x[10];
int waiting[10],turnaround[10],completion[10],p[10];
int i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;

cout<<"\nEnter the number of Processes: ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter arrival time of process["<<i<<"] :";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"\nEnter burst time of process["<<i<<"] :";
cin>>b[i];
}
for(i=0;i<n;i++)
{
cout<<"\nEnter priority of process["<<i<<"] :";
cin>>p[i];
}
for(i=0; i<n; i++)
x[i]=b[i];

p[9]=-1;
for(time=0; count!=n; time++)
{
smallest=9;
for(i=0; i<n; i++)
{
if(a[i]<=time && p[i]>p[smallest] && b[i]>0 )
smallest=i;
}
b[smallest]--;

if(b[smallest]==0)
{
count++;
end=time+1;
completion[smallest] = end;
waiting[smallest] = end - a[smallest] - x[smallest];
turnaround[smallest] = end - a[smallest];
}
}
cout<<"Process"<<"\t"<< "burst-time"<<"\t"<<"arrival-time" <<"\t"<<"waiting-time"
<<"\t"<<"turnaround-time"<< "\t"<<"completion-time"<<"\t"<<"Priority"<<endl;
for(i=0; i<n; i++)
{

cout<<"p"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<<"\t\t"<<turnaround[i]<<"\t
\t"<<completion[i]<<"\t\t"<<p[i]<<endl;
avg = avg + waiting[i];
tt = tt + turnaround[i];
}
cout<<"\n\nAverage waiting time ="<<avg/n;
cout<<" Average Turnaround time ="<<tt/n<<endl;
}
Output :
Viva Voice

Q1 In the priority scheduling algorithm, when a process arrives at the ready queue, its
priority is compared with the priority of which process?

In priority scheduling algorithm, when a process arrives at the ready queue, its priority is
compared with the priority of the currently running process.

Q2 What is starvation?

Starvation is the name given to the indefinite postponement of a process because it requires
some resource before it can run, but the resource, though available for allocation, is never
allocated to this process.

Q3 What is aging?

Aging is a technique of gradually increasing the priority of processes that wait in the system
for a long time.

Q4 What are the factors for assigning priority to the process?

The priority of a process depends on which algorithm we are or our system is using. Whether
it is FCFS or Shortest Job Next. If it is FCFS then the first process that arrived first would get
the higher priority than other processes and in case SJN which process has the shortest
processing time will get the higher priority than others. So deciding the priority of a process
is an algorithm dependent task, like, this only our operating system work while assigning the
priority.
Experiment 4 22 April 2020

Aim : Write a program to implement CPU scheduling for Round Robin. (Preemptive).

Language Used : C++

Theory :

A round-robin is a CPU scheduling algorithm that shares equal portions of resources in


circular orders to each process and handles all processes without prioritization. In the
round-robin, each process gets a fixed time interval of the slice to utilize the resources or
execute its task called time quantum or time slice. Some of the round-robin processes are
preempted if they are executed in a given time slot, while the rest of the processes go back to
the ready queue and wait to run in a circular order with the scheduled time slot until they
complete their task. It removes the starvation for each process to achieve CPU scheduling by
proper partitioning of the CPU.

Source Code :

// Round Robin - Preemptive Scheduling


#include<iostream>
using namespace std;

void findWaitingTime(int processes[], int n,int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}

void findTurnAroundTime(int processes[], int n,int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime(int processes[], int n, int bt[],int quantum)


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt, quantum);


findTurnAroundTime(processes, n, bt, wt, tat);

cout << "Processes "<< " Burst time " << " Waiting time " << " Turn around time\n";

for (int i=0; i<n; i++)


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "<< (float)total_tat / (float)n;
}

int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};

int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

Output :
Viva Voce

1: What do you mean by Time Slice?

The period of time for which a process is allowed to run uninterrupted in a pre-emptive
multitasking operating system. The scheduler is run once every time slice to choose the next
process to run. If the time slice is too short then the scheduler will consume too much
processing time but if it is too long then processes may not be able to respond to external
events quickly enough.

2: Time quantum is defined in which scheduling algorithm?

In the round-robin scheduling algorithm Time Quantum is defined.

3: What is the limitation of the Round Robin Scheduling Algorithm?

● If the slicing time of OS is low, the processor output will be reduced.


● This method spends more time on context switching
● Its performance heavily depends on time quantum.
● Priorities cannot be set for the processes.
● Round-robin scheduling doesn't give special priority to more important tasks.
● Decreases comprehension
● Lower time quantum results in higher context switching overhead in the system.
● Finding a correct time quantum is a quite difficult task in this system.

4: What are the factors that affect Round Robin Scheduling Algorithm?

The issue with the round-robin scheme is the length of the quantum. Setting the quantum too
short causes too many context switches and lower the CPU efficiency. On the other hand,
setting the quantum too long may cause poor response time and approximates First Come
First Served (FCFS).

5: Round-robin scheduling falls under the category of which scheduling


Non-preemptive scheduling or Preemptive scheduling?

Pre-emptive Scheduling
Experiment 5(a) 29 April 2020

Aim : Write a program to implement the first fit for memory management.

Language Used: C++

Theory :

In this scheme, we check the blocks in a sequential manner which means we pick the first
process then compare its size with the first block size if it is less than the size of the block it
is allocated otherwise we move to the second block and so on.

When the first process is allocated we move on to the next process until all processes are
allocated.

Source Code :

// First - Fit
#include<iostream>
using namespace std;

int main(){
int num_blocks,num_files,blocks[10],files[10],fragment[10],filesAllocated[10];
int i=0,j=0;
for(i=0;i<10;i++)
{
fragment[i]=1;
filesAllocated[i]=0;
}
cout<<"\nEnter the No. of Blocks: ";
cin>>num_blocks;
cout<<"\nEnter the No. of Files: ";
cin>>num_files;
cout<<"\nEnter the sizes of blocks ";
for(i=0;i<num_blocks;i++)
{
cout<<"\nBlock["<<i+1<<"]: ";
cin>>blocks[i];
}
cout<<"\nEnter the sizes of files ";
for(i=0;i<num_files;i++)
{
cout<<"\nFile["<<i+1<<"]: ";
cin>>files[i];
}
for(i=0;i<num_files;i++)
{
for(j=0;j<num_blocks;j++)
{
if(files[i]<=blocks[j])
{
blocks[j]=blocks[j]-files[i];
if(blocks[j]!=0)
{
fragment[j]++;
}
}
}
break;
}
cout<<"\nBLOCKS\tREMAIN\tFRAGMENTS";
for(i=0;i<num_blocks;i++)
cout<<endl<<i+1<<"\t"<<blocks[i]<<"\t"<<fragment[i];
cout<<"\n\nFiles which are not allocated memory \n";
for(i=0;i<num_files;i++)
{
if(filesAllocated[i]==0)
{
cout<<"\nFile "<<i+1;
}
}

}
Output :
Experiment 5(b) 29 April 2020

Aim : Write a program to implement the best fit for memory management.

Language Used : C++

Theory :

The best-fit deals with allocating the smallest free partition which meets the requirement of
the requesting process. This algorithm first searches the entire list of free partitions and
considers the smallest hole that is adequate. It then tries to find a hole that is close to the
actual process size needed.

Source Code :

// Best - Fit
#include<iostream>
using namespace std;
int main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
cout<<"\nEnter the number of blocks:";
cin>>nb;
cout<<"Enter the number of files:";
cin>>np;
cout<<"\nEnter the size of the blocks:-\n";
for(i=1;i<=nb;i++)
{
cout<<"Block no."<<i<<":";
cin>>b[i];
}
cout<<"\nEnter the size of the files :-\n";
for(i=1;i<=np;i++)
{
cout<<"File no. "<<i<<":";
cin>>p[i];
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
cout<<"\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment";
for(i=1;i<=np && parray[i]!=0;i++)
cout<<"\n"<<i<<"\t\t"<<p[i]<<"\t\t"<<parray[i]<<"\t\t"<<b[parray[i]]<<"\t\t"<<fragment[i];
return 0;
}

Output :
Experiment 5(c) 29 April 2020

Aim : Write a program to implement the worst fit for memory management.

Language Used : C++

Theory :

Worst Fit allocates a process to the partition which is largest sufficient among the freely
available partitions available in the main memory. If a large process comes at a later stage,
then memory will not have space to accommodate it. Worst fit is a strategy contrary to the
best fit that might make sense because it tends to minimize the effects of external
fragmentation.

Source Code :

// Worst - Fit
#include <iostream>
using namespace std;

int main()
{
int nBlocks,nProcess,blockSize[20],processSize[20];

cout<<" Enter the number of blocks: ";


cin>>nBlocks;

cout<<" Enter the number of files: ";


cin>>nProcess;

cout<<" Enter the size of "<<nBlocks<<" blocks: ";

for(int i=0;i<nBlocks;i++)
cin>>blockSize[i];

cout<<" Enter the size of "<<nProcess<<" files: ";

for(int i=0;i<nProcess;i++)
cin>>processSize[i];

for(int i=0;i<nProcess;i++)

{
int max = blockSize[0];
int pos = 0;
for(int j=0;j<nBlocks;j++)

if(max < blockSize[j]) { max = blockSize[j]; pos = j; } if(max >= processSize[i])


{
cout<<"\nFiles "<<i+1<<" is allocated to block "<<pos+1;
blockSize[pos] = blockSize[pos]-processSize[i];
}
else{
cout<<"\nFiles "<<i+1<<" can't be allocated";
}
}
return 0;
}

Output :
Viva Voce

Q1 Difference between Logical and Physical Address Space?

Q2 . Describe first-fit, best-fit strategies and worst fit strategies for disk space
allocation, with their merits and demerits.

First Fit Memory Allocation


This method keeps the free/busy list of jobs organized by memory location, low-ordered to
high-ordered memory. In this method, the first job claims the first available memory with
space more than or equal to its size. The operating system doesn‟t search for an appropriate
partition but just allocate the job to the nearest memory partition available with sufficient
size.

Advantages of First-Fit Memory Allocation:


It is fast in processing. As the processor allocates the nearest available memory partition to
the job, it is very fast in execution.

Disadvantages of First-Fit Memory Allocation :


It wastes a lot of memory. The processor ignores if the size of the partition allocated to the
job is very large as compared to the size of the job or not. It just allocates the memory. As a
result, a lot of memory is wasted and many jobs may not get space in the memory and would
have to wait for another job to complete.

Best-Fit Memory Allocation:


This method keeps the free/busy list in order by size – smallest to largest. In this method, the
operating system first searches the whole of the memory according to the size of the given
job and allocates it to the closest-fitting free partition in the memory, making it able to use
memory efficiently. Here the jobs are in the order from smallest job to the largest job.

Advantages of Best-Fit Allocation:


Memory Efficient. The operating system allocates the job minimum possible space in the
memory, making memory management very efficient. To save memory from getting wasted,
it is the best method.
Disadvantages of Best-Fit Allocation:

It is a Slow Process. Checking the whole memory for each job makes the working of the
operating system very slow. It takes a lot of time to complete the work.

Worst-Fit Memory Allocation :

In this allocation technique, the process traverses the whole memory and always search for
the largest hole/partition, and then the process is placed in that hole/partition. It is a slow
process because it has to traverse the entire memory to search the largest hole.

Advantages of Worst-Fit Allocation:

Since this process chooses the largest hole/partition, therefore there will be large internal
fragmentation. Now, this internal fragmentation will be quite big so that other small processes
can also be placed in that leftover partition.

Disadvantages of Worst-Fit Allocation:

It is a slow process because it traverses all the partitions in the memory and then selects the
largest partition among all the partitions, which is a time-consuming process.

Q3. What is the impact of fixed partitioning on fragmentation?

1) Internal Fragmentation
2) External Fragmentation
3) Limitation on the size of the process
4) Degree of multiprogramming is less
Experiment 6 (a) 20 May 2021

Aim: Write a program for page replacement policy using LRU

Language Used: C++

Theory :

The Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to be
replaced is least recently used. The idea is based on the locality of reference, the least
recently used page is not likely.

Source Code :

// LRU
#include <iostream>
using namespace std;
int min(int counter[],int nFrames)
{
int minimum = counter[0];
int pos = 0;
for(int i=0;i<nFrames;i++) {
if(minimum > counter[i])
{
minimum = counter[i];
pos = i;
}
}
return pos;
}
int main()
{
int pageString[50],n,frames[10],counter[10],recent = 0;
int pageFault = 0,nFrames;

cout<<"Enter the number of pages: ";


cin>>n;

cout<<"Enter the page reference string: ";


for(int i=0;i<n;i++)
cin>>pageString[i];

cout<<"\nEnter the number of frames: ";


cin>>nFrames;
for(int i=0;i<nFrames;i++)
{
frames[i] = 0;
counter[i] = 0;
}
for(int i=0;i<n;i++)
{ int flag =0;
for(int j=0;j<nFrames;j++)
{
if(frames[j] == pageString[i])
{
flag=1;
counter[j] = recent++;
break;
}
}
if(flag == 0)
{
for(int j=0;j<nFrames;j++)
{
if(frames[j] == 0)
{
frames[j] = pageString[i];
counter[j] = recent++;
flag=1;
pageFault++;
break;
}
}
}
if(flag == 0){
int PositionToreplace = min(counter,nFrames);
frames[PositionToreplace] = pageString[i];
counter[PositionToreplace] = recent++;
pageFault++;
}
cout<<endl;
for(int j=0;j<nFrames;j++)
{
cout<<frames[j]<<" ";
}
}
cout<<"\nPage Fault: "<<pageFault;
return 0;
}

Output :
Experiment 6(b) 20 May 2021

Aim : Write a program for page replacement policy using FIFO

Language Used : C++

Theory :

This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue.
When a page needs to be replaced page in the front of the queue is selected for removal.

Source Code :

// FIFO
#include <iostream>
using namespace std;
int getReplaceposition(int counter[],int n)
{
int max = counter[0];
int pos=0;
for(int i=0;i<n;i++) {
if(counter[i]>max)
{
pos=i;
max = counter[i];
}
}
return pos;
}
int main()
{
int pages[20],frames[10],counter[10];
int nPages,nFrames,pageFault=0;
cout<<"Enter the number of pages(MAX 20): ";
cin>>nPages;

cout<<"Enter the number of frames(MAX 10): ";


cin>>nFrames;

cout<<"Enter the Page reference string: ";


for(int i=0;i<nPages;i++){
cin>>pages[i];
}

for(int i=0;i<nFrames;i++)
{
frames[i] = 0;
counter[i] = 0;
}
for(int i=0;i<nPages;i++)
{
int flag =0;
for(int j=0;j<nFrames;j++)
{
if(frames[j] == pages[i])
{
flag=1;
break;
}
}
if(flag == 0)
{
pageFault++;
for(int j=0;j<nFrames;j++)
{
if(frames[j] == 0)
{
frames[j] = pages[i];
flag=1;
counter[j]++;
break;
}
}
}
if(flag == 0)
{
int pos = getReplaceposition(counter,nFrames);
frames[pos] = pages[i];
counter[pos] = 1;
for(int k=0;k<nFrames;k++)
{
if(k!=pos)
counter[k]++;
}
}
cout<<endl;
for(int j=0;j<nFrames;j++)
{
cout<<frames[j]<<" ";
}
}
cout<<"\nPage Fault: "<<pageFault;
return 0;
}

Output :
Experiment 6 (c) 20 May 2021

Aim : Write a program for page replacement policy using Optimal

Language Used : C++

Theory :

In this algorithm, OS replaces the page that will not be used for the longest period of time in
future.

Source Code :

// Optimal
#include<iostream>
using namespace std;
int main()
{
int nop,nof,page[20],i,count=0;
cout<<"\nEnter the No. of Pages:";
cin>>nop;

cout<<"\n Enter the Reference String:";


for(i=0;i<nop;i++)
{
cout<<"\t";
cin>>page[i];
}

cout<<"\n Enter the No of frames:-";


cin>>nof;

int frame[nof],fcount[nof];
for(i=0;i<nof;i++)
{
frame[i]=-1;
fcount[i]=0;
}
i=0;
while(i<nop)
{
int j=0,flag=0;
while(j<nof)
{
if(page[i]==frame[j]){
flag=1;
}
j++;
}
j=0;
cout<<"\n";
cout<<"\t"<<page[i]<<"-->";
if(flag==0)
{
if(i>=nof)
{
int max=0,k=0;
while(k<nof)
{
int dist=0,j1=i+1;
while(j1<nop)
{
if(frame[k]!=page[j1])
dist++;
else
{
break;
}
j1++;
}
fcount[k]=dist;
k++;
}
k=0;
while(k<nof-1)
{
if(fcount[max]<fcount[k+1])
max=k+1;
k++;
}
frame[max]=page[i];
}
else {
frame[i%nof]=page[i];
}
count++;
while(j<nof)
{
cout<<"\t|"<<frame[j]<<"|";
j++;
}
}
i++;
}
cout<<"\n";
cout<<"\n\tPage Fault is:"<<count;

return 0;
}

Output :
Viva Questions

1. Define Demand Paging, Page fault interrupt, and Thrashing.

• Demand Paging: Demand paging is the paging policy that a page is not read into memory
until it is requested, that is until there is a page fault on the page.
• Page fault interrupt: A page fault interrupt occurs when a memory reference is made to a
page that is not in memory. The present bit in the page table entry will be found to be off
by the virtual memory hardware and it will signal an interrupt.
• Thrashing: The problem of many page faults occurring in a short time, is called page
thrashing.

2. Under what circumstances do page faults occur? Describe the actions taken by the
operating system when a page fault occurs?

A page fault occurs when access to a page that has not been brought into the main
memory takes place. The operating system verifies the memory access, aborting the
program if it is invalid. If it is valid, a free frame is located and I/O is requested to read
the needed page into the free frame. Upon completion of I/O, the process table and page
table are updated and the instruction is restarted

3. What is the cause of thrashing? How does the system detect thrashing? Once it
detects thrashing, what can the system do to eliminate this problem?

Thrashing is caused by under allocation of the minimum number of pages required by a


process, forcing it to continuously page fault. The system can detect thrashing by
evaluating the level of CPU utilization as compared to the level of multiprogramming. It
can be eliminated by reducing the level of multiprogramming.

4. What do you mean by Belady’s anomaly?

Belady‟s anomaly is the name given to the phenomenon where increasing the number of
page frames result in an increase in the number of page faults for a given memory access
Pattern.

5. What is a page and what is a frame. How are the two related?

The page frame is the storage unit (typically 4KB in size) whereas the page is the
contents that you would store in the storage unit i.e. the page frame. For example, the
RAM is divided into fixed-size blocks called page frames which is typically 4KB in size,
and each page frame can store 4KB of data i.e. the page.
Experiment 7 27 May 2021

Aim: Write a program to implement reader/writer problem using semaphore.

Language Used: C

Theory :

Consider a situation where we have a file shared between many people.\

● If one of the people tries editing the file, no other person should be reading or
writing at the same time, otherwise, changes will not be visible to him/her.
● However, if some person is reading the file, then others may read it at the same time.
Precisely in OS, we call this situation as the readers-writers problem

Solution when Reader has the Priority over Writer


Here priority means, no reader should wait if the share is currently opened for reading.

Writer process:
1. Writer requests the entry to the critical section.
2. If allowed i.e. wait() gives a true value, it enters and performs the write. If not
allowed, it keeps on waiting.
3. It exits the critical section.

Reader process:
1. Reader requests the entry to the critical section.
2. If allowed:
● it increments the count of the number of readers inside the critical section. If
this reader is the first reader entering, it locks the wrt semaphore to
restrict the entry of writers if any reader is inside.
● It then, signals mutex as any other reader is allowed to enter while others
are already reading.
● After performing reading, it exits the critical section. When exiting, it
checks if no more reader is inside, it signals the semaphore “wrt” as now,
The writer can enter the critical section.
3. If not allowed, it keeps on waiting.

Thus, the semaphore „wrt„ is queued on both readers and writers in a manner such that
preference is given to readers if writers are also there. Thus, no reader is waiting simply
because a writer has requested to enter the critical section.
Source Code :

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>

sem_t mutex,writeblock;
int data = 0,rcount = 0;

void *reader(void *arg)


{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}

void *writer(void *arg)


{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}

main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
}

Output :
Viva - Questions

1. Define the critical section problem and explain the necessary characteristics of a
correct solution.

The critical section is a code segment where the shared variables can be accessed. An
atomic action is required in a critical section i.e. only one process can execute in
its critical section at a time. All the other processes have to wait to execute in their critical
Sections.

2. What do understand by Race Condition?

A race condition occurs when two threads access a shared variable at the same time. The
first thread reads the variable, and the second thread reads the same value from the
Variable.

3. Define semaphore and its limitations.

A semaphore is a variable or abstract data type used to control access to a common


resource by multiple processes and avoid critical section problems in a concurrent system
such as a multitasking operating system.

Some of the disadvantages of semaphores are as follows − Semaphores are complicated


so the wait and signal operations must be implemented in the correct order to prevent
deadlocks. Semaphores are impractical for last scale use as their use leads to loss of
Modularity.

4. Define monitor.

In concurrent programming (also known as parallel programming), a monitor is a


synchronization construct that allows threads to have both mutual exclusion and the
ability to wait (block) for a certain condition to become false.

5. What are classical problems of process synchronization?

Classical Problems of Synchronization


• Bounded Buffer (Producer-Consumer) Problem.
• Dining Philosophers Problem.
• The Readers Writers Problem.
Experiment 10 27 May 2021

Aim: Write a program to implement Banker‟s algorithm for deadlock avoidance.

Language Used: C++

Theory :

Banker‟s Algorithm is a resource allocation and deadlock avoidance algorithm. This


algorithm test for safety simulating the allocation for the predetermined maximum possible
amounts of all resources, then makes an “s-state” check to test for possible activities, before
deciding whether allocation should be allowed to continue.

In simple terms, it checks if the allocation of any resource will lead to deadlock or not, OR is
it safe to allocate a resource to a process and if not then the resource is not allocated to that
process. Determining a safe sequence (even if there is only 1) will assure that system will not
go into deadlock.

Banker‟s algorithm is generally used to find if a safe sequence exists or not. But here we will
determine the total number of safe sequences and print all safe sequences.

The data structure used is:


• Available vector
• Max Matrix
• Allocation Matrix
• Need Matrix

Source Code :

#include <iostream>
using namespace std;

int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

cout << "Following is the SAFE Sequence" << endl;


for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;

return (0);
}

Output :
Viva - Questions

1. What necessary conditions can lead to a deadlock situation in a system?

Deadlock in OS is a situation where two or more processes are blocked. Conditions for
Deadlock- Mutual Exclusion, Hold and Wait, No preemption, Circular wait. These 4
conditions must hold simultaneously for the occurrence of deadlock.

2. What factors determine whether a detection algorithm must be utilized in a


deadlock avoidance system?

One is that it depends on how often a deadlock is likely to occur under the implementation of
this algorithm. The other has to do with how many processes will be affected by deadlock
when this algorithm is applied.

3. What is a Safe State and its’ use in deadlock avoidance?


When a process requests an available resource, the system must decide if immediate
allocation leaves the system in a safe state. The system is in a safe state if there exists
a safe sequence of all processes. Deadlock Avoidance: ensure that a system will never
enter an unsafe state.

4. What is the resource allocation graph?

The resource allocation graph is the pictorial representation of the state of a system. As
its name suggests, the resource allocation graph is the complete information about all the
processes which are holding some resources or waiting for some resources. .... Vertices
are mainly of two types, Resource and process.

You might also like