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

Os Lab Manual Bcs303 PDF

Uploaded by

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

Os Lab Manual Bcs303 PDF

Uploaded by

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

Academic Year 2023 – 2024 (ODD Semester)

INTEGRATED LAB MANUAL

OPERATING SYSTEMS

BCS303

III Semester CS

Prepared By

Krishna V
Asst. Professor
Dept. of Data Science
Vision and Mission of the Institute
Vision
To become a leading institute for quality technical education and research with ethical values.
Mission
M1: To continually improve quality education system that produces thinking engineers having
good technical capabilities with human values.
M2: To nurture a good eco-system that encourages faculty and students to engage in meaningful
research and development.
M3: To strengthen industry institute interface for promoting team work, internship and
entrepreneurship.
M4: To enhance educational opportunities to the rural and weaker sections of the society to equip
with practical skills to face the challenges of life.

Vision and Mission of the Department


Vision
To become a leading department engaged in quality education and research in the field of computer
science and engineering.

Mission
M1: To nurture a positive environment with state of art facilities conducive for deep learning and
meaningful research and development.

M2: To enhance interaction with industry for promoting collaborative research in emerging
technologies.
M3: To strengthen the learning experiences enabling the students to become ethical professionals
with good interpersonal skills, capable of working effectively in multi-disciplinary teams.
PROGRAM EDUCATIONAL OBJECTIVES (PEOS)
PEO 1: Successful and ethical professionals in IT and ITES industries contributing to societal
progress.
PEO 2: Engaged in life-long learning, adapting to changing technological scenarios.
PEO 3: Communicate and work effectively in diverse teams and exhibit leadership qualities.

PROGRAM SPECIFIC OUTCOMES (PSOs)


PSO 1: Analyze, design, implement and test innovative application software systems to meet the
specified requirements.
PSO 2: Understand and use systems software packages.
PSO 3: Understand the organization and architecture of digital computers, embedded systems and
computer networks.
OS Integrated Lab 2024-25

Dept. of CSE, Vemana IT 1


OS Integrated Lab 2024-25

1 Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)
2 Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a) FCFS b) SJF c) Round Robin d) Priority.
3 Develop a C program to simulate producer-consumer problem using semaphores.

4 Develop a C program which demonstrates interprocess communication between a reader


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.
6 Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.

7 Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU

8 Simulate following File Organization Techniques


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.

Dept. of CSE, Vemana IT 2


OS Integrated Lab 2024-25

Program 1
// Develop a c program to implement the Process system calls (fork (), exec (), wait (), create
process, terminate process)

process.c

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

int main()
{
pid_t pid;
pid = fork();
if(pid==0)
{
printf("It is the child process and pid is %d\n",getpid());
printf("Executing add program in CHILD process\n");
execl("./add.exe","add.exe",(char *)0);
}
else if(pid > 0)
{
printf("It is the parent process and pid is %d\n",getpid());
//int status;
wait(NULL); //wait(&status);
printf("Child is reaped(cleared)\n");
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}

COMPILATION STEPS:
Step 1: Create add.c program for adding two numbers
gedit add.c
#include<stdio.h>
void main( )
{
int a,b,res;

Dept. of CSE, Vemana IT 3


OS Integrated Lab 2024-25

printf(“Enter the value of a and b\n”);


scanf(“%d%”, &a,&b);
res=a+b;
printf(“a=%d, b=%d, res=%d\n”, a,b,res);
}
Step 2: compile add.c program by creating separate exe file
cc -o add.exe add.c
Step 3: Compile 1_process.c file and run it

cc process.c
./a.out

Enter the value of a and b


10 20
a=10 b=20 res=30

Dept. of CSE, Vemana IT 4


OS Integrated Lab 2024-25

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/=n;
avtat/=n;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}

Dept. of CSE, Vemana IT 5


OS Integrated Lab 2024-25

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;

Dept. of CSE, Vemana IT 6


OS Integrated Lab 2024-25

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

Dept. of CSE, Vemana IT 7


OS Integrated Lab 2024-25

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++)
{
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|Turnaround Time|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;
}

Dept. of CSE, Vemana IT 8


OS Integrated Lab 2024-25

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

Output:

d) Priority

#include<stdio.h>

int main()
{

Dept. of CSE, Vemana IT 9


OS Integrated Lab 2024-25

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++)
{
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];
}

Dept. of CSE, Vemana IT 10


OS Integrated Lab 2024-25

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

Output:

Dept. of CSE, Vemana IT 11


OS Integrated Lab 2024-25

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

prodcons.c

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

#define BUFFER_SIZE 5

//#include <time.h> // use time.h header file to use time

sem_t mutex, full, empty;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;

//time_t t1; // declare time variable

void *producer(void *p){


int item;
// srand ( (unsigned) time (&t1)); // pass the srand() parameter

item = rand() % 50+1; // Produce a random item within 1-50 , rand() % 100+11-100

sem_wait(&empty);
sem_wait(&mutex);

// Critical Section: Add item to the buffer


buffer[in] = item;
printf("Producer %d produced item %d\n", *((int*)p),item);

Dept. of CSE, Vemana IT 12


OS Integrated Lab 2024-25

in = (in + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&full);

pthread_exit(NULL);
}

void *consumer(void *c) {


int item;
sem_wait(&full);
sem_wait(&mutex);

// Critical Section: Remove item from the buffer


item = buffer[out];
printf("Consumer %d consumed item %d \n", *((int*)c),item);
out = (out + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&empty);
pthread_exit(NULL);
}

int main() {
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);

int a[5]={1,2,3,4,5};
int i;
pthread_t p[5],c[5];

// Creating 5 producer and consumer threads


for(i=0;i<5;i++)
pthread_create(&p[i], NULL, producer, (void *)&a[i]);
for(i=0;i<5;i++)
pthread_create(&c[i], NULL, consumer, (void *)&a[i]);

// Wait for threads to finish


for(i=0;i<5;i++)
pthread_join(p[i], NULL);
for(i=0;i<5;i++)

Dept. of CSE, Vemana IT 13


OS Integrated Lab 2024-25

pthread_join(c[i], NULL);

// Destroy semaphores
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);

return 0;
}

Compilation steps:

cc prodcons.c –pthread
./a.out

OUTPUT:

Producer 1 produced item 3


Consumer 3 consumed item 3
Producer 5 produced item 6
Consumer 2 consumed item 6
Producer 4 produced item 7
Consumer 1 consumed item 7
Producer 3 produced item 5
Producer 2 produced item 3
Consumer 5 consumed item 5
Consumer 4 consumed item 3

Dept. of CSE, Vemana IT 14


OS Integrated Lab 2024-25

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.c
/*Writer Process*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include<string.h>
int main()
{
int fd;
char buf[1024],msg[100];
/* create the FIFO (named pipe) */
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
printf(“Enter the message to be write infile\n”);
gets(msg);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd, msg, sizeof(msg));
/* write the message to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}
reader.c
/* Reader Process*/

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

Dept. of CSE, Vemana IT 15


OS Integrated Lab 2024-25

#define MAX_BUF 1024


int main()
{
int fd;
/* A temp FIFO file is not created in reader */
char *myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* 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.Open two terminal, in terminal 1 compile writer.c program first( cc writer.c)
2. then run it ./a.out
3. type the what ever the message you want
4. In terminal 2 compile reader.c program ( cc -o readr.exe reader.c)
5. run it (./reader.exe)
6. you are able to see the message in another terminal

Program 5
/* 5. Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance. */

banker.c

#include<stdio.h>
#include<stdlib.h>
int p,r,i,j,max[10][10],alloc[10][10],avail[10],need[10][10];
int request[10][10],finish[10],work[10];
int safety_algo()
{
int k,count=0,flag;
for(i=0;i<p;i++)
finish[i]=0;
for(j=0;j<r;j++)
work[j]=avail[j];
while(count<p)
{
k=0;

Dept. of CSE, Vemana IT 16


OS Integrated Lab 2024-25

for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<r;j++)
if(finish[i]!=0||need[i][j]>work[j])
{
flag=1;
break;
}
if(flag!=1)
{
finish[i]=1;
for(j=0;j<r;j++)
work[j]=work[j]+alloc[i][j];
printf("p%d\t",i);
count++;
k++;
}
}
if(k==0)
{
return 0;
}
}
return 1;
}

