-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocate.java
More file actions
186 lines (176 loc) · 6.29 KB
/
Allocate.java
File metadata and controls
186 lines (176 loc) · 6.29 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package fileallocation;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Allocate
{
Fylat blocks[];
public ArrayList<Fylat> files;
public ArrayList<ArrayList<Integer>> indexed;
Random rand = new Random();
public int lastaccess;
int blocknum;
public Allocate(int blocknum, ArrayList<Fylat> files)
{
this.blocknum=blocknum;
blocks=new Fylat[blocknum];
this.files=files;
indexed=new ArrayList<ArrayList<Integer>>();
}
public void display()
{
for(int i=0;i<blocks.length;i++)
{
if(blocks[i]==null)
{
System.out.println("Block number: "+i+" is empty");
}
else
{
System.out.println("Block number: "+i+" contains the file: "+blocks[i].filename);
}
}
}
public void contiguous()
{
int count; // count=hagm el file
System.out.println("**************\n");
System.out.println("Display #1 \n");
System.out.println("**************\n\n");
for (int i = 0; i < files.size(); i++)
{
this.lastaccess = rand.nextInt(blocknum);
while(blocks[lastaccess]!= null)
{
this.lastaccess = rand.nextInt(blocknum);
}
if(lastaccess+files.get(i).filesize<blocks.length)
{
count=0;
for (int j = lastaccess; j < files.get(i).filesize + lastaccess; j++)
{
if(blocks[j]==null)
{
count++;
}
}
if (count==files.get(i).filesize)
{
for(int z =lastaccess;z<lastaccess+count;z++)
{
blocks[z]=files.get(i);
}
System.out.println("File "+files.get(i).filename +"'s starting block is: " + (lastaccess));
int size;
size=lastaccess;
lastaccess+=count; // start mn akher mkan w2f feh el file
System.out.println("File "+files.get(i).filename +"'s ending block is: " + (lastaccess-1));
System.out.println("File "+files.get(i).filename +"'s size is: " + (lastaccess-size));
}
}
else
{
System.out.println("File "+files.get(i).filename +" can't be allocated");
}
}
System.out.println("\n**************\n");
System.out.println("Display #2 \n");
System.out.println("**************\n");
}
public void linked()
{
int count; // count=hagm el file
System.out.println("**************\n");
System.out.println("Display #1 \n");
System.out.println("**************\n\n");
for (int i = 0; i < files.size(); i++)
{
int check=0;
for(int z=0;z<blocks.length;z++)
{
if(blocks[z]==null)
{
check++;
}
}
if(check>=files.get(i).filesize)
{
this.lastaccess = rand.nextInt(blocknum);
count=0;
while(count!=files.get(i).filesize)
{
while(blocks[lastaccess]!= null)
{
this.lastaccess = rand.nextInt(blocknum);
}
blocks[this.lastaccess]=files.get(i);
count++;
if (count==1)
{
System.out.println("File "+files.get(i).filename +"'s starting block is: " + (lastaccess));
}
}
System.out.println("File "+files.get(i).filename +"'s ending block is: " + (lastaccess));
System.out.println("File "+files.get(i).filename +"'s size is: " + (count));
}
else
{
System.out.println("File "+files.get(i).filename +" can't be allocated");
}
}
System.out.println("\n**************\n");
System.out.println("Display #2 \n");
System.out.println("**************\n");
}
public void indexed()
{
int count=0;
for(int i=0;i<files.size();i++)
{
indexed.add(new ArrayList<Integer>());//two dim array rakam el file w ely byshawr
int check=0;
for(int z=0;z<blocks.length;z++)
{
if(blocks[z]==null)
{
check++;
}
}
if(check>=files.get(i).filesize)
{
int counter2=0;
this.lastaccess = rand.nextInt(blocknum);
if(counter2==0&&blocks[lastaccess]==null)
{
files.get(i).start=indexed.get(i);
}
count=0;
while(count!=files.get(i).filesize)
{
while(blocks[lastaccess]!= null)
{
this.lastaccess = rand.nextInt(blocknum);
}
blocks[this.lastaccess]=files.get(i);
files.get(i).start.add(lastaccess);
count++;
counter2++;
}
}
}
for(int j=0;j<indexed.size();j++)
{
System.out.println(" File " + j);
for(int z=0;z<indexed.get(j).size();z++)
{
if(z==indexed.get(j).size()-1)
{
System.out.println(" File Block "+j+" Memory Block Number "+indexed.get(j).get(z)+" Next BLock Number: None ");
break;
}
System.out.println(" File Block "+j+" Memory Block Number "+indexed.get(j).get(z)+" Next BLock Number "+indexed.get(j).get(z+1));
}
System.out.println("");
}
}
}