69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct _proc
{
char name[32]; /*定义进程名称*/
int team; /*定义柱面号*/
int ci; /*定义磁道面号*/
int rec; /*定义记录号*/
struct _proc *prior;
struct _proc *next;
}PRO;
PRO *g_head=NULL,*g_curr=NULL,*local;
int record=0; //初始柱面号
int yi=1; //初始方向
int rec0=0; //初始记录号
void init()
{
PRO *p; /*初始化链表(初始I/O表)*/
g_head = (PRO*)malloc(sizeof(PRO));
g_head->next = NULL;
g_head->prior = NULL;
p = (PRO*)malloc(sizeof(PRO));
strcpy(p->name, "P1");
p->team=4;
p->ci=2;
p->rec=0;
p->next = NULL;
p->prior = g_head;
g_head->next = p;
g_curr=g_head->next;
p = (PRO*)malloc(sizeof(PRO));
strcpy(p->name, "P2");
p->team=4;
p->ci=3;
p->rec=3;
p->next = NULL;
p->prior = g_curr;
g_curr->next = p;
g_curr=p;
local = (PRO*)malloc(sizeof(PRO)); /*选中进程*/
strcpy(local->name, "P0");
local->team=0;
local->ci=0;
local->rec=0;
local->next=NULL;
local->prior=NULL;
}
void PrintInit() /*打印I/O表*/
{
PRO *t = g_head->next;
printf("当前I/O表为:\n");
printf(" **********************************\n");
printf(" ");
printf("*进程名 柱面号 磁道号 记录号*\n");
while(t!=NULL)
{
printf(" *%4s %8d %8d %5d *\n", t->name, t->team, t->ci, t->rec );
t = t->next;
}
printf(" **********************************\n");
printf("选择的进程为 :\n");
printf(" 进程名 柱面号 记录号\n");
printf("%4s %8d %8d\n", local->name, local->team, local->rec );
switch(yi)
{
case 1:
printf("current direction is UP\n");
printf("----------------------------------------\n");
break;
case 0:
printf("current direction is DOWN\n");
printf("----------------------------------------\n");
break;
}
}
void acceptreq() /*接受请求函数*/
{
PRO *p;
p = (PRO*)malloc(sizeof(PRO));
printf("please input the information of the new process\n");
printf("进程名:");
scanf("%s",p->name);
printf("柱面号(0-199):");
scanf("%d",&p->team); /*输入请求进程信息*/
printf("磁道号(0-20):");
scanf("%d",&p->ci);
printf("记录号(0-7):");
scanf("%d",&p->rec);
getchar();
g_curr=g_head; /*将此节点链入I/O请求表*/
while(g_curr->next!=NULL)
g_curr=g_curr->next;
p->next=NULL;
p->prior=g_curr;
g_curr->next=p;
g_curr=g_head->next;
PrintInit(); /*将新的I/O请求表输出*/
}
void qddd() /*驱动调度函数*/
{
PRO *out;
int deng=0;
int deng1=0;
int min=g_head->next->team;
int max=g_head->next->team;
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if(g_curr->team==record)
{
min=g_curr->rec;
out=g_curr;
deng=1;
break;
}
}
switch(deng)
{
case 1:
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if(g_curr->team==record&&abs(g_curr->rec-rec0)<=abs(min-rec0))
{
min=g_curr->rec;
out=g_curr;
}
}
break;
case 0:
switch (yi)
{
case 1:
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (g_curr->team > record)
{
min = g_curr->team;
deng1=1;
break;
}
}
switch(deng1)
{
case 1:
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (min>=g_curr->team&&g_curr->team>record)
{
min=g_curr->team;
out=g_curr;
}
}
break;
case 0:
yi=0;
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (max<=g_curr->team)
{
min=g_curr->team;
out=g_curr;
}
}
break;
}
break;
case 0:
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (g_curr->team < record)
{
max = g_curr->team;
deng1=1;
break;
}
}
switch(deng1)
{
case 1:
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (max<=g_curr->team&&g_curr->team<record)
{
max=g_curr->team;
out=g_curr;
}
}
break;
case 0:
yi=1;
for (g_curr=g_head->next;g_curr!=NULL;g_curr=g_curr->next)
{
if (min>=g_curr->team)
{
min=g_curr->team;
out=g_curr;
}
}
break;
}
break;
}
break;
}
strcpy(local->name,out->name);
local->team=out->team;
local->ci=out->ci;
local->rec=out->rec;
PrintInit(); /*将新的I/O请求表输出*/
printf("被选中进程:\n");
printf(" 进程名 柱面号 磁道号 记录号\n");
printf("%4s %8d %8d %5d\n", out->name, out->team, out->ci,out->rec );
switch(yi)
{
case 1:
printf("current direction is UP\n");
printf("---------------------------------------\n");
break;
case 0:
printf("current direction is DOWN\n");
printf("----------------------------------------\n");
break;
}
record = local->team;
rec0=local->rec;
if (out->next==NULL) /*将选中的进程从I/O请求表中删除*/
{
out->prior->next=NULL;
free(out);
}
else
{
out->prior->next=out->next;
out->next->prior=out->prior;
free(out);
}
}
void acceptnum() /*通过输入0~1选择‘驱动调度’或是‘接受请求’*/
{
float num;
char c='y';
do
{
printf("please input a number between 0 and 1\n");
printf("num<=0.5:接受请求 num>0.5:驱动调度\n");
printf("num:");
scanf("%f",&num);
getchar();
while((num<0||num>1)) /*过滤不合法数据 注意:本程序其他输入数据可能未过滤*/
{
printf("ERROR!!!! Input again please!\nnum:\n");
scanf("%f",&num);
getchar();
}
if(num>0.5) /*驱动调度*/
{
if (g_head->next==NULL)
printf("请求I/O表 is empty!!!\n"); /*请求表为空 无需调度*/
else
{
printf("----------------------------\n");
printf("驱动调度\n");
qddd(); /*调用函数进行调度*/
}
}
else /*接受请求*/
{
printf("---------------------------------\n");
printf("接受请求\n");
acceptreq();
}
printf("是否继续(y/n):"); /*输入n离开本程序*/
c=getchar();
if(c=='n'||c=='N')
printf("thank you for testing my program!\nBYE!\n");
}while(c=='y'||c=='Y');
}
int main () /*主程序*/
{
init();
PrintInit();
acceptnum();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef struct _proc
{
char name[32]; /*定义进程名称*/
int cylinder; /*定义柱面号*/
int track; /*定义磁道面号*/
int rec; /*定义记录号*/
struct _proc *prior;
struct _proc *next;
}PRO;
PRO *g_head=NULL,*g_curr=NULL,local/* no need to dynamically allocation local*/;
int g_cylinder=0; //初始柱面号
int direction=1; //初始方向
int rec0=0; //初始记录号
// Add a new process with given parameters to the end of the
// global doubly linked list pointed by g_head
//
void AddProcess(const char * name, int cylinder, int track, int rec)
{
PRO * p=(PRO*)malloc(sizeof(PRO));
strcpy(p->name, name);
p->cylinder=cylinder;
p->track=track;
p->rec=rec;
p->next=NULL;
// should use a PRO* to point to the tail, or is g_curr designed for that purpose??
PRO** c=&g_head, *prior=NULL;
while(*c)
prior=*c, c=&(*c)->next;
*c=p;
p->prior=prior;
printf("AddProcess %s, prior is %p\n", p->name, p->prior);
}
// PRO as per its definition, can be copied bitwise
void CopyProcess(PRO* dest, const PRO* src)
{
memcpy(dest, src, sizeof(PRO));
}
// Remove an existing PRO node from the double list pointed by g_head;
//
void RemoveProcess(PRO * p)
{
PRO * prev, *next;
// FOR DEBUG
printf("RemoveProcess:%s, %p, %p\n", p->name, p->prior, p->next);
prev=p->prior;
next=p->next;
if(prev)
prev->next=next;
else // is the first node
g_head=next;
if(next)
next->prior=prev;
free(p);
}
void init()
{
AddProcess("P1", 1, 2, 0);
AddProcess("P2",2,3,3);
AddProcess("P3", 3,5, 5);
AddProcess("P4", 4, 5, 1);
AddProcess("P5", 3, 5, 1);
AddProcess("P6", 2, 5, 1);
memset(&local, 0, sizeof(PRO));
strcpy(local.name, "P0");
}
void PrintInit() /*打印I/O表*/
{
PRO *t = g_head;
printf("Current I/O table is:\n");
printf(" **********************************\n");
printf(" ");
printf("*Process Cylinder Track Record*\n");
while(t!=NULL)
{
printf(" *%4s %8d %8d %5d *\n", t->name, t->cylinder, t->track, t->rec );
t = t->next;
}
printf(" **********************************\n");
printf("Most recently disptached process:\n");
printf(" Process Cylinder Track Record\n");
printf("%7s %8d %7d, %8d\n\nCurrent direction: %s\n---------------------\n", local.name, local.cylinder,
local.track, local.rec, direction?"UP":"DOWN" );
}
void acceptreq() /*接受请求函数*/
{
PRO p;
printf("please input the information of the new process\n");
printf("Process:");
scanf("%s",p.name);
printf("Cylinder(0-199):");
scanf("%d",&p.cylinder); /*输入请求进程信息*/
printf("Track(0-20):");
scanf("%d",&p.track);
printf("Record(0-7):");
scanf("%d",&p.rec);
getchar();
AddProcess(p.name, p.cylinder, p.track, p.rec);
}
void qddd() /*驱动调度函数*/
{
PRO *out, *p;
int deng=0;
int deng1=0;
int min;
int max;
if(!g_head)
{
printf("\n\n***Cannot disptach with an empty I/O table!***\n\n");
return;
}
min=g_head->cylinder;
max=g_head->cylinder;
for (p=g_head;p!=NULL;p=p->next)
{
if(p->cylinder==g_cylinder)
{
min=p->rec;
out=p;
deng=1;
break;
}
}
switch(deng)
{
case 1:
for (p=g_head;p!=NULL;p=p->next)
{
if(p->cylinder==g_cylinder&&abs(p->rec-rec0)<=abs(min-rec0))
{
min=p->rec;
out=p;
}
}
break;
case 0:
switch (direction)
{
case 1:
for (p=g_head;p!=NULL;p=p->next)
{
if (p->cylinder > g_cylinder)
{
min = p->cylinder; // what's the purpose of min? value assigned here never used later
deng1=1;
break;
}
}
switch(deng1)
{
case 1:
for (p=g_head;p!=NULL;p=p->next)
{
if (min>=p->cylinder&&p->cylinder>g_cylinder)
{
min=p->cylinder;
out=p;
}
}
break;
case 0:
direction=0;
for (p=g_head;p!=NULL;p=p->next)
{
if (max<=p->cylinder)
{
min=p->cylinder;
out=p;
}
}
break;
}
break;
case 0:
for (p=g_head;p!=NULL;p=p->next)
{
if (p->cylinder < g_cylinder)
{
max = p->cylinder;
deng1=1;
break;
}
}
switch(deng1)
{
case 1:
for (p=g_head;p!=NULL;p=p->next)
{
if (max<=p->cylinder&&p->cylinder<g_cylinder)
{
max=p->cylinder;
out=p;
}
}
break;
case 0:
direction=1;
for (p=g_head;p!=NULL;p=p->next)
{
if (min>=p->cylinder)
{
min=p->cylinder;
out=p;
}
}
break;
}
break;
}
break;
}
CopyProcess(&local, out);
RemoveProcess(out);
g_cylinder = local.cylinder;
rec0=local.rec;
}
// prompt for user input
// 0: 驱动调度
// 1: 接受请求
// other: exit
void acceptinput() /*通过输入0或1选择‘驱动调度’或是‘接受请求’*/
{
int num, done;
do
{
printf("Please enter an integer to choose action to be taken: \n");
printf("0:Accept Request; 1:Driver Dispatch; otherwise: Exit\n");
printf("num:");
scanf("%d",&num);
done=0;
switch(num){
case 1: /*驱动调度*/
printf("----------------------------\n");
printf("Dispatching...\n");
qddd(); /*调用函数进行调度*/
break;
case 0: /*接受请求*/
printf("---------------------------------\n");
printf("Adding a new request...\n");
acceptreq();
break;
default:
return;
}
PrintInit();
}while(true);
}
int main () /*主程序*/
{
init();
PrintInit();
acceptinput();
return 0;
}
注意,你的调度程序保证了柱面小的进程总是优先被调用,这样第172到174行的代码,转贴如下
{
if (p->cylinder > g_cylinder)
{
min = p->cylinder; // what's the purpose of min? value assigned here never used later
deng1=1;
break;
}
}
这个判断总是为真(p->cylinder等于g_cylinder的情形在157-164行处理了), 这样deng1永远不会为0,从而方向永远没有机会改变,总是输出为UP。 这是个逻辑错误,如果你贴出你的qddd的调度算法的描述或伪码,大家会更容易找出问题。
你的代码中修改的部分,比如local直接用一个全局PROC对象而非指针,g_head->next大部分替换为个g_head, 插入节点、删除节点、复制节点的代码抽取出来成为独立的函数等,我个人觉得是更好的设计。为了我个人的理解,对象名也通过查找替代整理了一下,如果原来的名字更方便你自己理解,当然应该保留。
不好意思,C我也不算熟,如果口气有点托大,请谅解。