操作系统 内存分配(C实现 Visual Stdio2005)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEMORY 64
#define MAPSIZE 8

typedef short int byte;

typedef struct jcb
{
 char *name;   //作业名称
 int size;     //作业需要的内存大小
 int *jobPage; //指向作业的页表
 struct jcb *next;
}JCB;
byte bitMap[MAPSIZE];
int freeMem;
JCB *memJobHead;
void init()
{
 JCB *job;
 bitMap[0] = 206;
 bitMap[1] = 84;
 bitMap[2] = 0;
 bitMap[3] = 129;
 bitMap[4] = 0;
 bitMap[5] = 0;
 bitMap[6] = 0;
 bitMap[7] = 0;
 freeMem   = 54;
 memJobHead = (JCB*)malloc(sizeof(JCB));
 memJobHead->name = "j1";
 memJobHead->size = 6;
 memJobHead->jobPage = (int*)malloc(sizeof(int) * memJobHead->size);
 job = (JCB*)malloc(sizeof(JCB));
 job->name = "j2";
 job->size = 4;
 job->jobPage = (int*)malloc(sizeof(int) * memJobHead->size);
 memJobHead->next = job;
 job->next = memJobHead;

    memJobHead->jobPage[0] = 0;
 memJobHead->jobPage[1] = 1;
 memJobHead->jobPage[2] = 9;
 memJobHead->jobPage[3] = 11;
 memJobHead->jobPage[4] = 13;
 memJobHead->jobPage[5] = 24;
 job->jobPage[0] = 4;
 job->jobPage[1] = 5;
 job->jobPage[2] = 6;
 job->jobPage[3] = 31;

}
void output()
{
 byte test;
 for (int i = 0; i < MAPSIZE; i++){
  test = 0X80;
  for (int j = 0; j < 8; j++){
   test = test & bitMap[i];
   if (test != 0)
    printf("%3d", 1);
   else{
    printf("%3d", 0);
    test = 0X80;
    test >>= (j+1);
    continue;
   }
   test >>= 1;
  }
  printf("/n");
 }
}

//将有足够内存空间的作业添加到内存作业链表中
void addMemJob()
{
 JCB *newJob;
    byte find;
    byte test;
 int count;
 byte flag;
 bool finish;
    char *pName;
// newJob = job;
 count = 0;
 finish = false;

 printf("请输入要创建作业的作业名(exit-退出):");
 pName = (char*)malloc(sizeof(char)*15);
 scanf("%s", pName);
 if (strcmp(pName, "exit") == 0){
  printf("没有创建任何作业/n");
//  free(head);
  return ;
 }
 else{
  newJob = (JCB*)malloc(sizeof(JCB));
  newJob->name = pName;
  printf("请输入作业需占用的内存块数:");
  scanf("%d", &newJob->size);
  newJob->jobPage = (int*)malloc(sizeof(int) * newJob->size);
 }
 //将作业添加到内存作业链表中
 if (memJobHead == NULL){
  memJobHead = newJob;
  newJob->next = newJob;
 }
 else{
  newJob->next = memJobHead->next;
  memJobHead->next;
 }
 //修改内存位示图
 if (newJob->size < freeMem){
 for (int i = 0; i < MAPSIZE; i++){
  find = 0XFF;
  test = 0X80;
  flag = 0X80;
  if (!finish){
      find ^= bitMap[i];
      if (find){
       for (int j = 0; j < 8; j++){
        test &= find;
        if (test > 0){
         count++;
                        bitMap[i] |= (flag >>= j);       //修改将要分配的块的标志
         *newJob->jobPage++ = (i * 8) + j;
      flag = 0X80;
         if (count == newJob->size){       //分配足够资源后退出
       finish = true;     
          break;
         }
        }
     else{
      test = 0X80;
      test >>= (j+1);
      continue;
     }
        test >>= 1;
          }
      }
  }
 }
 }
 else{
  printf("为%s分配内存失败/n", newJob->name);
 }
 freeMem -= newJob->size;
}

//当内存资源不足或外存资源已全部调度完成,回收内存空间
void delMemJob()
{
 JCB *currJob;
 JCB *preJob;
 JCB *job;
 char *pName;
 int size;
 int i,j;
 byte bit;

 printf("请输入要释放内存的作业名(exit-退出):");
 pName = (char*)malloc(sizeof(char)*15);
 scanf("%s", pName);
 if (strcmp(memJobHead->name,pName) == 0)
  job = memJobHead;
 else{
  job = memJobHead;
  while (strcmp(job->name, pName) != 0){
   job = job->next;
  }
 }
 //修改位示图
 for (size = 0;size < job->size; size++){
  bit = 0X80;
  i = job->jobPage[size] / 8;
  j = job->jobPage[size] % 8;
  bit >>= j;
  bit = ~bit;
  bitMap[i] &= bit;
 }
 //将作业从内存作业链表中删除
 if ((memJobHead == job) && (memJobHead->next == memJobHead)){
  free(job->jobPage);
  free(job);
  memJobHead = NULL;
  return;
 }
 
 if (memJobHead == job){
  currJob = memJobHead->next;
  while (currJob->next != job)
   currJob = currJob->next;
  preJob = currJob;
  memJobHead = job->next;
  preJob->next = job->next;
 }
 else{
  currJob = memJobHead;
  while (currJob->next != job)
   currJob = currJob->next;
  preJob = currJob;
  preJob->next = job->next;
 }
 freeMem += job->size;
 free(job->jobPage);
 free(job);
}
void main()
{

 init();
 printf("初始状态/n");
 output();
 addMemJob();
 output();
 delMemJob();
 output();
    getchar();
 getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值