0% found this document useful (0 votes)
27 views

Operating System Lab Manual

Uploaded by

shaikhumaiya841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Operating System Lab Manual

Uploaded by

shaikhumaiya841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

YENEPOYA INSTITUTE OF TECHNOLOGY

(Affiliated to Visvesvaraya Technological University, Belagavi) N.H. 13, Thodar,


Moodbidri, Mangaluru 574225 (D.K), Karnataka

DEPARTMENT OF
ARTIFICIAL INTELLIGENCE & MACHINE LEARNING

LAB MANUAL

Operating Systems
(BCS303)

[As per Choice Based Credit System (CBCS) scheme]


ACADEMIC YEAR 2023-2024
Operating Systems – BCS303

Yenepoya Institute of Technology, Moodbidri

Vision
We are committed to creating new milestones and standards for students to experience an
unparalleled educational journey that is intellectually, socially and personally transformative.

Mission
YIT will endeavor to educate and transform the student community by instilling in them pride in
their gifts and talents, nurturing them and guiding them in how best to utilize it for human welfare
and progress..

Department of Artificial Intelligence & Machine Learning

Vision
To be a centre of excellence to produce engineers in the field of artificial intelligence & Machine
Learning who are committed, innovation driven, self - determined, and capable of developing
applications for the improvement of society
Mission
 To expedite the students to various knowledge platforms through expertized academic
guidance and supervision.
 To inspire the students to be innovative, skilled engineers by imparting the leadership
qualities, team-spirit, and ethical responsibilities.
 To encourage faculty and students to get involved in lifelong learning, research oriented and
significantly contributing to the advancement of the civilization.

Program Educational Objectives

Artificial intelligence & Machine learning graduates will be able to


 Demonstrate technical Competence in Artificial intelligence & Machine learning and their
applications
 Design solution for problems in different domains like medical, data science by applying
Artificial intelligence & Machine learning Concepts.
 Take up Masters and Research Programs in the area of Artificial intelligence & Machine
learning.

Program Specific Outcomes


Artificial intelligence & Machine learning graduates will be able to
 Apply knowledge of Artificial intelligence & Machine learning and formulate solutions for
interdisciplinary problems
 Develop models in Data Science,Machine Learning and Big Data technologies and integrate
into real time applications
Department of AI&ML, YIT, Moodbidri 2
Operating Systems – BCS303

Sl.
Experiments
No
Develop a c program to implement the Process system calls(fork(),exec(),wait(), create process,
1
terminate process)

Simulate the following CPU scheduling algorithms to find turnaround time and waitingtime
2
a)FCFS b)SJF c)Round Robin d)Priority.

3 Develop a C program to simulate producer-consumer problem using semaphores.


Develop a C program which demonstrates inter process communication between a reader
4
process and a writer process. Use mkfifo, open, read, write and close APIs in your program.
5 Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
Develop a C program to simulate the following contiguous memory allocation Techniques:
6
a) Worst fit b)Best fit c) Firstfit.
Develop a C program to simulate page replacement algorithms:
7
a) FIFO b) LRU
Simulate the following File Organization Techniques
8
a)Single level directory b)Two level directory
9 Develop a C program to simulate the Linked file allocation strategies.

10 Develop a C program to simulate SCAN disk scheduling algorithm.


Course outcomes (Course Skill Set):
At the end of the course the student will be able to:
CO1.Explain the structure and functionality of operating system
CO2. Apply appropriate CPU scheduling algorithms for the given problem.
CO3.Analyse the various techniques for process synchronization and deadlock handling.
CO4. Apply the various techniques for memory management
CO5. Explain file and secondary storage management strategies.
CO6. Describe the need for information protection mechanisms

Assessment Details (both CIE and SEE)

The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is
50%. The minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and
for the SEE minimum passing mark is 35% of the maximum marks (18 out of 50 marks). A student
shall be deemed to have satisfied the academic requirements and earned the credits allotted to each
subject/ course if the student secures a minimum of 40% (40 marks out of 100) in the sum total of the
CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken together.

CIE for the theory component of the IPCC (maximum marks 50)

● IPCC means practical portion integrated with the theory of the ourse.
● CIE marks for the theory component are 25 marks and that for the practical component is 25 marks.
Department of AI&ML, YIT, Moodbidri 3
Operating Systems – BCS303
● 25 marks for the theory component are split into 15 marks for two Internal Assessment Tests (Two
Tests, each of 15 Marks with 01-hour duration, are to be conducted) and 10 marks for other assessment
methods mentioned in 22OB4.2. The first test at the end of 40-50% coverage of the syllabus and the
second test after covering 85-90% of the syllabus.

● Scaled-down marks of the sum of two tests and other assessment methods will be CIE marks for the
theory component of IPCC (that is for 25 marks).

● The student has to secure 40% of 25 marks to qualify in the CIE of the theory component of IPCC.
CIE for the practical component of the IPCC

● 15 marks for the conduction of the experiment and preparation of laboratory record, and 10 marks
for the test to be conducted after the completion of all the laboratory sessions.

● On completion of every experiment/program in the laboratory, the students shall be evaluated


including viva-voce and marks shall be awarded on the same day.

● The CIE marks awarded in the case of the Practical component shall be based on the continuous
evaluation of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all
experiments, write-ups are added and scaled down to 15 marks.

● The laboratory test (duration 02/03 hours) after completion of all the experiments shall be conducted
for 50 marks and scaled down to 10 marks.

● Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory
component of IPCC for 25 marks.

● The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of the
IPCC. SEE for IPCC Theory SEE will be conducted by University as per the scheduled timetable, with
common question papers for the course (duration 03 hours)

1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a
maximum of 3 sub-questions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module.
4. Marks scored by the student shall be proportionally scaled down to 50 Marks. The theory portion of
the IPCC shall be for both CIE and SEE, whereas the practical portion will have a CIE component
only. Questions mentioned in the SEE paper may include questions from the practical component.

Department of AI&ML, YIT, Moodbidri 4


Operating Systems – BCS303
Suggested Learning Resources:

Textbooks

1. Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, Operating System Principles 8th edition,
Wiley-India, 2015

Reference Books

1. Ann McHoes Ida M Fylnn, Understanding Operating System, Cengage Learning, 6th Edition
2. D.M Dhamdhere, Operating Systems: A Concept Based Approach 3rd Ed, McGraw- Hill, 2013.
3. P.C.P. Bhatt, An Introduction to Operating Systems: Concepts and Practice 4th Edition, PHI(EEE),
2014.
4. William Stallings Operating Systems: Internals and Design Principles, 6th Edition, Pearson.

Web links and Video Lectures (e-Resources):

1. https://youtu.be/mXw9ruZaxzQ
2. https://youtu.be/vBURTt97EkA
3.https://www.youtube.com/watch?
v=783KABtuE4&list=PLIemF3uozcAKTgsCIj82voMK3TMR0YE_f
4. https://www.youtube.com/watch?v=3-
ITLMMeeXY&list=PL3pGy4HtqwD0n7bQfHjPnsWzkeRn6mkO

Activity Based Learning (Suggested Activities in Class)/ Practical Based learning

● Assessment Methods

o Case Study on Unix Based Systems (10 Marks)


o Lab Assessment (25 Marks)

Department of AI&ML, YIT, Moodbidri 5


Operating Systems – BCS303
Program-1

Develop a C program to implement the Process system calls


(fork(),exec(),wait(), create process, terminate process)

#include<stdio.h> // printf()
#include<stdlib.h> // exit()
#include<sys/types.h> // pid_t
#include<sys/wait.h> // wait()
#include<unistd.h> // fork

int main(int argc, char **argv)


{
pid_tpid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
int i=0;
for(i=0;i<8;i++)
{
printf("%d\n",i);
}
exit(0);
}
else if(pid> 0)
{
printf("It is the parent process and pid is %d\n",getpid());
int status;
wait(&status);
printf("Child is reaped\n");
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}

Department of AI&ML, YIT, Moodbidri 6


Operating Systems – BCS303

Output:

Department of AI&ML, YIT, Moodbidri 7


Operating Systems – BCS303
Program-2

Simulate the following CPU scheduling algorithms to find turnaround time


and waiting time

