操作系统实验3:内存分配与回收

今天早上做了操作系统实验:

1、阅读理解两个例程,掌握例程的运作流程。

2、连续式分配例程中提供了三种分配算法:首次适应、循环首次适应、最佳适应。例程还缺少分配作业和回收作业的功能。请至少实现一种分配算法并完成分配作业的功能,保证该例程能够正确实现分配作业的功能

3、回收作业的功能属于选做部分。

4、分页管理例程缺少分配作业和回收的功能,请实现这两个功能,保证该例程能够正确实现分页管理的分配与回收功能

5、上述要求2和4,必须完成其中一个。

 

连续式分配代码贴上:

 

  1 #include <stdio.h>

  2 #include <stdlib.h>

  3 #include <string.h>

  4 #include <time.h>

  5 

  6 #define ret printf("\n")

  7 #define spa printf(" ")

  8 #define hli printf("-")

  9 #define vli printf("    |")

 10 #define tab printf("\t")

 11 #define capacity 1024          //内存总大小

 12 #define max 100              //数组最大长度

 13 #define jobsum 8             //作业总数量

 14 

 15 

 16 

 17 void _sleep(int n){

 18     clock_t goal;

 19     goal = (clock_t)n * CLOCKS_PER_SEC + clock();

 20     while(goal > clock());

 21 }

 22 

 23 char _keygo(){                //按任意键继续

 24     char c;

 25     printf("Please touch any key to continue....\n");

 26     c = getchar();

 27     return c;

 28 }

 29 

 30 typedef struct job JOB;        

 31 struct job {                //作业结构体

 32     int jobid;                //作业ID,系统做东分配,无需输入

 33     char name[20];            //作业名称

 34     int vol;                //作业大小,即要申请的内存空间大小

 35 };                    

 36 

 37 JOB jobdata[jobsum] = {        //作业队列

 38     {8100, "System", 50},

 39     {8101, "QianQian", 32},

 40     {8102, "XunLei", 128},

 41     {8103, "Dictionary", 76},

 42     {8104, "NorDun", 86},

 43     {10001, "QQ", 168},

 44     {10002, "eMule", 98},

 45     {10003, "Word", 43},

 46 };

 47 

 48 typedef struct memblock MEM;//内存分区结构

 49 struct memblock{

 50     int head;                //地址

 51     int length;                //长度

 52     int state;                //状态,0表示空闲,1表示占用

 53     int jobid;                //已分配,记录作业ID,否则为0

 54 };

 55 

 56 MEM memdata[max] = {        //内存状态表

 57     {0, 50, 1, 8100},

 58     {50, 50, 0, 0},

 59     {100, 32, 1, 8101},

 60     {132, 128, 1, 8102},

 61     {260, 100, 0, 0},

 62     {360, 76, 1, 8103},

 63     {436, 200, 0, 0},

 64     {636, 88, 1, 8104},

 65     {724, 300, 0, 0},

 66 };

 67 

 68 int memsum = 9;                //当前分区总数量,包括已分配分区和空闲分区

 69 int curp = 0;                //curp 是位置指针

 70 

 71 MEM membackup[9] = {        //内存状态源数据备份,用于还原

 72     {0, 50, 1, 8100},

 73     {50, 50, 0, 0},

 74     {100, 32, 1, 8101},

 75     {132, 128, 1, 8102},

 76     {260, 100, 0, 0},

 77     {360, 76, 1, 8103},

 78     {436, 200, 0, 0},

 79     {636, 88, 1, 8104},

 80     {724, 300, 0, 0},

 81 };

 82 

 83 int job_locate(int id){        //根据作业ID,查找作业在数组jobdata的位置

 84     int i;

 85     for(i=0;i<jobsum;++i)

 86         if(id == jobdata[i].jobid)

 87             return i;

 88     return -1;

 89 }

 90 

 91 void mem_state(){            //根据memdata数组显示当前内存状态

 92     int i, j, k;

 93     for(i=0;i<memsum;++i){

 94         tab;

 95         printf("%4ik", memdata[i].head);

 96         for(j=0;j<20;++j) hli;

 97         ret;tab; vli;

 98         for(j=0;j<5;++j) spa;

 99         if(memdata[i].jobid == 0)

100             printf("%10s", "$block$");

101         else{

102             k = job_locate(memdata[i].jobid);

103             printf("%10s", jobdata[k].name);

104         }

105         vli;ret;

106     }

107     printf("\t1024k");

108     for(j=0;j<20;++j) hli;

109     ret;

110 }

111 

112 int mem_allocate_ff(int k){            //首次适应算法

113     int i;

114     int job_size = jobdata[k].vol;

115 

116     // for(i=0;i<memsum;++i){

117     //     printf("%d %d %d %d\n", memdata[i].head, memdata[i].length, memdata[i].state, memdata[i].jobid);

118     // }

119     for(i=0;i<memsum;++i){

120         if(!memdata[i].state && job_size <= memdata[i].length)

121             return i;

122     }

123     return -1;

124 }

125 

