#include
int memoryStartAddress = -1; int memorySize = -1; struct jobList {
int id; /* 作业ID */
int size; /* 作业大小(需要的存储空间大小) */ int status;
/* 作业状态 0 : new job ,1 : in the memory , 2 : finished 、 */ struct jobList *next; /* 作业链表指针 */ };
struct freeList {
int startAddress; /* 分区起始地址 */ int size; /* 分区大小 */ struct freeList *next; /* 分区链表指针 */ };
struct usedList {
int startAddress; /* 分区起始地址 */
int jobID; /* 分区中存放作业ID */ struct usedList *next; /* 分区链表指针 */ };
void errorMessage(void) /*出现严重错误时显示信息并结束程序*/ {
printf(\
printf(\ getch(); exit(1); }
void open **fp,char * *mode)
/*以要求的方式打开文件*/ {
if((*fp = fopen()) == NULL) { printf(\、\ errorMessage(); }
}
void makeFreeNode(struct freeList **empty,int startAddress,int size) /*根据参数startAddress、size创建空闲节点,由empty指针返回*/ {
if((*empty = malloc(sizeof(struct freeList))) == NULL) { printf(\、\ errorMessage(); }
(*empty)->startAddress = startAddress; (*empty)->size = size; (*empty)->next = NULL; }
void iniMemory(void) /*初始化存储空间起始地址、大小*/ {
char MSA[10],MS[10];
printf(\ scanf(\
memoryStartAddress = atoi(MSA);
printf(\ scanf(\
memorySize = atoi(MS); }
char selectFitMethod(void) /*选择适应算法*/ {
FILE *fp;
char fitMethod; do{ printf(\ \\n 1 (Best fit) \\ \\n 2 (Worst fit) \\ \\n 3 (First fit) \\ \\n 4 (Last fit)\\n\ fitMethod = getche();
}while(fitMethod < '1' || fitMethod > '4'); open,\、cl\ switch(fitMethod) { case '1': fprintf(fp,\
fprintf(fp,\ break; case '2': fprintf(fp,\
fprintf(fp,\ break; case '3': fprintf(fp,\
fprintf(fp,\ break; case '4': fprintf(fp,\
fprintf(fp,\ break; }
fclose(fp);
return fitMethod; }
void inputJob(void) /*从键盘输入作业到D盘的JOB文件*/ {
int /*id,size, */status = 0,jobnum = 0; FILE *fp;
char id[10],size[10]; open,\、cl\
fprintf(fp,\
printf(\ \\nEnter a integer smaller than 1 to quit 、\\njob_ID\\tsize\\n\ do{
/* scanf(\ */ scanf(\ if(atoi(id) > 0 && atoi(size) > 0) { fprintf(fp,\ /* fprintf(fp,\ */ jobnum++; } else break;
}while(1); if(jobnum) printf(\ else { printf(\、\ errorMessage(); }
fclose(fp); }
int makeJobList(struct jobList **jobs)
/*从JOB文件中读出作业并创建作业链表*/ {
char jobID[10],size[10],status[10]; struct jobList *rear; FILE *fp;
open,\、cl\
fscanf(fp,\
if((*jobs = malloc(sizeof(struct jobList))) == NULL) { printf(\、\ fclose(fp); errorMessage(); }
rear = *jobs;
(*jobs)->next = NULL; while(!feof(fp)) { struct jobList *p; fscanf(fp,\ if((p = malloc(sizeof(struct jobList))) == NULL) { printf(\、\ fclose(fp); errorMessage(); } p -> next = rear -> next; rear -> next = p; rear = rear -> next; rear -> id = atoi(jobID);
rear -> size = atoi(size); rear -> status = atoi(status); }
fclose(fp); return 0; }
int updateJob jobList *jobs) /*更新作业链表中作业的状态*/ {
FILE *fp;
struct jobList *p;
open,\、cl\
fprintf(fp,\ for(p = jobs -> next;p;p = p -> next) fprintf(fp,\ fclose(fp); return 0; }
int showFreeList(struct freeList *empty) /*空闲分区队列显示*/ {
FILE *fp;
struct freeList *p = empty -> next; int count = 0;
open,\、cl\
fprintf(fp,\、、、\ printf(\、、、\ if(p) { fprintf(fp,\ printf(\ for(;p;p = p -> next) { fprintf(fp,\ printf(\ } fclose(fp); return 1; } else { fprintf(fp,\