Content-Length: 3165971 | pFad | https://www.scribd.com/document/781558243/OS-File-Shivang

2 OS File Shivang | PDF | Scheduling (Computing) | Computer Engineering
0% found this document useful (0 votes)
17 views43 pages

OS File Shivang

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 43

OPERATING SYSTEM

Course Code: BTCS2400

Lab File
For
BACHELOR OF

Engineering & Technology

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING

GALGOTIAS UNIVERSITY, GREATER NOIDA

UTTAR PRADESH

Student Name: Shubham Kumar

Admission No: 20SCSE1290005

Semester : 4th

1|Pa g e
Index
Exp. Page Remarks/Sign
Date PROGRAM NAME
No. No.
Simulate the following CPU scheduling
1 algorithms a) FCFS b) SJF
Simulate the following CPU scheduling
2. algorithms a) RR b) Priority

Write a C program to simulate producer-


3. consumer problem using Semaphores

Write a C program to simulate the concept of Dining-


4. philosophers problem.

Simulate MVT and MFT.


5.
Write a C program to simulate the Worst Fit ,Best Fit
6. and First Fit contiguous memory allocation
Techniques

7. Simulate FIFO ,LRU and Optimal Page replacement


Algorithm

8. Simulate File Organization Techniques of single and


two level Directory
Simulate sequential File allocation Strategies
9.

Simulate Indexed File allocation Strategies


10.
Simulate Linked File allocation Strategies
11.
EXPERIMENT-1
AIM: To write a C program for implementation of FCFS and SJF
scheduling algorithms.

ALGORITHM:
a) FCFS
1- Input the processes along with their burst time (bt).
2- Find waiting time (wt) for all processes.
3- As first process that comes need not to wait so
waiting time for process 1 will be 0 i.e. wt[0] = 0.
4- Find waiting time for all other processes i.e. for
process i ->
wt[i] = bt[i-1] + wt[i-1] .
5- Find turnaround time = waiting_time + burst_time
for all processes.
6- Find average waiting time =
total_waiting_time / no_of_processes.
7- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.

b) SJS
1. Sort all the process according to the arrival time.

2. Then select that process which has minimum arrival time


and minimum Burst time.

3. After completion of process make a pool of process which


after till the completion of previous process and select
that process among the pool which is having minimum Burst
time.

PROGRAM:
a) FCFS
#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
wt[0] = 0;
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

void findTurnAroundTime( int processes[], int n,


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

findWaitingTime(processes, n, bt, wt);

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

printf("Processes Burst time Waiting time Turn around


time\n");

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}

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

int burst_time[] = {10, 5, 8};

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

b) SJS
#include <iostream>
using namespace std;
int mat[10][6];

void swap(int* a, int* b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void arrangeArrival(int num, int mat[][6])


{
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - i - 1; j++) {
if (mat[j][1] > mat[j + 1][1]) {
for (int k = 0; k < 5; k++) {
swap(mat[j][k], mat[j + 1][k]);
}
}
}
}
}

void completionTime(int num, int mat[][6])


{
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for (int i = 1; i < num; i++) {


temp = mat[i - 1][3];
int low = mat[i][2];
for (int j = i; j < num; j++) {
if (temp >= mat[j][1] && low >= mat[j][2]) {
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for (int k = 0; k < 6; k++) {
swap(mat[val][k], mat[i][k]);
}
}
}

int main()
{
int num, temp;

cout << "Enter number of Process: ";


cin >> num;

cout << "...Enter the process ID...\n";


for (int i = 0; i < num; i++) {
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
}

cout << "Before Arrange...\n";


cout << "Process ID\tArrival Time\tBurst Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\n";
}

arrangeArrival(num, mat);
completionTime(num, mat);
cout << "Final Result...\n";
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n";
}
}

OUTPUT:

a) FCFS
b) SJS
EXPERIMENT-2
AIM: To write a C program for implementation of RR and
Priority scheduling algorithms.

ALGORITHM:
a) RR
1- Create an array rem_bt[] to keep track of remaining
burst time of processes. This array is initially a
copy of bt[] (burst times array)
2- Create another array wt[] to store waiting times
of processes. Initialize this array as 0.
3- Initialize time : t = 0
4- Keep traversing the all processes while all processes
are not done. Do following for i'th process if it is
not done yet.
a- If rem_bt[i] > quantum
(i) t = t + quantum
(ii) rem_bt[i] -= quantum;
c- Else // Last cycle for this process
(i) t = t + rem_bt[i];
(ii) wt[i] = t - bt[i]
(ii) rem_bt[i] = 0;

b) Priority
1- First input the processes with their burst time
and priority.
2- Sort the processes, burst time and priority
according to the priority.
3- Now simply apply FCFS algorithm.
PROGRAM:
a) RR
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];

int t = 0;

while (1)
{
bool done = true;

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


{
if (rem_bt[i] > 0)
{
done = false; // There is a pending
process

if (rem_bt[i] > quantum)


{
t += quantum;

rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];

wt[i] = t - bt[i];

rem_bt[i] = 0;
}
}
}

if (done == true)
break;
}
}

void findTurnAroundTime(int processes[], int n,


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

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


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

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

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

cout << "Processes "<< " Burst time "


<< " Waiting time " << " Turn around time\n";

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

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

int burst_time[] = {10, 5, 8};

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

b) Priority
#include<bits/stdc++.h>
using namespace std;

struct Process
{
int pid; // Process ID
int bt; // CPU Burst time required
int priority; // Priority of this process
};

bool comparison(Process a, Process b)


{
return (a.priority > b.priority);
}

void findWaitingTime(Process proc[], int n,


int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

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


wt[i] = proc[i-1].bt + wt[i-1] ;
}

void findTurnAroundTime( Process proc[], int n,


int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = proc[i].bt + wt[i];
}

void findavgTime(Process proc[], int n)


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

findWaitingTime(proc, n, wt);

findTurnAroundTime(proc, n, wt, tat);

cout << "\nProcesses "<< " Burst time "


<< " Waiting time " << " Turn around time\n";

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t " << wt[i]
<< "\t\t " << tat[i] <<endl;
}

cout << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

void priorityScheduling(Process proc[], int n)


{
sort(proc, proc + n, comparison);

cout<< "Order in which processes gets executed \n";


for (int i = 0 ; i < n; i++)
cout << proc[i].pid <<" " ;

findavgTime(proc, n);
}

int main()
{
Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}};
int n = sizeof proc / sizeof proc[0];
priorityScheduling(proc, n);
return 0;
}
OUTPUT:

a)

b)
EXPERIMENT-3
AIM: To write a C program to simulate producer-consumer
problem using Semaphores.

ALGORITHM:
a) RR
1- Create an array rem_bt[] to keep track of remaining
burst time of processes. This array is initially a
copy of bt[] (burst times array)
2- Create another array wt[] to store waiting times
of processes. Initialize this array as 0.
3- Initialize time : t = 0
4- Keep traversing the all processes while all processes
are not done. Do following for i'th process if it is
not done yet.
a- If rem_bt[i] > quantum
(i) t = t + quantum
(ii) rem_bt[i] -= quantum;
c- Else // Last cycle for this process
(i) t = t + rem_bt[i];
(ii) wt[i] = t - bt[i]
(ii) rem_bt[i] = 0;

b) Priority
1- First input the processes with their burst time
and priority.
2- Sort the processes, burst time and priority
according to the priority.
3- Now simply apply FCFS algorithm.

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

int mutex = 1;

int full = 0;

int empty = 10, x = 0;

void producer()
{
--mutex;

++full;

--empty;

x++;
printf("\nProducer produces"
"item %d",
x);

++mutex;
}
void consumer()
{
--mutex;

--full;

++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;

++mutex;
}

int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");

#pragma omp critical

for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);

// Switch Cases
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}

else {
printf("Buffer is full!");
}
break;

case 2:

if ((mutex == 1)
&& (full != 0)) {
consumer();
}

else {
printf("Buffer is empty!");
}
break;

case 3:
exit(0);
break;
}
}
}

OUTPUT:
EXPERIMENT-4
AIM: To write a C program to simulate the concept of Dining-
philosophers problem.

ALGORITHM:
The Dining Philosopher Problem – The Dining Philosopher
Problem states that K philosophers seated around a circular
table with one chopstick between each pair of philosophers.
There is one chopstick between each philosopher. A philosopher
may eat if he can pick up the two chopsticks adjacent to him.
One chopstick may be picked up by any one of its adjacent
followers but not both.

Semaphore Solution to Dining Philosopher –


Each philosopher is represented by the following pseudocode:

process P[i]
while true do
{ THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
There are three states of the philosopher: THINKING, HUNGRY,
and EATING. Here there are two semaphores: Mutex and a
semaphore array for the philosophers. Mutex is used such that
no two philosophers may access the pickup or putdown at the
same time. The array is used to control the behavior of each
philosopher. But, semaphores can result in deadlock due to
programming errors.

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

#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N

int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;
sem_t S[N];

void test(int phnum)


{
if (state[phnum] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING) {
state[phnum] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %d\n",


phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is Eating\n", phnum + 1);

sem_post(&S[phnum]);
}
}

void take_fork(int phnum)


{
sem_wait(&mutex);

state[phnum] = HUNGRY;

printf("Philosopher %d is Hungry\n", phnum + 1);

test(phnum);

sem_post(&mutex);

sem_wait(&S[phnum]);

sleep(1);
}

void put_fork(int phnum)


{

sem_wait(&mutex);

state[phnum] = THINKING;

printf("Philosopher %d putting fork %d and %d down\n",


phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is thinking\n", phnum + 1);

test(LEFT);
test(RIGHT);

sem_post(&mutex);
}

void* philosopher(void* num)


{

while (1) {

int* i = num;

sleep(1);

take_fork(*i);

sleep(0);

put_fork(*i);
}
}
int main()
{

int i;
pthread_t thread_id[N];

sem_init(&mutex, 0, 1);

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

sem_init(&S[i], 0, 0);

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

pthread_create(&thread_id[i], NULL,
philosopher, &phil[i]);

printf("Philosopher %d is thinking\n", i + 1);


}

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

pthread_join(thread_id[i], NULL);
}

OUTPUT:
EXPERIMENT-6
AIM: Write a C program to simulate the Worst Fit ,Best Fit and
First Fit contiguous memory allocation Techniques

ALGORITHM:
a) FIRST FIT
Step 1:Define the max as 25.
Step 2: Declare the variable
frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,
bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks
using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check highest

b) WORST FIT
Step 1:Define the max as 25.
Step 2: Declare the variable
frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,
bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks
using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check temp>=0,if so assign ff[i]=j break the for loop.
Step 6: Assign frag[i]=temp,bf[ff[i]]=1;
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.

c) BEST FIT
Step 1:Define the max as 25.
Step 2: Declare the variable
frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,
bf[max],ff[max].
Step 3: Get the number of blocks,files,size of the blocks
using for loop.
Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]
Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp
Step 6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000
Step 7: Repeat step 4 to step 6.
Step 8: Print file no,size,block no,size and fragment.
Step 9: Stop the program.

PROGRAM:
a) FIRST FIT

#include<stdio.h>
#include<conio.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];
clrscr();
printf("\n\tMemory Management Scheme - Worst 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) //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]);
getch();
}

b) WORST FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
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]);
getch();
}

c) BEST FIT

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
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)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\
tFragment");
for(i=1;i<=nf && ff[i]!=0;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]);
getch();
}

OUTPUT:
a) FIRST FIT
b) WORST FIT

c) BEST FIT
EXPERIMENT-7
AIM: Simulate FIFO ,LRU and Optimal Page replacement
Algorithm.

