0% found this document useful (0 votes)
51 views30 pages

18BCE2429 Os Last

The document discusses three file allocation methods - contiguous, linked, and indexed allocation. It provides the algorithms for each method and includes sample C code to implement the algorithms. The code takes input on the number of files and blocks and outputs file details like name, starting block, length, and occupied blocks.

Uploaded by

Latera Gonfa
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)
51 views30 pages

18BCE2429 Os Last

The document discusses three file allocation methods - contiguous, linked, and indexed allocation. It provides the algorithms for each method and includes sample C code to implement the algorithms. The code takes input on the number of files and blocks and outputs file details like name, starting block, length, and occupied blocks.

Uploaded by

Latera Gonfa
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/ 30

OPERATING SYSTEM

SLOT:L47+L48
NAME SHAMBEL GONFA
REGGISTER NUMBER 18BCE2429
SUBMITTED TO:
PROF. JAGALIGAM
CONTIGOUS ALLOCATION
AIM: Write a C Program to implement contigous File Allocation method.
ALGORITHM:
Step 1: Start the program.

Step 2: Get the number of memory partition and their sizes.

Step 3: Get the number of processes and values of block size for each process.

Step 4: First fit algorithm searches all the entire memory block until a hole which is

big enough is encountered. It allocates that memory block for the requesting

process.

Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can

be allocated to requesting process and allocates if.

Step 6: Worst fit algorithm searches the memory blocks for the largest hole and

allocates it to the process.

Step 7: Analyses all the three memory management techniques and display the best

algorithm which utilizes the memory resources effectively and efficiently.

Step 8: Stop the program.


PROGRAM

#include<stdio.h>

#include<conio.h>

main()

int n,i,j,b[20],sb[20],x,t[20],c[20][20];

printf("ENTER NO. OF FILES ");

scanf("%d",&n);

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

printf("ENTER BLOCKS TAKEN BY FILE%d ",i+1);

scanf("%d",&b[i]);

printf("ENTER STARTING BLOCK OF FILE%d ",i+1);

scanf("%d",&sb[i]);

t[i]=sb[i];

for(j=0;j<b[i];j++)

c[i][j]=sb[i]++;

printf("FILE\tSTART\tLENGTH\n");

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

printf("%d\t %d \t%d\n",i+1,t[i],b[i]);

while(1)

printf("ENTER THE FILE NAME ");


scanf("%d",&x);

printf("FILE NAME:%d\n",x);

printf("LENGTH:%d\n",b[x-1]);

printf("BLOCKS OCCUPIED:");

for(i=0;i<b[x-1];i++)

printf("%4d\n",c[x-1][i]);

}
LINKED FILE ALLOCATION
AIM: Write a C Program to implement Linked File Allocation method.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory

Step 2: When the page is required replace the page at the head of the queue

Step 3: Now the new page is inserted at the tail of the queue

Step 4: Create a stack

Step 5: When the page fault occurs replace page present at the bottom of the stack

Step 6: Stop the allocation

PROGRAM
#include<stdio.h>

#include<conio.h>

struct file

char fname[10];

int start,size,block[10];

}f[10];

main()

int i,j,n;

printf("Enter no. of files:");

scanf("%d",&n);

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

printf("Enter file name:");

scanf("%s",&f[i].fname);

printf("Enter starting block:");

scanf("%d",&f[i].start);

f[i].block[0]=f[i].start;

printf("Enter no.of blocks:");

scanf("%d",&f[i].size);

printf("Enter block numbers:");

for(j=1;j<=f[i].size;j++)

scanf("%d",&f[i].block[j]);

printf("File\tstart\tsize\tblock\n");

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

printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);

for(j=1;j<=f[i].size-1;j++)

printf("%d--->",f[i].block[j]);

printf("%d",f[i].block[j]);

printf("\n");

getch();
}

OUT PUT
INDEXED FILE ALLOCATION
AIM: Write a C Program to implement Indexed File Allocation method.

Algorithm:

Step 1: Start.

Step 2: Let n be the size of the buffer

Step 3: check if there are any producer

Step 4: if yes check whether the buffer is full

Step 5: If no the producer item is stored in the buffer

Step 6: If the buffer is full the producer has to wait

Step 7: Check there is any cosumer.If yes check whether the buffer is empty

Step 8: If no the consumer consumes them from the buffer

Step 9: If the buffer is empty, the consumer has to wait.

Step 10: Repeat checking for the producer and consumer till required

Step 11: Terminate the process.

PROGRAM
#include<stdio.h>

#include<conio.h>

main()

int n,m[20],i,j,sb[20],s[20],b[20][20],x;

printf("Enter no. of files:");

scanf("%d",&n);

for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);

scanf("%d%d",&sb[i],&s[i]);

printf("Enter blocks occupied by file%d:",i+1);

scanf("%d",&m[i]);

printf("enter blocks of file%d:",i+1);