a) FCFS
#include<stdio.h>
int main()
{
int n, bt[20], wt[20], tat[20], avwt=0, avtat=0,i,j;
printf("Enter total number of processes(maximum 20): ");
scanf("%d",&n);
printf("\nEnter Process Burst Time:\n");
for(i=0;i<n;i++)
{
printf("P[%d]: ",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");


//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}

Department of AI&ML, YIT, Moodbidri 8


Operating Systems – BCS303

Output:

b) SJF

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process: ");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d: ",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
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++)

Department of AI&ML, YIT, Moodbidri 9


Operating Systems – BCS303
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{ tat[i]=bt[i]+wt[i];
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=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
return 0;
}

Output:

c) Round Robin
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{

Department of AI&ML, YIT, Moodbidri 10


Operating Systems – BCS303
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|TurnaroundTime|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum&&rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)

{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}

Department of AI&ML, YIT, Moodbidri 11


Operating Systems – BCS303
Output:

d) Priority

#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;
}

for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
Department of AI&ML, YIT, Moodbidri 12
Operating Systems – BCS303
{
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;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


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

tat[i]=bt[i]+wt[i];
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;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Department of AI&ML, YIT, Moodbidri 13


Operating Systems – BCS303

Output:

Department of AI&ML, YIT, Moodbidri 14


Operating Systems – BCS303
Program-3

Develop a C program to simulate producer-consumer problem using


semaphores.

#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf("\n 1. Produce \t 2. Consume \t3. Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: if((in+1)%bufsize==out)
printf("\n Buffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}}}

Department of AI&ML, YIT, Moodbidri 15


Operating Systems – BCS303

Output:

Department of AI&ML, YIT, Moodbidri 16


Operating Systems – BCS303
Program-4

Develop a C program which demonstrates inter-process communication between


a reader process and a writer process. Use mkfifo, open, read, write and close
APIs in your program.

/*Writer Process*/

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
intfd;
char buf[1024];
/* create the FIFO (named pipe) */
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));
/* write "Hi" to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}

/* Reader Process*/

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#define MAX_BUF 1024
int main()
{
intfd;
/* A temp FIFO file is not created in reader */
char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
Department of AI&ML, YIT, Moodbidri 17
Operating Systems – BCS303
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Writer: %s\n", buf);
close(fd);
return 0;
}

Instructions for Execution:

1. Run Reader Process


2. Then Run Writer Process

Department of AI&ML, YIT, Moodbidri 18


Operating Systems – BCS303
Program-5

Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;

printf("Enter the no of processes : ");


scanf("%d", &p);

for(i = 0; i< p; i++)


completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i< p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i< p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources : ");


for(i = 0; i< r; i++)
scanf("%d", &avail[i]);

for(i = 0; i< p; i++)

for(j = 0; j < r; j++)


need[i][j] = Max[i][j] - alloc[i][j];

do

Department of AI&ML, YIT, Moodbidri 19


Operating Systems – BCS303
{
printf("\n Max matrix:\tAllocation matrix:\n");

for(i = 0; i< p; i++)


{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");

for( j = 0; j < r; j++)

printf("%d ", alloc[i][j]);


printf("\n");
}

process = -1;

for(i = 0; i< p; i++)


{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}

Department of AI&ML, YIT, Moodbidri 20


Operating Systems – BCS303
}
while(count != p && process != -1);

if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i< p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");

Output:

Department of AI&ML, YIT, Moodbidri 21


Operating Systems – BCS303

Department of AI&ML, YIT, Moodbidri 22


Operating Systems – BCS303
Program-6

Develop a C program to simulate the following contiguous memory allocation


Techniques
a) Worst fit
#include<stdio.h>
int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top = 0;
static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\t");
scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m <number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m <number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m <number_of_files; m++)
{
for(n = 0; n <number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] - files[m];
if(temp >= 0)
{
if(top < temp)
{
file_arr[m] = n;
top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;

Department of AI&ML, YIT, Moodbidri 23


Operating Systems – BCS303
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
for(m = 0; m <number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]);
}
printf("\n");

return 0;
}
Output:

b) Best Fit
#include<stdio.h>

void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static intbarray[20],parray[20];

printf("\n\t\t\tMemory Management Scheme - Best Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);

printf("\nEnter the size of the blocks:-\n");


Department of AI&ML, YIT, Moodbidri 24
Operating Systems – BCS303
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}

printf("\nEnter the size of the processes :-\n");


for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&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;
}

printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np &&parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

Department of AI&ML, YIT, Moodbidri 25


Operating Systems – BCS303

c) First Fit

#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
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]);
Department of AI&ML, YIT, Moodbidri 26
Operating Systems – BCS303
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
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]);
}
Output:

Department of AI&ML, YIT, Moodbidri 27


Operating Systems – BCS303
Program-7

Develop a C program to simulate page replacement algorithms

a) FIFO

#include<stdio.h>
int main()
{
inti,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

Department of AI&ML, YIT, Moodbidri 28


Operating Systems – BCS303
Output:

b) LRU

#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n………………………………..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
} for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
Department of AI&ML, YIT, Moodbidri 29
Operating Systems – BCS303
for(i=0;i<nor;i++)
{
flag=0;

printf("\n\t Reference NO %d->\t",ref[i]);

for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
}
intlruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;

Department of AI&ML, YIT, Moodbidri 30


Operating Systems – BCS303
}

Output:

Department of AI&ML, YIT, Moodbidri 31


Operating Systems – BCS303
Program-8

Simulate the following File Organization Techniques


a) Single level directory

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break; } }
if(i==dir.fcnt) printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
Department of AI&ML, YIT, Moodbidri 32
Operating Systems – BCS303
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)

printf("File %s not found",f);


break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

Output:

Department of AI&ML, YIT, Moodbidri 33


Operating Systems – BCS303

b) Two-Level Directory

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

struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
Department of AI&ML, YIT, Moodbidri 34
Operating Systems – BCS303
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;

strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
gotojmp;
}
}
printf("File %s not found",f);

Department of AI&ML, YIT, Moodbidri 35


Operating Systems – BCS303
gotojmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

Department of AI&ML, YIT, Moodbidri 36


Operating Systems – BCS303

Output:

Department of AI&ML, YIT, Moodbidri 37


Operating Systems – BCS303
Program-9

Develop a C program to simulate the Linked file allocation strategies.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
Department of AI&ML, YIT, Moodbidri 38
Operating Systems – BCS303
getch();
}
Output:

Department of AI&ML, YIT, Moodbidri 39


Operating Systems – BCS303
Program-10

Develop a C program to simulate SCAN disk scheduling algorithm.


#include <stdio.h>
int request[50];
int SIZE;
int pre;
int head;
intuptrack;
intdowntrack;
struct max{
int up;
int down;
} kate[50];
intdist(int a, int b){
if (a > b)
return a - b;
return b - a;
}
void sort(int n){
inti, j;
for (i = 0; i< n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (request[j] > request[j + 1]){
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}
j = 0;
i = 0;
while (request[i] != head){
kate[j].down = request[i];
j++;
i++;
}
downtrack = j;
i++;
j = 0;
while (i< n){
kate[j].up = request[i];
j++;
i++;
}
uptrack = j;
}
Department of AI&ML, YIT, Moodbidri 40
Operating Systems – BCS303
void scan(int n){
inti;
intseekcount = 0;
printf("SEEK SEQUENCE = ");
sort(n);
if (pre < head){

for (i = 0; i<uptrack; i++){

printf("%d ", head);


seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
for (i = downtrack - 1; i> 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
}
else{
for (i = downtrack - 1; i>= 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
for (i = 0; i<uptrack - 1; i++){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
}
printf(" %d\nTOTAL DISTANCE :%d", head, seekcount);
}
int main(){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d", &SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d", &n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for (i = 0; i< n; i++)
scanf("%d", &request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d", &head);
request[n] = head;
request[n + 1] = SIZE - 1;
request[n + 2] = 0;
printf("ENTER THE PRE REQUEST :\n");

Department of AI&ML, YIT, Moodbidri 41


Operating Systems – BCS303
scanf("%d", &pre);
scan(n + 3);
}

Output:

Department of AI&ML, YIT, Moodbidri 42


Operating Systems – BCS303

Department of AI&ML, YIT, Moodbidri 43

You might also like