#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _proc
{
char name[32]; /*定义进程名称*/
int zhu; /*定义柱面号*/
int ci; /*定义磁道面号*/
int rec; /*定义记录号*/
struct _proc *next;
struct _proc *prior;
}PROC;
PROC *head=NULL,*current=NULL,*curr=NULL;/*定义头指针和当前位置指针和临时指针*/
int direction;/*定义移动臂方向*/
void init()/*建立双向链表*/
{
PROC *p;
direction=1;/*默认移动臂方向是向里*/
head=(PROC*)malloc(sizeof(PROC));
head->next=NULL;
head->prior=NULL;
p=(PROC*)malloc(sizeof(PROC));
strcpy(p->name, “P1″);
p->zhu=50;
p->ci=2;
p->rec=1;
p->next=NULL;
p->prior=head;
head->next=p;
curr=p;
p=(PROC*)malloc(sizeof(PROC));
strcpy(p->name, “P2″);
p->zhu=99;
p->ci=6;
p->rec=2;
curr->next=p;
p->prior=curr;
p->next=NULL;
curr=p;
p=(PROC*)malloc(sizeof(PROC));
strcpy(p->name, “P3″);
p->zhu=150;
p->ci=4;
p->rec=3;
curr->next=p;
p->prior=curr;
p->next=NULL;
curr=p;
p=(PROC*)malloc(sizeof(PROC));
strcpy(p->name, “P4″);
p->zhu=5;
p->ci=2;
p->rec=1;
curr->next=p;
p->prior=curr;
p->next=NULL;
curr=p;
current=head->next;
}
void printinit()/*打印函数*/
{
PROC *p2;
p2=head->next;
printf(“————————————-\n”);
printf(“ ———I/O LIST———\n”);
printf(“ process zhumian cidao rec \n”);
while(p2!=NULL)
{
printf(“%4s %10d %8d %5d\n”, p2->name, p2->zhu, p2->ci, p2->rec );
p2=p2->next;
}
printf(“the current process: \n”);
printf(“ process zhumian cidao rec \n”);
printf(“%4s %8d %8d %5d\n”, current->name, current->zhu, current->ci, current->rec );
printf(“the current direction is: %5s \n”,direction==1?”up”:”down”);
printf(“————————————-\n”);
}
void drive()/*驱动调度*/
{
int min,temp[30]={0},i,k,j;
PROC *p3;
if(head->next==NULL); /*若已全部调度,则空操作*/
else
{
if(direction==1)
{
i=0;j=0;k=0;min=1000;
for(p3=head->next;p3!=NULL;p3=p3->next)
{
if(p3->zhu-current->zhu>=0)/*将所有向里的进程记录下来*/
{temp[i]=p3->zhu-current->zhu;
i++;
}
else
{
direction=0;
drive();
}
}
for(j=0;j<=i;j++)
{
if(temp[j]<min)
{
min=temp[j];/*把最小值赋给min*/
k=j;/*记录最小值的位置给k*/
}
}
for(p3=head->next;p3!=NULL;p3=p3->next)/*使current指向离当前距离最短的进程*/
{if(p3->zhu==current->zhu+temp[k])
{
current=p3;
if(strcmp(head->next->name,current->name)==0)
head=head->next;
if(current->next==NULL)/*删除访问过的进程*/
current->prior->next=NULL;
else
{current->prior->next=current->next;
current->next->prior=current->prior;
}
printinit();
free(current);
break;
}
else
p3=p3->next;
}
}
else
{
i=0;j=0;k=0;min=1000;
for(p3=head->next;p3!=NULL;p3=p3->next)
{
if(current->zhu-p3->zhu>0)/*将所有外的进程记录下来*/
{temp[i]=current->zhu-p3->zhu;
i++;
}
else
{
direction=1;
drive();
}
}
for(j=0;j<=i;j++)
{
if(temp[j]<min)
{
min=temp[j];/*把最小值赋给min*/
k=j;/*记录最小值的位置给k*/
}
}
for(p3=head->next;p3!=NULL;p3=p3->next)/*使current指向离当前距离最短的进程*/
{if(p3->zhu==current->zhu-temp[k])
{
current=p3;
if(strcmp(head->next->name,current->name)==0)
head=head->next;
if(current->next==NULL)/*删除访问过的进程*/
current->prior->next=NULL;
else
{
current->prior->next=current->next;
current->next->prior=current->prior;
}
printinit();
free(current);
break;
}
else p3=p3->next;
}
}
}
}
accept()/*接受信息*/
{
PROC *p,*p1=head->next;
p=(PROC*)malloc(sizeof(PROC));
printf(“please input the information of the new process\nprocess-name:\nprocess-zhu\nprocess-ci\nprocess-rec\n”);
printf(“1.name\n”);
scanf(“%s”,p->name);
printf(“2.team 0-199\n”);
scanf(“%d”,&p->zhu); /*输入请求进程信息*/
printf(“3.ci 0-19\n”);
scanf(“%d”,&p->ci);
printf(“4.rec 0-7\n”);
scanf(“%d”,&p->rec);
getchar();
while(p1->next!=NULL) /*将此节点链入I/O请求表*/
p1=p1->next;
p1->next=p;
p->prior=p1;
p->next=NULL;
printf(“NEW I/O LIST\n\n”);
printinit();
}
main()/*主函数*/
{
int random=0;
init();
printinit();
do{
printf(“please input a number (0 or 1 or 2)\n”);
printf(“’0′ stand for accept request\n’1′ stand for drive control \n’2′ stand for quit\n”);
scanf(“%d”,&random);
if(random==1||random==0||random==2)
switch(random)
{
case 0:accept();break;
case 1:drive();break;
case 2:break;
}
else printf(“error! Wrong number,please input again”);
}while(random!=2);
}
在程序调度的那个函数有什么错啊,为什么不能完成调度
>> 本文固定链接: http://www.vcgood.com/archives/2371
高手们,看看程序
太恐怖了,看一下把