for(j=0;j<m[i];j++)

scanf("%d",&b[i][j]);

printf("\nFile\t index\tlength\n");

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

printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);

while(1)

printf("\nEnter file name:");

scanf("%d",&x);

printf("file name is:%d\n",x);

i=x-1;

printf("Index is:%d",sb[i]);

printf("Block occupied are:");

for(j=0;j<m[i];j++)

printf("%3d",b[i][j]);
}

OUTPUT
SCAN

CODE#include<conio.h>

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

printf("%d-->",d[i]);

sum=disk+max;

printf("\nmovement of total cylinders %d",sum);


getch();

return 0;

OUTPUT

C SCAN

CODE

#include<stdio.h>

int main()

int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;

printf("enter the no of tracks to be traveresed");

scanf("%d'",&n);

printf("enter the position of head");


scanf("%d",&h);

t[0]=0;t[1]=h;

printf("enter total tracks");

scanf("%d",&tot);

t[2]=tot-1;

printf("enter the tracks");

for(i=3;i<=n+2;i++)

scanf("%d",&t[i]);

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

for(j=0;j<=(n+2)-i-1;j++)

if(t[j]>t[j+1])

{ temp=t[j];

t[j]=t[j+1]; t[j+1]=temp;

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

if(t[i]==h)

j=i;

p=0;

while(t[j]!=tot-1)

atr[p]=t[j];

j++;

p++;

atr[p]=t[j];

p++;
i=0;

while(p!=(n+3) && t[i]!=t[h])

{ atr[p]=t[i]; i++;

p++;

for(j=0;j<n+2;j++)

if(atr[j]>atr[j+1]) d[j]=atr[j]-atr[j+1];

else d[j]=atr[j+1]-atr[j];

sum+=d[j];

printf("total header movements%d",sum);

printf("avg is %f",(float)sum/n);

getch();

}
FCFS
CODE
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
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",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
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;
}
OUT PUT

SSFS

CODE
#include<stdio.h>

struct head
{
int num;
int flag;
};

int main()
{
struct head h[33];
int array_1[33], array_2[33];
int count = 0, j, x, limit, minimum, location, disk_head, sum = 0;
printf("\nEnter total number of locations:\t");
scanf("%d", &limit);
printf("\nEnter position of disk head:\t");
scanf("%d", &disk_head);
printf("\nEnter elements of disk head queue\n");
while(count < limit)
{
scanf("%d", &h[count].num);
h[count].flag = 0;
count++;
}
for(count = 0; count < limit; count++)
{
x = 0;
minimum = 0;
location = 0;
for(j = 0; j < limit; j++)
{
if(h[j].flag == 0)
{
if(x == 0)
{
array_1[j] = disk_head - h[j].num;
if(array_1[j] < 0)
{
array_1[j] = h[j].num - disk_head;
}
minimum = array_1[j];
location = j;
x++;
}
else
{
array_1[j] = disk_head - h[j].num;
if(array_1[j] < 0)
{
array_1[j] = h[j].num - disk_head;
}
}
if(minimum > array_1[j])
{
minimum = array_1[j];
location = j;
}
}
}
h[location].flag = 1;
array_2[count] = h[location].num - disk_head;
if(array_2[count] < 0)
{
array_2[count] = disk_head - h[location].num;
}
disk_head = h[location].num;
}
count = 0;
while(count < limit)
{
sum = sum + array_2[count];
count++;
}
printf("\nTotal movements of the cylinders:\t%d", sum);
return 0;
}
OUT PUT

LOOK

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

#define LOW 0
#define HIGH 199

int main(){
int queue[20], head, q_size, i,j, seek=0, diff, max, temp, queue1[20], queue2[20], temp1=0, temp2=0;
float avg;

printf("%s\t", "Input the number of disk locations");


scanf("%d", &q_size);

printf("%s\t", "Enter initial head position");


scanf("%d", &head);

printf("%s\n", "Enter disk positions to read");

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


scanf("%d", &temp);
//queue1 - elems greater than head
if(temp >= head){
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
}
}

//sort queue1 - increasing order


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

//sort queue2 - decreasing order


for(i=0; i<temp2-1; i++){
for(j=i+1; j<temp2; j++){
if(queue2[i] < queue2[j]){
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}

if(abs(head-LOW) >= abs(head-HIGH)){

for(i=1,j=0; j<temp1; i++,j++){


queue[i] = queue1[j];
}

for(i=temp1+1, j=0; j<temp2; i++, j++){


queue[i] = queue2[j];
}

} else {

for(i=1,j=0; j<temp2; i++,j++){


queue[i] = queue2[j];
}

for(i=temp2+1, j=0; j<temp1; i++, j++){


queue[i] = queue1[j];
}

}
queue[0] = head;

for(j=0; j<q_size; 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)q_size;
printf("Average seek time is %f\n", avg);

return 0;
}
OUT PUT

You might also like