void resource_request(int i)
{
int j;
for(j=0;j<r;j++)
{
if(request[i][j]>need[i][j])
{
printf("Error::request more than demand\n");
return;
}
}
for(j=0;j<r;j++)
{
if(request[i][j]>avail[j])
{
printf("request more than available,process has to wait\n");

Dept. of CSE, Vemana IT 17


OS Integrated Lab 2024-25

return;
}
}
for(j=0;j<r;j++)
{
alloc[i][j]=alloc[i][j]+request[i][j];
avail[j]=avail[j]-request[i][j];
need[i][j]=need[i][j]-request[i][j];
}
if(safety_algo()==1)
printf("\n SAFE::process %d can be allocated\n",i);
else
printf("UNSAFE::process shas towait\n");
}
int main()
{
printf("enter the number of processes:");
scanf("%d",&p);
printf("\n enter the number of resources:");
scanf("%d",&r);
printf("\n enter the max matrix\n");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("\n enter the alloc matrix\n");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("\n enter the available resources\n");
for(j=0;j<r;j++)
scanf("%d",&avail[j]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
printf("\tNEED\t\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d\t",need[i][j]);
printf("\n");
}
if(safety_algo()==1)
printf("\n current state of system::SAFE\n");

Dept. of CSE, Vemana IT 18


OS Integrated Lab 2024-25

else
printf("\n current state of system::UNSAFE\n");
printf("enter the new request(processid)\n");
scanf("%d",&i);
printf("\n enter the request\n");
for(j=0;j<r;j++)
scanf("%d",&request[i][j]);
resource_request(i);
return 0;
}

Execution:
[root@localhost pgms]# cc 8.c

RUN1:
[root@localhost pgms]# ./a.out
enter the number of processes:5
enter the number of resources:3
enter the max matrix
753
322
902
222
433
enter the alloc matrix
010
200
302
211
002
enter the available resources
332
NEED
743
122
600
011
431
P1 P3 P4 P0 P2
current state of system::SAFE
enter the new request(processid)
1
enter the request

Dept. of CSE, Vemana IT 19


OS Integrated Lab 2024-25

102
SAFE::process 1 can be allocated

RUN2:
[root@localhost pgms]# ./a.out
enter the number of processes:5
enter the number of resources:3
enter the max matrix
753
322
902
222
433
enter the alloc matrix
010
200
302
211
002
enter the available resources
332
NEED
743
122
600
011
431
P1 P3 P4 P0 P2
current state of system::SAFE
enter the new request(processid)
2
enter the request
102
Error::request more than demand

Dept. of CSE, Vemana IT 20


OS Integrated Lab 2024-25

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++)

Dept. of CSE, Vemana IT 21


OS Integrated Lab 2024-25

{
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;
}
}
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+1, files[m], file_arr[m], blocks[file_arr[m]],
fragments[m]);
}
printf("\n");

return 0;
}

Dept. of CSE, Vemana IT 22


OS Integrated Lab 2024-25

Output:

b) Best Fit
#include<stdio.h>

void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[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");


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++)

Dept. of CSE, Vemana IT 23


OS Integrated Lab 2024-25

{
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]);
}

c) First Fit

Dept. of CSE, Vemana IT 24


OS Integrated Lab 2024-25

#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]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1 || b[j]!=0 ) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
b[j]=temp;
goto x;
}
}
}
x: frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t",i,f[i],ff[i],frag[i]);

Dept. of CSE, Vemana IT 25


OS Integrated Lab 2024-25

}
Output:

Dept. of CSE, Vemana IT 26


OS Integrated Lab 2024-25

Program 7
/* 7.Develop a C program to simulate page replacement algorithms: a) FIFO b) LRU */

#include<stdio.h>
int n,f;
int in[100];
int p[50];
int hit=0;
int i,j,k;
int pgfaultcnt=0;

void getData()
{
printf("\n Enter length of page reference sequence:");
scanf("%d",&n);
printf("\n Enter the page reference sequence:\n");
for(i=0;i<n;i++)
scanf("%d",&in[i]);
printf("\nEnter no.of frames:");
scanf("%d",&f);
}

void initialize()
{
pgfaultcnt=0;
for(i=0;i<f;i++)
p[i]=9999;
}

int isHit(int data)


{
hit=0;
for(j=0;j<f;j++)
{
if(p[j]==data)

{
hit=1;
break;
}

Dept. of CSE, Vemana IT 27


OS Integrated Lab 2024-25

}
return hit;
}
void dispPages()
{
for(k=0;k<f;k++)
{
if(p[k]!=9999)
printf("%d ",p[k]);
}
}

void dispPgFaultCnt()
{
printf("\nTotal no. of pagefaults:%d",pgfaultcnt);
}

/*FIFO page replacement algorithm */


void fifo()
{
int j=0;
initialize();
for(i=0;i<n;i++)
{
printf("\nPage%d:",in[i]);
if(isHit(in[i])==0)
{
p[j++]=in[i];
pgfaultcnt++;
dispPages();
if(j==f)
j=0; }
else
printf("No. pagefault"); }
dispPgFaultCnt();
}

/*LRU page replacement algorithm */


void lru()
{

Dept. of CSE, Vemana IT 28


OS Integrated Lab 2024-25

initialize();
int least[50];
for(i=0;i<n;i++) {
printf("
\nPage%d:",in[i]);
if(isHit(in[i])==0) {
for(j=0;j<f;j++) {
int pg=p[j];
int found=0;
for(k=i-1;k>=0;k--)
{
if(pg==in[k]) {
least[j]=k;
found=1;
break; }
else
found=0;
}
if(!found)
least[j]=-9999;
}
int min=9999;
int repindex;
for(j=0;j<f;j++)
{
if(least[j]<min)
{
min=least[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;
dispPages();
}
else
printf("No. pagefault!");
}
dispPgFaultCnt();
}

Dept. of CSE, Vemana IT 29


OS Integrated Lab 2024-25

int main()
{
int choice;
while(1)
{
printf("\nPage Replacement Algorithms\n1.Enter data\n2.FIFO\n3.LRU\n4.Exit\nEnter your
choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
getData();
break;
case 2:
fifo();
break;
case 3:
lru();
break;
default:
return 0;
break;
}
}
}
OUTPUT
RUN 1
[root@localhost pgms]# cc 9.c
[root@localhost pgms]# ./a.out
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:5
Enter the page reference sequence:
12345

Dept. of CSE, Vemana IT 30


OS Integrated Lab 2024-25

Enter no.of frames:2


Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:2
Page1:1
Page2:1 2
Page3:3 2
Page4:3 4
Page5:5 4
Total no. of pagefaults:5
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:
3

Page1:1
Page2:1 2
Page3:3 2
Page4:3 4
Page5:5 4
Total no. of pagefaults:5
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:4

RUN 2

[root@localhost pgms]# ./a.out


Page Replacement Algorithms
1.Enter data
2.FIFO

Dept. of CSE, Vemana IT 31


OS Integrated Lab 2024-25

3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:16
Enter the page reference sequence:
0123012301234567
Enter no.of frames:3
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:16
Enter the page reference sequence:
0123012301234567
Enter no.of frames:3
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:2
Page0:0
Page1:0 1
Page2:0 1 2
Page3:3 1 2
Page0:3 0 2
Page1:3 0 1
Page2:2 0 1
Page3:2 3 1
Page0:2 3 0
Page1:1 3 0
Page2:1 2 0
Page3:1 2 3
Page4:4 2 3
Page5:4 5 3
Page6:4 5 6
Page7:7 5 6
Total no. of pagefaults:16

Dept. of CSE, Vemana IT 32


OS Integrated Lab 2024-25

Page Replacement Algorithms


1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:3
Page0:0
Page1:0 1
Page2:0 1 2
Page3:3 1 2
Page0:3 0 2
Page1:3 0 1
Page2:2 0 1
Page3:2 3 1
Page0:2 3 0
Page1:1 3 0
Page2:1 2 0
Page3:1 2 3
Page4:4 2 3
Page5:4 5 3
Page6:4 5 6
Page7:7 5 6
Total no. of pagefaults:16
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:4
RUN 3
[root@localhost pgms]# ./a.out
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:1
Enter length of page reference sequence:16
Enter the page reference sequence:
0123012301234567

Dept. of CSE, Vemana IT 33


OS Integrated Lab 2024-25

Enter no.of frames:3


Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:2
Page0:0
Page1:0 1
Page2:0 1 2
Page3:3 1 2
Page0:3 0 2
Page1:3 0 1
Page2:2 0 1
Page3:2 3 1
Page0:2 3 0
Page1:1 3 0
Page2:1 2 0
Page3:1 2 3
Page4:4 2 3
Page5:4 5 3
Page6:4 5 6
Page7:7 5 6
Total no. of pagefaults:16
Page Replacement Algorithms
1.Enter data
2.FIFO
3.LRU
4.Exit
Enter your choice:4

[root@localhost pgms]#

Dept. of CSE, Vemana IT 34


OS Integrated Lab 2024-25

Program-8
Simulate 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 -- ");

Dept. of CSE, Vemana IT 35


OS Integrated Lab 2024-25

scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
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:

Dept. of CSE, Vemana IT 36


OS Integrated Lab 2024-25

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;
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)

Dept. of CSE, Vemana IT 37


OS Integrated Lab 2024-25

{
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]);
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]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);