126 

127 int mem_allocate_cf(int k){            //循环首次适应算法

128     int i;

129     int job_size = jobdata[k].vol;

130     int t = memsum;

131     printf("memsum = %d\n", memsum);

132     while(t){

133         curp %= memsum;

134         i = curp;

135         //printf("t : %d curp : %d length: %d state: %d\n", t, curp, memdata[i].length, memdata[i].state);

136         if(!memdata[i].state && job_size <= memdata[i].length){

137             curp++;

138             return i;

139         }

140         curp++;

141         t--;

142     }

143     return -1;

144 }

145 

146 int mem_allocate_bf(int k){            //最佳适应算法

147     int i;

148     int job_size = jobdata[k].vol;

149     int min = -1;

150     for(i=0;i<memsum;++i){

151         if(!memdata[i].state && job_size <= memdata[i].length){

152             if(min == -1) min = i;

153             else if(memdata[i].length<memdata[min].length) min = i;

154         }    

155     }

156     if(min!= -1) return min;

157     return -1;

158 }

159 

160 void mem_allocate(int i, int j){    //将分区j分配给作业i

161     int k;

162 

163     if(jobdata[i].vol == memdata[j].length){

164         memdata[j].state = 1;

165         memdata[j].jobid = jobdata[i].jobid;

166     }

167     else{

168         for(k=memsum-1;k>=j;--k){

169             memdata[k+1] = memdata[k];

170         }

171         int temp = memdata[j].length;

172         memdata[j].length = jobdata[i].vol;

173 

174         memdata[j].state = 1;

175         memdata[j].jobid = jobdata[i].jobid;

176         memdata[j+1].length = temp - memdata[j].length;

177         memdata[j+1].state = 0;

178         memdata[j+1].jobid = 0;

179         memdata[j+1].head = memdata[j].length + memdata[j].head;

180         ++memsum;

181     }

182 }

183 

184 int mem_locate(int id){        //根据作业ID,查找jobdata的id在数组memdata的位置

185     int i;

186     for(i=0;i<memsum;++i)

187         if(id == memdata[i].jobid)

188             return i;

189     return -1;

190 }

191 

192 void mem_redraw(int k){                //作业回收

193     int i;

194     int t = mem_locate(k);

195     printf("t : %d\n", t);

196     if(t == -1) return ;

197 

198     memdata[t].state = 0;

199     memdata[t].jobid = 0;

200 

201     if( t > 0 && memdata[t-1].state == 0 ){

202         memdata[t-1].state = 0;

203         memdata[t-1].jobid = 0;

204         memdata[t-1].length += memdata[t].length;

205         for(i=t+1;i<memsum;++i)

206             memdata[i-1] = memdata[i];

207         --memsum;

208         --t;

209     }

210     if(t+1 <= memsum && memdata[t+1].state == 0){

211         memdata[t].state = 0;

212         memdata[t].jobid = 0;

213         memdata[t].length += memdata[t+1].length;

214         for(i=t+2;i<memsum;++i)

215             memdata[i-1] = memdata[i];

216         --memsum;

217     }

218 }

219 

220 void mem_restore(){                    //内存状态还原

221     int k;

222     memsum = 9;

223     for(k=0;k<memsum;++k){

224         memdata[k].head = membackup[k-1].head;

225         memdata[k].jobid = membackup[k-1].jobid;

226         memdata[k].state = membackup[k-1].state;

227         memdata[k].length = membackup[k-1].length;

228     }

229     for(k=memsum;k<max;++k){

230         memdata[k].head = 0;

231         memdata[k].jobid = 0;

232         memdata[k].state = 0;

233         memdata[k].length = 0;

234     }

235 }

236 

237 int main(int argc, char const *argv[])

238 {

239     int i, j, start;

240     printf("\n------------The current memory state----------!\n");

241     mem_state();

242     _keygo();

243     printf("\n------------The work allocate start-----------!\n");

244     start = 5;

245     for(i=start;i<jobsum;++i){

246         printf("The work %s wait to allocate, The size is %iK\n", jobdata[i].name, jobdata[i].vol);

247         _sleep(1);

248         j = mem_allocate_ff(i);

249         printf("The allocate : %d\n", j);

250         if(j == -1) printf("The work %s is too big, the allocation is failed\n", jobdata[i].name);

251         else{

252             printf("allocation space to The work %s\n", jobdata[i].name);

253             mem_allocate(i, j);

254             mem_state();

255         }

256         _keygo();

257     }

258     printf("----------------The allocation over!---------\n");

259     printf("restore the memory!\n");

260     printf("\n----------------The current memory state------------\n");

261     mem_state();

262     j = 88;

263     while(j != 0){

264         printf("\n Please choose the job No: ");

265         scanf("%d", &j);

266         mem_redraw(j);

267         printf("\nThe restore success !\n");

268         printf("\n-------------The current memory state---------\n");

269         mem_state();

270     }

271     _keygo();

272     return 0;

273 }

 

 

 

 

你可能感兴趣的:(操作系统)