forked from csc-mec/SS_LAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked.c
85 lines (69 loc) · 1.69 KB
/
linked.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<stdlib.h>
#define disk_size 100
int disk[disk_size];
int f_link[disk_size];
int linked(int start, int blocks[], int numb)
{
int curr = start;
int flag = 0;
if(disk[curr] == 0)
{
for(int i=0; i<numb; i++)
{
if(disk[curr] == 0)
{
disk[curr] = 1;
printf("Block %d has been allocated\n",curr);
f_link[i] = blocks[i];
curr = blocks[i];
}
else
{
printf("Block %d cannot be allocated.\nAllocation Failed\n");
flag=1;
return flag;
}
}
f_link[curr] = -1;
printf("The EOF is at Block %d\n",curr);
}
else
{
printf("The Starting Block cannot be allocated.\nAllocation Failed\n");
flag=1;
return flag;
}
return flag;
}
void main()
{
printf("Linked Allocation of files\n");
int start,numb;
printf("Enter the starting block: ");
scanf("%d",&start);
printf("Enter the number of blocks for file: ");
scanf("%d",&numb);
int blocks[numb];
printf("Enter the sequence of blocks: ");
for (int i = 0; i < numb; i++)
{
scanf("%d",&blocks[i]);
}
for(int i=0;i<disk_size;i++)
{
disk[i] = 0;
}
int flag = 0;
flag = linked(start,blocks,numb);
if(flag == 0)
{
printf("The Block link:\t\t");
printf("%d(start)->",start);
for(int i=0; i<numb; i++)
{
printf("%d->",f_link[i]);
}
printf("EOF");
}
}