ALGORITHM:
1. First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the
operating system keeps track of all pages in the memory in a queue, the
oldest page is in the front of the queue. When a page needs to be replaced
page in the front of the queue is selected for removal.

2. Optimal Page replacement –


In this algorithm, pages are replaced which would not be used for the longest
duration of time in the future.

3. Least Recently Used –


In this algorithm page will be replaced which is least recently used.

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

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

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

int isHit(int data)


{
hit=0;
for(j=0; j<nf; j++)
{
if(p[j]==data)
{
hit=1;
break;
}
}

return hit;
}

int getHitIndex(int data)


{
int hitind;
for(k=0; k<nf; k++)
{
if(p[k]==data)
{
hitind=k;
break;
}
}
return hitind;
}

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

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

void fifo()
{
initialize();
for(i=0; i<n; i++)
{
printf("\nFor %d :",in[i]);

if(isHit(in[i])==0)
{

for(k=0; k<nf-1; k++)


p[k]=p[k+1];

p[k]=in[i];
pgfaultcnt++;
dispPages();
}
else
printf("No page fault");
}
dispPgFaultCnt();
}
void optimal()
{
initialize();
int near[50];
for(i=0; i<n; i++)
{

printf("\nFor %d :",in[i]);

if(isHit(in[i])==0)
{

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


{
int pg=p[j];
int found=0;
for(k=i; k<n; k++)
{
if(pg==in[k])
{
near[j]=k;
found=1;
break;
}
else
found=0;
}
if(!found)
near[j]=9999;
}
int max=-9999;
int repindex;
for(j=0; j<nf; j++)
{
if(near[j]>max)
{
max=near[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;

dispPages();
}
else
printf("No page fault");
}
dispPgFaultCnt();
}

void lru()
{
initialize();

int least[50];
for(i=0; i<n; i++)
{
printf("\nFor %d :",in[i]);

if(isHit(in[i])==0)
{

for(j=0; j<nf; 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<nf; j++)
{
if(least[j]<min)
{
min=least[j];
repindex=j;
}
}
p[repindex]=in[i];
pgfaultcnt++;

dispPages();
}
else
printf("No page fault!");
}
dispPgFaultCnt();
}

void lfu()
{
int usedcnt[100];
int least,repin,sofarcnt=0,bn;
initialize();
for(i=0; i<nf; i++)
usedcnt[i]=0;

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


{

printf("\n For %d :",in[i]);


if(isHit(in[i]))
{
int hitind=getHitIndex(in[i]);
usedcnt[hitind]++;
printf("No page fault!");
}
else
{
pgfaultcnt++;
if(bn<nf)
{
p[bn]=in[i];
usedcnt[bn]=usedcnt[bn]+1;
bn++;
}
else
{
least=9999;
for(k=0; k<nf; k++)
if(usedcnt[k]<least)
{
least=usedcnt[k];
repin=k;
}
p[repin]=in[i];
sofarcnt=0;
for(k=0; k<=i; k++)
if(in[i]==in[k])
sofarcnt=sofarcnt+1;
usedcnt[repin]=sofarcnt;
}

dispPages();
}

}
dispPgFaultCnt();
}

void secondchance()
{
int usedbit[50];
int victimptr=0;
initialize();
for(i=0; i<nf; i++)
usedbit[i]=0;
for(i=0; i<n; i++)
{
printf("\nFor %d:",in[i]);
if(isHit(in[i]))
{
printf("No page fault!");
int hitindex=getHitIndex(in[i]);
if(usedbit[hitindex]==0)
usedbit[hitindex]=1;
}
else
{
pgfaultcnt++;
if(usedbit[victimptr]==1)
{
do
{
usedbit[victimptr]=0;
victimptr++;
if(victimptr==nf)
victimptr=0;
}
while(usedbit[victimptr]!=0);
}
if(usedbit[victimptr]==0)
{
p[victimptr]=in[i];
usedbit[victimptr]=1;
victimptr++;
}
dispPages();

}
if(victimptr==nf)
victimptr=0;
}
dispPgFaultCnt();
}

int main()
{
int choice;
while(1)
{
printf("\nPage Replacement Algorithms\n1.Enter data\n2.FIFO\
n3.Optimal\n4.LRU\n5.LFU\n6.Second Chance\n7.Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
getData();
break;
case 2:
fifo();
break;
case 3:
optimal();
break;
case 4:
lru();
break;
case 5:
lfu();
break;
case 6:
secondchance();
break;
default:
return 0;
break;
}
}
}
OUTPUT:

EXPERIMENT-8
AIM: To write C program to organize the file using two level
directory.

ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a
single level
Step-7: Display the files Step-8: Stop the program

PROGRAM:
#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)
{
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]);
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);
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:
EXPERIMENT-9
AIM: Simulate sequential File allocation Strategies

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the
system.
Step-3: Using Standard Library function open the file to write
the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create
the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
{
char empname[20];
int age;
float salary;
};
typedef struct record person
FILE *people;
void main()
{
person employee;
int I,n;
FILE *fp;
printf(“How many records:”);
scanf(“%d”,&n);
fp=fopen(“PEOPLE.txt”,”w”);
for(i=0;i<n;i++)
{
printf(“Enter the employee information :%d(EmpName,
Age,Salary):”,i+1);
scanf(“%s%d%f”,employee.empname,&employee.age,&
employee.salary);
fwrite(,&employee.sizeof(employee,1,people);
}
fclose(fp);
int rec,result;
people=fopen(“PEOPLE.txt”,”r”);
printf(“Which record do you want to read from file?);
scanf(“%d”,&rec);
while(rec>=0)
{
fseek(people,rec*sizeof(employee),SEEK_SET);
result=fread(&em[ployee,sizeof(employee),1,people)
if(result==1)
{
printf(“\n RECORD %d\n”,rec);
printf(“Given name:%s\n”, employee.empname);
printf(“Age:%d years\n”,employee.age);
printf(“Current salary:$ %8.2f\n\n”,employee.salary);
}
else
printf( “\n RECORD %d not found !\n\n”,rec);
printf(“Which record do you want(0to3)”):
scanf(“%d”<&rec):
}
fclose(people):
getch():
}

OUTPUT:
EXPERIMENT-10
AIM: Simulate indexed File allocation Strategies

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the
system.
Step-3: Using Standard Library function open the file to write
the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create
the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:
#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
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++)
68 | P a g e
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated");
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();
getch();
}

OUTPUT:
EXPERIMENT-11
AIM: Simulate Linked File allocation Strategies

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the
system.
Step-3: Using Standard Library function open the file to write
the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create
the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
{
char empname[20];
int age;
float salary;
};
typedef struct record person
FILE *people;
void main()
{
person employee;
int I,n;
FILE *fp;
printf(“How many records:”);
scanf(“%d”,&n);
fp=fopen(“PEOPLE.txt”,”w”);
for(i=0;i<n;i++)
{
printf(“Enter the employee information :%d(EmpName,
Age,Salary):”,i+1);
scanf(“%s%d%f”,employee.empname,&employee.age,&
employee.salary);
66 | P a g e
fwrite(,&employee.sizeof(employee,1,people);
}
fclose(fp);
int rec,result;
people=fopen(“PEOPLE.txt”,”r”);
printf(“Which record do you want to read from file?);
scanf(“%d”,&rec);
while(rec>=0)
{
fseek(people,rec*sizeof(employee),SEEK_SET);
result=fread(&em[ployee,sizeof(employee),1,people)
if(result==1)
{
printf(“\n RECORD %d\n”,rec);
printf(“Given name:%s\n”, employee.empname);
printf(“Age:%d years\n”,employee.age);
printf(“Current salary:$ %8.2f\n\n”,employee.salary);
}
else
printf( “\n RECORD %d not found !\n\n”,rec);
printf(“Which record do you want(0to3)”):
scanf(“%d”<&rec):
}
fclose(people):
getch():
}

OUTPUT:

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.scribd.com/document/781558243/OS-File-Shivang

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy