C语言任务调度框架,C语言实现任务调度.doc

该博客讨论了一种任务调度算法,包括等待时间最短优先(WSP)和按提交顺序执行两种策略。代码实现了任务节点结构体,用于存储任务的名称、所需CPU时间、提交时间和等待时间。`waitlest`函数按照WSP策略重新排序任务,减少平均等待时间,而`shijiancha`函数则按提交顺序执行任务并输出。在主函数中,用户可以输入任务信息并选择调度策略进行模拟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define MAXNUM 100

typedef struct Node1

{int name;

int time;

int gettime;

struct Node1 *next;

}Lnode1;

void waitlest(Lnode1 *p)

{Lnode1 *q,*head;

int a,wt,st,ft;

wt=0;st=0;ft=0;

printf("任务 所需CPU时间(s) 等待时间 结束时间 开始时间\n");

head=p;

while(head->next)

{q=head;

p=q->next;

while(p->next)

{ q=q->next;

p=p->next;

if(q->timetime)

{a=q->name;

q->name=p->name;

p->name=a;

a=q->time;

q->time=p->time;

p->time=a;

}

}

printf(" %d ",p->name);

printf("%10d",p->time);

wt=ft;

st=wt;

ft=ft+p->time;

printf("%10d",wt);

printf("%10d",ft);

printf("%10d",st);

printf("\n");

q->next=NULL;

free(p);

}

}

void shijiancha(Lnode1 *p)

{Lnode1 *first,*head,*q,*r;

int a,n,t;

printf("任务 \n");

first=p;

p=first->next;

n=p->gettime;

q=(Lnode1*)malloc(sizeof(struct Node1));

head=q;

while(p)

{r=(Lnode1*)malloc(sizeof(struct Node1));

q->next=r;

q=q->next;

q->next=NULL;

q->name=p->name;

q->time=p->time;

q->gettime=p->gettime;

r=head->next;

if(r->next)

{if(r->next->next)

{q=r->next;

while(q->next)

{

if(r->time>q->time)

{a=r->name;

r->name=q->name;

q->name=a;

a=r->time;

r->time=q->time;

q->time=a;

}

q=q->next;

r=r->next;

}//end while q n

}//end if r n n

}//end if r n

if(head->next->next)

{t=p->gettime-n;

n=p->gettime;

while(t!=0)

{if(t>head->next->time)

{t=t-head->next->time;

printf(" %d ",head->next->name);

printf("\n");

head->next=head->next->next;

}

else {if(tnext->time)

{head->next->time=head->next->time-t;

t=0;

}

else

{t=0;

printf(" %d ",head->next->name);

printf("\n");

head->next=head->next->next;

}

}

}//end while t

}//end if h n n

p=p->next;

}//end while p

r=head->next;

q=r->next;

while(q)

{

if(r->time>q->time)

{a=r->name;

r->name=q->name;

q->name=a;

a=r->time;

r->time=q->time;

q->time=a;

}

q=q->next;

r=r->next;

}

r=head->next;

while(r)

{ printf(" %d ",r->name);

printf("\n");

r=r->next;

}

}//end shijiancha

void main()

{int t,a,b,n;

Lnode1 *p,*q,*first;

p=(Lnode1*)malloc(sizeof(struct Node1));

first=p;

a=0;

printf("输入1,选择忽略提交任务的时间差;输入2,选择考虑提交任务的时

间差:");

scanf("%d",&b);

do

{ q=(Lnode1*)malloc(sizeof(struct Node1));

p->next=q;

p=p->next;

printf("请输入任务名:");

scanf("%d",&n);

p->name=n;

if(b==2)

{ printf("请输入任务提交时刻:");

scanf("%d",&t);

p->gettime=t;

}

else p->gettime=0;

printf("请输入任务所需时间(s):");

scanf("%d",&t);

p->time=t;

printf("是否继续添加任务?输入1继续提交,输入0结束提交 ");

scanf("%d",&a);

}while(a!=0);

p->next=NULL;

if(b==1)

{ printf("输入1:按提交顺序执行;输入2:按平均等待时间最短");

scanf("%d",&a);

if(a==1)

{ p=first->next;

printf("任务 所需CPU时间(s)\n");

while(p)

{

printf(" %d ",p->name);

printf(" %d",p->time);

printf("\n");

p=p->next;

}

}

else waitlest(first);

}

else shijiancha(first);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值