0% found this document useful (0 votes)
3 views8 pages

Experiment 4

Uploaded by

Ak
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)
3 views8 pages

Experiment 4

Uploaded by

Ak
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/ 8

Experiment 4 (i)

Objective: Implement file storage allocation technique: Contiguous (using array)


Solution:
About Contiguous allocation technique:
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a file
requires n blocks and is given a block b as the starting location, then the blocks assigned to
the file will be: b, b+1, b+2,……b+n-1. This means that given the starting block address and the
length of the file (in terms of blocks required), we can determine the blocks occupied by the
file.
The directory entry for a file with contiguous allocation contains
Address of starting block
Length of the allocated portion.
The file ‘mail’ in the following figure starts from the block 19 with length = 6 blocks.
Therefore, it occupies 19, 20, 21, 22, 23, 24 blocks.

Figure: Contiguous file allocation technique

4(i). Example program for Contiguous (using array) file storage allocation technique
#include<stdio.h>
struct filep
{
int id;
int start;
int count;
};
typedef struct filep files;
int main()
{
int disk[100];
int i,j,k;
for(i=0;i<100;i++)
disk[i]=-1;
files f[3];
for(i=0;i < 3;i++)
{
f[i].id=i;
printf("enter the number of blocks for file : %d", i);
scanf("%d", &f[i].count);
}
int flag = 0;
for(i=0;i<3;i++)
{
flag=0;
for(j=0;j<100;j++)
{
if(disk[j] == -1)
{

for(k=j;k< j+f[i].count && disk[k] == -1;k++)


{
flag++;
printf("\n flag for file %d : %d\n", i, flag);
}
if(flag == f[i].count)
{
for(k=j;k< j+f[i].count;k++)
{

f[i].start = j;
disk[k]=f[i].id;
}
j=j+f[i].count;
break;
}
else
{
j=j+flag;
flag=0;
}

}
if(j==99 && flag == 0)
printf("\n disk is full \n");
}
}
printf("\n here is the disk allocation details \n");
for(i=0;i<100;i++)
printf("%d ", disk[i]);
return 0;
}

Input: Program 4(i):


Disk Size = 100 (Block (all of equal size))
File ID Block_Count

File 0 5

File 1 3

File 2 2

Output:
here is the disk allocation details
0 0 0 0 0 1 1 1 2 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -
1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -
1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Experiment 4 (ii)
Objective: Implement file storage allocation technique: Linked –list (using linked-list)
Solution:
About Linked List Allocation technique:
In this scheme, each file is a linked list of disk blocks which need not be contiguous. The disk
blocks can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each block
contains a pointer to the next block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last
block (25) contains -1 indicating a null pointer and does not point to any other block.

Figure: Linked List file allocation technique


4(ii). Example program for file storage allocation technique (Linked List Allocation)

#include<stdio.h>
#include<stdlib.h>
struct file
{
int id;
int block;
struct file *next_block;
};
typedef struct file f;
int main()
{
f *file1, *temp, *temp2;
int i=1,j, count;
printf("enter the number of blocks for file : %d", i);
scanf("%d", &count);
for(j=0;j<count;j++)
{
if(j==0)
{
temp=(f*)malloc(sizeof(f));
temp->id=i;
temp->block=j;
temp->next_block=NULL;
file1=temp;
printf("%d\t", i);

}
else
{
temp=file1;
while ( temp->next_block != NULL)
temp=temp->next_block;

temp2=(f*)malloc(sizeof(f));
temp2->id=i;
temp2->block=j;
temp2->next_block=NULL;
temp->next_block = temp2;
printf("%d\t", i);

}
}
printf("\n Here is the disk allocation for file : \n");
temp=file1;
while(temp!=NULL)
{
printf("file id: %d\t block number: %d\t block address %d\n", temp->id,
temp->block,(int)temp);
temp=temp->next_block;
}
return 0;}
Input: Program 4(ii):
File ID Block_Count

File 1 5

Output:
Here is the disk allocation for file :
file id: 1 block number: 0 block address 38287408
file id: 1 block number: 1 block address 38287440
file id: 1 block number: 2 block address 38287472
file id: 1 block number: 3 block address 38287504
file id: 1 block number: 4 block address 38287536
file id: 1 block number: 5 block address 38287568
Experiment 4 (iii)

Objective: Implement file storage allocation technique: Indirect allocation (indexing)


Solution:
About Indirect allocation (indexing) technique:
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains
the disk address of the ith file block. The directory entry contains the address of the index
block as shown in the image:

Figure: Indirect (index) file allocation technique


4(iii). Example program for file storage allocation technique (Indirect (index))

#include<stdio.h>
#include<stdlib.h>
struct file
{
int id;
int block;
struct file *next_block;
};
typedef struct file f;

int main()
{
f *files[3], *temp, *temp2;
int count, i, j, id;
for(i=0;i<3;i++)
{
printf("enter the number of blocks for file : %d", i);
scanf("%d", &count);
for(j=0;j<count;j++)
{
if(j==0)
{
temp=(f*)malloc(sizeof(f));
temp->id=i;
temp->block=j;
temp->next_block=NULL;
files[i]=temp;
printf("%d\t", i);
}
else
{
temp=files[i];
while ( temp->next_block != NULL)
temp=temp->next_block;
temp2=(f*)malloc(sizeof(f));
temp2->id=i;
temp2->block=j;
temp2->next_block=NULL;
temp->next_block = temp2;
printf("%d\t", i);
}

}
}
temp=files[id];
while(temp!=NULL)
{
printf("file id: %d\t block number: %d\t block address %d\n", temp->id,
temp->block,(int)temp);
temp=temp->next_block;
}
return 0;
}
Input: Program 4(iii):
File ID Block_Count

File 0 5

File 1 4

File 2 6

Output:
disk allocation using index as file id
file id: 0 block number: 0 block address 35997744
file id: 0 block number: 1 block address 35997776
file id: 0 block number: 2 block address 35997808
file id: 0 block number: 3 block address 35997840
file id: 0 block number: 4 block address 35997872
file id: 1 block number: 0 block address 35997904
file id: 1 block number: 1 block address 35997936
file id: 1 block number: 2 block address 35997968
file id: 1 block number: 3 block address 35998000

file id: 2 block number: 0 block address 35998032


file id: 2 block number: 1 block address 35998064
file id: 2 block number: 2 block address 35998096
file id: 2 block number: 3 block address 35998128
file id: 2 block number: 4 block address 35998160
file id: 2 block number: 5 block address 35998192

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy