0% found this document useful (0 votes)
118 views47 pages

OS - Lab Record

This document discusses shell programming in Linux operating systems. It defines a shell as a command language interpreter that executes commands from the user. It classifies shells as either command line shells or graphical shells. It provides examples of the BASH, CSH, and KSH command line shells commonly used in Linux. It describes how shell scripts allow storing and executing sequences of commands to automate repetitive tasks. Overall, the document provides an overview of shell programming and scripts in Linux operating systems.

Uploaded by

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

OS - Lab Record

This document discusses shell programming in Linux operating systems. It defines a shell as a command language interpreter that executes commands from the user. It classifies shells as either command line shells or graphical shells. It provides examples of the BASH, CSH, and KSH command line shells commonly used in Linux. It describes how shell scripts allow storing and executing sequences of commands to automate repetitive tasks. Overall, the document provides an overview of shell programming and scripts in Linux operating systems.

Uploaded by

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

O.S.

- LAB RECORD
Submitted in partial fulfilment of the requirements for the course of
B.TeCh in
Computer Science & Engineering

BY
MD NAJEEB ANSARI
( Roll - 21BLCS010HY )

MAULANA AZAD NATIONAL URDU UNIVERSITY


(A Central University established by an Act of Parliament in 1998)
Dept of CSIT, Gachibowli, Hyderabad – 500032 (Telangana State)

( ACADEMIC YEAR : 2021 - 2022 )

I
Certificate

This is to certify that, Mr. MD NAJEEB ANSARI bearing


Enrollment No. 21BLCS010HY Studying in our University as a
student of B.Tech 2ND YEAR / 4TH Semester has completed his
OPERATING SYSTEM Laboratory Record, is Satisfactory /Good
/ Excellent during the year 2021-22.

Internal Examiner Head of the Department,


CS & IT

External Examiner

II
MAULANA AZAD NATIONAL URDU UNIVERSITY
(A Central University established by an Act of Parliament in 1998)

INDEX
S.NO PROGRamme Name PaGe NO RemaRKS

01 Basics of UNIX Commands


02 Shell Programming

03 FCFS Scheduling

04 SJF Scheduling
05 SRTF Scheduling

06 R.R Scheduling
07 Priority Scheduling

08 Contigeous File Allocation


09 Indexed File Allocation
10 Linked File Allocation

11 Banker's Algorithm

12 Resource Request Algorithm


13 FIFO Page Replacement
14 Optimal Page Replacement
15 LRU Page Replacement
16 FCFS Mass Storage Scheduling Algorithm

17 SSTF Mass Storage Scheduling Algorithm

18 SCAN Mass Storage Scheduling Algorithm

19 C-SCAN Mass Storage Scheduling Algorithm

III
Experiment No 01 -

AIM : To write Queries for Data Definition and Data Manipulation Language

DESCRIPTION :
UNIX is a powerful Operating System initially developed by Ken Thompson, Dennis
Ritchie at AT&T Bell laboratories in 1970. It is prevalent among scientific, engineering,
and academic institutions due to its most appreciative features like multitasking, flexibility,
and many more. In UNIX, the file system is a hierarchical structure of files and directories
where users can store and retrieve information using the files.

Basic Commands :
Getting help in Unix
· man – view manual pages for Unix commands
Unix Shell Commands
· clear – clear screen
· history – show history of previous commands
Time and Date commands
· date – show current date and time
· sleep – wait for a given number of seconds
· uptime – find out how long the system has been up
Unix users commands
These commands allow you to get basic information about Unix users in your environment.
· whoami – show your username
· id – print user identity
· groups – show which groups user belongs to
· passwd – change user password
· who – find out who is logged into the system
· last – show history of logins into the system
Unix file operations
Navigating filesystem and managing files and access permissions:
· ls – list files and directories
· cp – copy files (work in progress)
· rm – remove files and directories (work in progress)
· mv – rename or move files and directories to another location
· chmod – change file/directory access permissions
· chown – change file/directory ownership
Text file operations in Unix
Most of important configuration in Unix is in clear text files, these commands will let you
quickly inspect files or view logs:
· cat – concatenate files and show contents to the standard output
· more – basic pagination when viewing text files or parsing Unix commands output
· less – an improved pagination tool for viewing text files (better than more command)
· head – show the first 10 lines of text file (you can specify any number of lines)
· tail – show the last 10 lines of text file (any number can be specified)
· grep – search for patterns in text files

