操作系统源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
操作系统源代码 Prepared on 22 November 2020
#include<>
#include<>
#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("\n\tError !\a");
printf("\nPress any key to exit !");
getch();
exit(1);
}
void openFile(FILE **fp,char *filename,char *mode)
/*以要求的方式打开文件*/
{
if((*fp = fopen(filename,mode)) == NULL)
{
printf("\nCan't open %s in mode %s.",filename,mode);
errorMessage();
}
}
void makeFreeNode(struct freeList **empty,int startAddress,int size)
/*根据参数startAddress、size创建空闲节点,由empty指针返回*/ {
if((*empty = malloc(sizeof(struct freeList))) == NULL)
{
printf("\nNot enough to allocate for the free node .");
errorMessage();
}
(*empty)->startAddress = startAddress;
(*empty)->size = size;
(*empty)->next = NULL;
}
void iniMemory(void) /*初始化存储空间起始地址、大小*/
{
char MSA[10],MS[10];
printf("\nPlease input the start address of the memory !");
scanf("%s",MSA);
memoryStartAddress = atoi(MSA);
printf("\nPlease input the size of the memory !");
scanf("%s",MS);
memorySize = atoi(MS);
}
char selectFitMethod(void) /*选择适应算法*/
{
FILE *fp;
char fitMethod;
do{
printf("\n\nPlease input a char as fallow to select the fit method !\ \n 1 (Best fit) \
\n 2 (Worst fit) \
\n 3 (First fit) \
\n 4 (Last fit)\n");
fitMethod = getche();
}while(fitMethod < '1' || fitMethod > '4');
openFile(&fp,"d:\\","a");
switch(fitMethod)
{
case '1':
fprintf(fp,"\n\n\n\n\tBest fit");
fprintf(fp,"\n**********************************************");
break;
case '2':
fprintf(fp,"\n\n\n\n\tWorst fit");
fprintf(fp,"\n**********************************************");
break;
case '3':
fprintf(fp,"\n\n\n\n\tFirst fit");
fprintf(fp,"\n**********************************************");
break;
case '4': fprintf(fp,"\n\n\n\n\tLast fit");
fprintf(fp,"\n**********************************************");
break;
}
fclose(fp);
return fitMethod;
}
void inputJob(void) /*从键盘输入作业到D盘的JOB文件*/
{
int /*id,size, */status = 0,jobnum = 0;
FILE *fp;
char id[10],size[10];
openFile(&fp,"d:\\","w");
fprintf(fp,"job_ID\tsize\tstatus");
printf("\n\n\n\nPlease input the jobs as fallow !\
\nEnter a integer smaller than 1 to quit .\njob_ID\tsize\n");
do{
/* scanf("%d%d",&id,&size); */
scanf("%s\t%s",id,size);
if(atoi(id) > 0 && atoi(size) > 0)
{
fprintf(fp,"\n%s\t%s\t%d",id,size,status);
/* fprintf(fp,"\n%d\t%d\t%d",id,size,status); */
jobnum++;