Dept. of CSE, Vemana IT 38


OS Integrated Lab 2024-25

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

Output:

Dept. of CSE, Vemana IT 39


OS Integrated Lab 2024-25

Program-9
Develop a C program to simulate the Linked file allocation strategies.
linkedalloc.c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{

Dept. of CSE, Vemana IT 40


OS Integrated Lab 2024-25

int f[50], p,i, st, len, j, c, k, a;

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",f[j]);
}
else
{
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("\n Do you want to enter more file(Yes - 1/No - 0):");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}
OUTPUT:
cc linkedalloc.c
./a.out

RUN 1:
Enter how many blocks already allocated: 3

Dept. of CSE, Vemana IT 41


OS Integrated Lab 2024-25

Enter blocks already allocated: 1 3 5

Enter index starting block and length: 2 3

2---->4------>6

RUN 2:

Enter how many blocks already allocated: 3

Enter blocks already allocated: 1 3 5

Enter index starting block and length: 1 3

1 starting block is already allocated

Do you want to enter more file(Yes - 1/No - 0): 0

Dept. of CSE, Vemana IT 42


OS Integrated Lab 2024-25

Program 10

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

scan.c

#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++)
{
for(j=i;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
max=d[n-1];

for(i=0;i<n;i++)
{
if(disk==d[i])

Dept. of CSE, Vemana IT 43


OS Integrated Lab 2024-25

{
dloc=i; break;
}
}
for(i=dloc;i>=0;i--)
{
printf("%d -->",d[i]);
}

printf("0 -->");
for(i=dloc+1;i<n;i++)
{
printf("%d-->",d[i]);
}
sum=disk+d[n-1];
printf("\nmovement of total cylinders %d",sum);
return 0;
}
Output:
cc scan.c
./a.out
RUN 1
Enter no of location 8
Enter position of head 53
Enter elements of disk queue
98
183
37
122
14
124
65
67
53->37->14->0->65->67->98->122->124->183->
Movement of total cylinders 236.

Dept. of CSE, Vemana IT 44


OS Integrated Lab 2024-25

RUN 2:
Enter no of location 8
Enter position of head 50
Enter elements of disk queue
90
120
35
122
38
128
65
68
50 -->38 -->35 -->0 -->65-->68-->90-->120-->122-->128-->
movement of total cylinders 178

Dept. of CSE, Vemana IT 45

You might also like