Department of Page No -
Unix directory management commands
Navigating filesystems and managing directories:
· cd – change directory
· pwd – confirm current directory
ln – make links and symlinks to files and directories
mkdir – make new directory
rmdir – remove directories in Unix
Unix system status commands
Most useful commands for reviewing hostname configuration and vital stats:
· hostname – show or set server hostname
· w – display system load, who’s logged in and what they are doing
· uname – print Unix system information
Reboot
· shutdown – graceful shutdown and reboot of your system
· halt – ungraceful (without stopping OS services) shutdown
· reboot – ungraceful reboot (without stopping OS services)
Networking commands in Unix
Most useful commands for inspecting network setup and exploring network connections and
ports:
· ifconfig – show and set IP addresses (found almost everywhere)
· ip – show and set IP addresses (in recent Linux versions)
· ping – check if remote host is reachable via ICMP ping
· netstat – show network stats and routing information
Process management
Listing processes and confirming their status, and stopping processes if needed:
· ps – list processes
· top – show tasks and system status
kill – kill a process (stop application running)
Remote access commands
ssh is really the only way to go, but it’s important to know telnet as well:
· telnet – clear-text (insecure) remote access protocol
· ssh – Secure SHell – encrypted remote access client
o check out the SSH reference!
File transfers commands
Always useful to know how to copy files between servers or just download some package
from the web:
· ftp – clear-text (insecure!) File Transfer Protocol client
· sftp – secure (encrypted) version of FTP
· scp – secure (encrypted) version of cp command
wget – download files from remote servers, HTTP/HTTPS and FTP

Department of Page No -
Experiment No 02 -

AIM : Shell Programming

THEORY :
A shell is special user program which provide an interface to user to use operating system
services. Shell accept human readable commands from user and convert them into
something which kernel can understand. It is a command language interpreter that execute
commands read from input devices such as keyboards or from files. The shell gets started
when the user logs in or start the terminal.
linux shell

Shell is broadly classified into two categories –


• Command Line Shell
• Graphical shell
Command Line Shell
Shell can be accessed by user using a command line interface. A special program called
Terminal in linux/macOS or Command Prompt in Windows OS is provided to type in the
human readable commands such as “cat”, “ls” etc. and then it is being execute. The result is
then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system looks like this

linux command line

In above screenshot “ls” command with “-l” option is executed.


It will list all the files in current working directory in long listing format.
Working with command line shell is bit difficult for the beginners because it’s hard to
memorize so many commands. It is very powerful, it allows user to store commands in a
file and execute them together. This way any repetitive task can be easily automated. These
files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.

Graphical Shells

Graphical shells provide means for manipulating programs based on graphical user
interface (GUI), by allowing for operations such as opening, closing, moving and resizing
windows, as well as switching focus between windows. Window OS or Ubuntu OS can be
considered as good example which provide GUI to user for interacting with program. User
do not need to type in command for every actions.A typical GUI in Ubuntu system –

GUI shell
There are several shells are available for Linux systems like –
• BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as
default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
• CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming
language.
• KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard
specifications etc.

Department of Page No -
Each shell does the same job but understand different commands and provide different built
in functions.
Shell Scripting
Usually shells are interactive that mean, they accept command as input from users and
execute them. However some time we want to execute a bunch of commands routinely, so
we have type in all commands each time in terminal.
As shell can also take commands as input from file we can write these commands in a file
and can execute them in shell to avoid this repetitive work. These files are called Shell
Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell
script is saved with .sh file extension eg. myscript.sh
A shell script have syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. it would be very easy
to get started with it.
A shell script comprises following elements –
• Shell Keywords – if, else, break etc.
• Shell commands – cd, ls, echo, pwd, touch etc.
• Functions
• Control flow – if..then..else, case and shell loops etc.
Why do we need shell scripts
There are many reasons to write shell scripts –
• To avoid repetitive work and automation
• System admins use shell scripting for routine backups
• System monitoring
• Adding new functionality to the shell etc.
Advantages of shell scripts
• The command and syntax are exactly the same as those directly entered in command line,
so programmer do not need to switch to entirely different syntax
• Writing shell scripts are much quicker
• Quick start
• Interactive debugging etc.
Disadvantages of shell scripts
• Prone to costly errors, a single mistake can change the command which might be harmful
• Slow execution speed
• Design flaws within the language syntax or implementation
• Not well suited for large and complex task
• Provide minimal data structure unlike other scripting languages. etc

Department of Page No -
Experiment No 03 -

AIM : To Write a C program to simulate the CPU Scheduling Algorithm (FCFS)

DESCRIPTION :
To calculate the average waiting time using the FCFS algorithm first the waiting time of the
first process is kept zero and the waiting time of the second process is the burst time of the
first process and the waiting time of the third process is the sum of the burst times of the
first and the second process and so on. After calculating all the waiting times the average
waiting time is calculated as the average of all the waiting times. FCFS mainly says first
come first serve the algorithm which came first will be served first.

ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time Step 4:
Set the waiting of the first process as ‗0‘and its burst time as its turnaround time
Step 5: for each process in the Ready Q calculate a).Waiting time (n) = waiting time (n-1) +
Burst time (n-1) b). Turnaround time (n)= waiting time(n)+Burst time(n)
Step 6: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process

SOURCE CODE :
#include<stdio.h>
#include<conio.h>
main(){
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}

Department of Page No -
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time --%f", wtavg/n); printf("\
nAverage Turnaround Time -- %f", tatavg/n);
}

OUTPUT :

Department of Page No -
Experiment No 04 -

AIM : To write a program to stimulate the CPU scheduling algorithm Shortest Job First (SJF)

DESCRIPTION :
To calculate the average waiting time in the shortest job first algorithm the sorting of the
process based on their burst time in ascending order then calculate the waiting time of each
process as the sum of the bursting times of all the process previous or before to that process

ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time Step 4: Start the Ready Q according the shortest Burst time by sorting according to
lowest to highest burst time
Step 5: Set the waiting time of the first process as ‗0‘ and its turnaround time as its burst
time. Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue, calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Step 9: Stop the process

SOURCE CODE :
#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];

Department of Page No -
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]); printf("\
nAverage Waiting Time-- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

OUTPUT :

Department of Page No -
Experiment No 05 -

AIM : To write a program to stimulate the CPU scheduling algorithm Shortest Remaining Time
First (SRTF)

DESCRIPTION :
SRTF, Which Stands for Shortest Remaining Time First is a scheduling algorithm used in
Operating Systems, which can also be called as the preemptive version of the SJF
scheduling algorithm. The process which has the least processing time remaining is
executed first. As it is a preemptive type of schedule, it is claimed to be better than SJF
scheduling Algorithm.

SOURCE CODE :
#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n); for(i=0;i<n;i+
+){
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++){
smallest=9;
for(i=0;i<n;i++){
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0){
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0) {
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-
at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}

Department of Page No -
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;

OUTPUT :

Department of Page No -
Experiment No 06 -

AIM : To simulate the CPU scheduling algorithm Round-Robin (RR)

DESCRIPTION :
To aim is to calculate the average waiting time. There will be a time slice, each process
should be executed within that time-slice and if not it will go to the waiting state so first
check whether the burst time is less than the time-slice. If it is less than it assign the
waiting time to the sum of the total times. If it is greater than the burst-time then subtract
the time slot from the actual burst time and increment it by time-slot and the loop continues
until all the processes are completed.

ALGORITHM :
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where No. of time slice for process
(n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
a) Waiting time for process (n) = waiting time of process(n-1)+ burst timeof process(n-1 ) +
the time difference in getting the CPU fromprocess(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)+
the time difference in getting CPU from process(n).
Step 7: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number ofprocess
Step 8: Stop the process

SOURCE CODE :
#include<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)

Department of Page No -
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++) if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is --%f ",awt/n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}

