Experiment 4
Experiment 4
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)
{
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;
}
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.
#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)
#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