OUTPUT :

Department of Page No -
Experiment No 07 -

AIM : To write a c program to simulate the CPU scheduling Priority algorithm

DESCRIPTION :
To calculate the average waiting time in the priority algorithm, sort the burst times
according to their priorities and then calculate the average waiting time of the processes.
The waiting time of each process is obtained by summing up the burst times of all the
previous processes.

SOURCE CODE :

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++){
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
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; //waiting time for first process is zero

Department of Page No -
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}

OUTPUT :

Department of Page No -
Experiment No 08 -

AIM : Contagious File Allocation


DESCRIPTION :
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a
file requires n blocks and is given a block b as the starting location, then the blocks
assigned to the file will be: b, b+1, b+2,……b+n-1. This means that given the starting block
address and the length of the file (in terms of blocks required), we can determine the blocks
occupied by the file.
SOURCE CODE :
#include<stdio.h>
#define max 25
int main(){
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++) {
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++) {
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++) {
for(j=1;j<=nb;j++) { if(bf[j]!
=1) {
temp=b[j]-f[i];
if(temp>=0) {
ff[i]=j;
break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}

Department of Page No -
OUTPUT :

Department of Page No -
Experiment No 09 -

AIM : To implement Indexed File Allocation

DESCRIPTION :
In the chained method file allocation table contains a field which points to starting block of
memory. From it for each bloc a pointer is kept to next successive block. Hence, there is no
external fragmentation.

ALGORITHM :
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
Check whether the selected location is free .
If the location is free allocate and set flag=1 to the allocated locations. q=random(100);
if(b[q].flag==0)
b[q].flag=1;
b[q].fno=j;
r[i][j]=q;
Step 5: Print the results file no, length, Blocks allocated.
Step 6: Stop the program

SOURCE CODE :
#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main(){
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0) {
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else {
printf("Block already allocated\n");
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1) {
printf("Block already allocated");

Department of Page No -
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit(0);
}

OUTPUT :

Department of Page No -
Experiment No 10 -

AIM : To implement linked file allocation strategy.

DESCRIPTION :
In the chained method file allocation table contains a field which points to starting block of
memory. From it for each bloc a pointer is kept to next successive block. Hence, there is no
external fragmentation

ALGORITHM :
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations. While allocating
next location address to attach it to previous location for(i=0;i<n;i++)
{
for(j=0;j<s[i];j++)
{
q=random(100); if(b[q].flag==0) b[q].flag=1; b[q].fno=j; r[i]
[j]=q; if(j>0)
{
p=r[i][j-1]; b[p].next=q;}
}
}
Step 5: Print the results file no, length ,Blocks allocated.
Step 6: Stop the program

SOURCE CODE :

#include<stdio.h>
main() {
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++) {
scanf("%d",&a); f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);

Department of Page No -
k=len;
for(j=st;j<(k+st);j++) {
if(f[j]==0)
{ f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit(0);
}

OUTPUT :

Department of Page No -
Experiment No 11 -

AIM : To implement Banker’s Algorithm : Safety Algorithm

DESCRIPTION :
Deadlock is a situation where in two or more competing actions are waiting f or the other to
finish, and thus neither ever does. When a new process enters a system, it must declare the
maximum number of instances of each resource type it needed. This number may exceed
the total number of resources in the system. When the user request a set of resources, the
system must determine whether the allocation of each resources will leave the system in
safe state. If it will the resources are allocation; otherwise the process must wait until some
other process release the resources.

SOURCE CODE :
//Banker's Algorithm
#include<stdio.h>
int main(){
int n,m,i,j,k;
n=5; //No. of processes
m=3; //No. of resources
int alloc[5][3]={{0,1,0},
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2}};
int max[5][3]={{7,5,3},
{3,2,2},
{9,0,2},
{2,2,2},
{4,3,3}};
int avail[3]={3,3,2};

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;

Department of Page No -
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;
}
}
}
}
int flag =1;
for(int i=0;i<n;i++)
{ if(f[i]==0){
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1){
printf("Following is the SAFE Sequence\n");
for(i=0;i<n-1;i++)
printf("P%d -> ",ans[i]);
printf("P%d", ans[n-1]);
}
return (0);
}

OUTPUT :

Department of Page No -
Department of Page No -
Experiment No 12 -

AIM : To implement Resource Request Algorithm

SOURCE CODE :

#include<stdio.h>
#include<stdlib.h>

void print(int x[][10],int n,int m){


int i,j;
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",x[i][j]);
}
}
}

//Resource Request algorithm


void res_request(int A[10][10],int N[10][10],int AV[10][10],int pid,int m)
{
int reqmat[1][10];
int i;
printf("\n Enter additional request :- \n");
for(i=0;i<m;i++){
printf(" Request for resource %d : ",i+1);
scanf("%d",&reqmat[0][i]);
}

for(i=0;i<m;i++)
if(reqmat[0][i] > N[pid][i]){
printf("\n Error encountered.\n");
exit(0);
}

for(i=0;i<m;i++) if(reqmat[0]
[i] > AV[0][i]){
printf("\n Resources unavailable.\n");
exit(0);
}

for(i=0;i<m;i++){ AV[0]
[i]-=reqmat[0][i];
A[pid][i]+=reqmat[0][i];
N[pid][i]-=reqmat[0][i];
}

Department of Page No -
}
//Safety algorithm
int safety(int A[][10],int N[][10],int AV[1][10],int n,int m,int a[]){
int i,j,k,x=0;
int F[10],W[1][10];
int pflag=0,flag=0;
for(i=0;i<n;i++)
F[i]=0;
for(i=0;i<m;i++)
W[0][i]=AV[0]
[i];

for(k=0;k<n;k++){
for(i=0;i<n;i++){
if(F[i] == 0){
flag=0;
for(j=0;j<m;j++){
if(N[i][j] > W[0][j])
flag=1;
}
if(flag == 0 && F[i] == 0){
for(j=0;j<m;j++) W[0][j]
+=A[i][j];
F[i]=1;
pflag++;
a[x++]=i;
}
}
}
if(pflag == n)
return 1;
}
return 0;
}

//Banker's Algorithm
void accept(int A[][10],int N[][10],int M[10][10],int W[1][10],int *n,int *m){
int i,j;
printf("Enter total no. of processes : "); scanf("%d",n);
printf("Enter total no. of resources : "); scanf("%d",m);
for(i=0;i<*n;i++){
printf("\n Process %d\n",i+1);
for(j=0;j<*m;j++){

Department of Page No -
printf(" Allocation for resource %d : ",j+1); scanf("%d",&A[i]
[j]);

}
}
for(i=0;i<*n;i++){
printf(" Process %d\n",i+1);
for(j=0;j<*m;j++){
printf(" Maximum for resource %d : ",j+1); scanf("%d",&M[i]
[j]);
}
}
printf("Available resources : \n");
for(i=0;i<*m;i++){
printf(" Resource %d : ",i+1);
scanf("%d",&W[0][i]);
}
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
N[i][j]=M[i][j]-A[i]
[j];

printf("Allocation Matrix");
print(A,*n,*m);
printf("\nMaximum Requirement Matrix");
print(M,*n,*m);
printf("\nNeed Matrix");
print(N,*n,*m);

int banker(int A[][10],int N[][10],int W[1][10],int n,int m){


int j,i,a[10];
j=safety(A,N,W,n,m,a);
if(j != 0 ){
printf("\n\n");
for(i=0;i<n;i++)
printf(" P%d ",a[i]);
printf("\n A safety sequence has been detected.\n"); return
1;
}else{
printf("\n Deadlock has occured.\n");
return 0;
}
}

Department of Page No -
int main(){
int ret;
int A[10][10];
int M[10][10];
int N[10][10];
int W[1][10];
int n,m,pid,ch;
printf("DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM\n");
accept(A,N,M,W,&n,&m);
ret=banker(A,N,W,n,m);
if(ret !=0 ){
printf("\n Do you want make an additional request ? (1=Yes|0=No)");
scanf("%d",&ch);
if(ch == 1){
printf("\n Enter process no. : ");
scanf("%d",&pid);
res_request(A,N,W,pid-1,m);
ret=banker(A,N,W,n,m);
if(ret == 0 )
exit(0);
}
}else
exit(0);
return 0;
}

Department of Page No -
OUTPUT :

Department of Page No -
Experiment No 13 -

AIM : To implement FIFO Page Replacement

DESCRIPTION :
Ø The FIFO page-replacement algorithm is easy to understand and program. However, its
performance is not always good.
Ø On the one hand, the page replaced may be an initialization module that was useda long
time ago and is no longer needed.
On the other hand, it could contain a heavily used variable that was initialized early and is
in constant use

ALGORITHM :
1. Start the process
2. Read number of pages n
3. Read number of pages no
4. Read page numbers into an array a[i]
5. Initialize avail[i]=0 .to check page hit
6. Replace the page with circular queue, while re-placing check page availability in the frame
Place avail[i]=1 if page is placed in theframe Count page faults
7. Print the results.
Stop the process

SOURCE CODE :

#include<stdio.h>
#include<conio.h>
int fr[3];
void main() {
void display();
int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};
int flag1=0,flag2=0,pf=0,frsize=3,top=0;
for(i=0;i<3;i++) {
fr[i]=-1;
}
for(j=0;j<12;j++) {
flag1=0; flag2=0;
for(i=0;i<12;i++) {
if(fr[i]==page[j]) {
flag1=1; flag2=1;
break;
}
}
if(flag1==0) {
for(i=0;i<frsize;i++) {
if(fr[i]==-1) {
fr[i]=page[j];

Department of Page No -
flag2=1;
break;
}
}
}
if(flag2==0) {
fr[top]=page[j];
top++;
pf++;
if(top>=frsize)
top=0;
}
display();
}
printf("Number of page faults : %d ",pf); getch();
}
void display() {
int i;
printf("\n");
for(i=0;i<3;i++)
printf("%d\t",fr[i]);
}

OUTPUT :

Department of Page No -
Experiment No 14 -

AIM : To implement Optimal Page Replacement

DESCRIPTION :
In operating systems, whenever a new page is referred and not present in memory, page
fault occurs and Operating System replaces one of the existing pages with newly needed
page. Different page replacement algorithms suggest different ways to decide which page to
replace. The target for all algorithms is to reduce number of page faults. In this algorithm,
OS replaces the page that will not be used for the longest period of time in future

SOURCE CODE :
#include<stdio.h>
int fsize;
int frm[15];
void display();
void main()
{
int pg[100],change[15],nPage,i,j,k,l,index,pf=0,temp,flag=0,flag1=0,found,max;
printf("\n Enter frame size:");
scanf("%d",&fsize);
printf("\n Enter number of pages:");
scanf("%d",&nPage);

for(i=0;i< nPage;i++)
{
printf("\n Enter page[%d]:",i+1);
scanf("%d",&pg[i]);
}
for(i=0;i< fsize;i++)
frm[i]=-1;

printf("\n page | \t Frame content ");


printf("\n-----------------------------------------");
for(j=0;j< nPage;j++)
{
flag=0;
flag1=0;
for(i=0;i< fsize;i++)
{
if(frm[i]==pg[j])
{
flag=1;
flag1=1;
break;
}

Department of Page No -
}
if(flag==0)
{

for(i=0;i< fsize;i++)
{
if(frm[i]==-1)
{
frm[i]=pg[j]
; pf++;
flag1=1;
break;
}
}
}

if(flag1==0)
{
for(i=0;i< fsize;i++)
change[i]=0;

for(i=0;i< fsize;i++)
{
for(k=j+1;l<=nPage;k++)
{
if(frm[i]==pg[k])
{
change[i]=k-j;
break;
}
}
}
found=0;

for(i=0;i< fsize;i++)
{
if(change[i]==0)
{
index=i;
found=0;
break;
}
}
if(found==0)
{

Department of Page No -
max=change[0];
index=0;
for(i=1;i< fsize;i++)
{
if(max< change[i])
{
max=change
[i];
index=i;
}
}
}
frm[index]=pg[j];
pf++;
}
printf("\n %d |",pg[j]);
display();
}
printf("\n-------------------------------------");
printf("\n total page fault:%d",pf);
}
void display()
{
int i;
for(i=0;i< fsize;i++)
printf("\t
%d",frm[i]);
}

OUTPUT :

Department of Page No -
Experiment No 15 -

AIM : To implement LRU Page Replacement

DESCRIPTION :
In operating systems, whenever a new page is referred and not present in memory, page
fault occurs and Operating System replaces one of the existing pages with newly needed
page. Different page replacement algorithms suggest different ways to decide which page to
replace. The target for all algorithms is to reduce number of page faults. In this algorithm,
OS replaces the page that will not be used for the longest period of time in future

SOURCE CODE :
#include<stdio.h>
int main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:"); for(i=0;i<n;i+
+)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k]; printf("\n\t%d\
n",q[k]); c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i]; k+
+;
for(j=0;j<k;j++) printf("\t
%d",q[j]); printf("\n");

Department of Page No -
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r]; for(r=0;r<f;r+
+)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i]; printf("\t
%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}

Department of Page No -
OUTPUT :

Department of Page No -
Experiment No 16 -

AIM : To implement FCFS Mass Storage Scheduling Algorithm

SOURCE CODE :
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int ar[20],tm[20];
int disk;
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
tm[i]=disk-ar[i];

if(tm[i]<0)
{
tm[i]=ar[i]-disk;
}
disk=ar[i];
sum=sum+tm[i];
}
/*for(i=0;i
{
printf("\n%d",tm[i]);
} */
printf("\nmovement of total cylinders %d",sum);
return 0;
}

Department of Page No -
OUTPUT :

Department of Page No -
Experiment No 17 -

AIM : To implement SSTF Mass Storage Scheduling Algorithm

SOURCE CODE :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial); while(count!
=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
RQ[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

Department of Page No -
OUTPUT :

Department of Page No -
Experiment No 18 -

AIM : To implement SCAN Mass Storage Scheduling Algorithm

SOURCE CODE :
#include<stdio.h>
int main()
{
int i,j,sum=0,n;
int d[20];
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
printf("enter number of location\t");
scanf("%d",&n);
printf("enter position of head\t");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
d[n]=disk;
n=n+1;
for(i=0;i<n;i++) // sorting disk locations
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
max=d[n];
for(i=0;i<n;i++) // to find loc of disc in array
{
if(disk==d[i]) { dloc=i; break; }
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}
printf("0 -->");
for(i=dloc+1;i<n;i++)

Department of Page No -
{
printf("%d-->",d[i]);
}
sum=disk+max;
printf("\nmovement of total cylinders %d",sum);
return 0;
}

OUTPUT :

Department of Page No -
Experiment No 19 -

AIM : To implement C-SCAN Mass Storage Scheduling Algorithm

SOURCE CODE :
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp; temp1+
+;
}
else
{
queue2[temp2]=temp; temp2+
+;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{

Department of Page No -
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek---
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

OUTPUT :

Department of Page No -

You might also like