进程调度算法–时间片轮转调度算法
#include
#include
#include
#define time 2
typedef int datatype;
typedef struct link_node {
char name[20];
int arrive;
int start;
int finish;
int zhouzhuan;
float daizhou;
int service;
int count;
int t;
struct link_node *next;
} node;
typedef node *linklist;
linklist creatbyqueue() {
linklist head,r,s;
datatype arrive, server;
char str[20];
head = r = (linklist)malloc(sizeof(node));
head->next = NULL;
printf("当前时间片为两个单位时间,所有进程拥有相间的时间片n");
printf("输入进程名,到达时间,服务时间:n");
scanf("%s",str);
scanf("%d",&arrive);
scanf("%d",&server);
s = (linklist)malloc(sizeof(node));
s->t = 0;
s->arrive = arrive;
s->service = server;
strcpy(s->name, str);
s->count = s->service;
head->next = s;
s->next = NULL;
return head;
}
void print(linklist head, int i) {
linklist p=head->next,q;
while(p) {
if(p->arrive == i) {
printf("进程%s加入就绪队列n",p->name);
}
p = p->next;
}
p=head->next;
printf("当前运行的进程是%sn",head->next->name);
printf("就绪队列中有进程:");
while(p->next) {
p = p->next;
if(p->arrive <= i)
printf(" %s",p->name);
}
printf("n");
}
void delete(linklist head, int i) {
linklist p,q;
p = head->next;
q = p->next;
head->next = q;
free(p);
}
void insert(linklist head) {
linklist p;
char str[20];
datatype arrive, server;
linklist r = (linklist)malloc(sizeof(node));
p = head->next;
while(p->next) {
p = p->next;
}
scanf("%s",str);
scanf("%d",&arrive);
scanf("%d",&server);
strcpy(r->name,str);
r->arrive = arrive;
r->t = 0;
p->next = r;
r->service = server;
r->count = r->service;
r->next = NULL;
}
void sort(linklist head) {
linklist p,pre,r,m;
m = head;
r = pre = head->next;
p = pre->next;
while(r) {
while(p) {
if(pre->arrive > p->arrive) {
m->next = p;
pre->next = p->next;
p->next = pre;
p = pre;
pre = m->next;
}
m = m->next;
pre = pre->next;
p = p->next;
}
r = r->next;
}
}
void statistic(linklist head, int i) {
linklist p = head->next;
p->finish = i;
p->zhouzhuan = p->finish - p->arrive;
p->daizhou = p->zhouzhuan * 1.0/ p->service;
}
void finish(linklist run, int i) {
printf("进程%s任务完成n",run->next->name);
statistic(run, i);
printf("到达时间t开始时间t完成时间t周转时间t带权周转时间n");
printf("%5dt%12dt%12dt%12dtt%.3ftn",run->next->arrive, run->next->start, run->next->finish, run->next->zhouzhuan, run->next->daizhou);
delete(run,i);
if(run->next!=NULL)
if(run->next->service == run->next->count)
run->next->start = i;
if(run->next != NULL)
print(run,i);
}
int main() {
linklist head,p;
int i = 0;
head = creatbyqueue();
int flag = 1;
while(flag) {
printf("是否继续输入(1或0):");
scanf("%d",&flag);
if(flag) {
insert(head);
}
}
sort(head);
linklist run =(linklist)malloc(sizeof(node));
run->next = NULL;
linklist r = run;
while(head->next!=NULL ) {
printf("已经过单位时间%dn",i);
if(head->next->arrive > i && run->next == NULL) {
printf("当前无进程执行或就绪n");
}
else if(head->next->arrive == i) {
while(head->next) {
if(head->next->arrive == i) {
r->next = head->next;
head->next = head->next->next;
r = r->next;
r->next = NULL;
} else break;
}
if(run->next->count != 0) {
if(run->next->service==run->next->count)
run->next->start = i;
if(run->next->t != time) {
run->next->count--;
run->next->t++;
} else {
printf("进程%s的时间片用完n",run->next->name);
run->next->t = 0;
if(run->next->next != NULL) {
r->next = run->next;
run->next = run->next->next;
r = r->next;
r->next = NULL;
if(run->next->service==run->next->count)
run->next->start = i;
run->next->count--;
run->next->t++;
}
}
print(run,i);
} else {
finish(run,i);
if(run->next) {
run->next->count--;
run->next->t++;
}
}
} else {
if(run->next->count != 0) {
if(run->next->t != time) {
run->next->count--;
run->next->t++;
} else {
printf("进程%s的时间片用完n",run->next->name);
run->next->t = 0;
if(run->next->next != NULL) {
r->next = run->next;
run->next = run->next->next;
r = r->next;
r->next = NULL;
if(run->next->service==run->next->count)
run->next->start = i;
run->next->count--;
run->next->t++;
} else {
run->next->count--;
}
}
print(run,i);
} else {
finish(run,i);
if(run->next) {
run->next->count--;
run->next->t++;
}
}
}
i++;
}
while(run->next != NULL) {
printf("已经过单位时间%dn",i);
if(run->next->service==run->next->count)
run->next->start = i;
if(run->next->count != 0) {
if(run->next->t != time) {
run->next->count--;
run->next->t++;
} else {
printf("进程%s的时间片用完n",run->next->name);
run->next->t = 0;
if(run->next->next != NULL) {
r->next = run->next;
run->next = run->next->next;
r = r->next;
r->next = NULL;
if(run->next->service==run->next->count)
run->next->start = i;
run->next->count--;
run->next->t++;
} else {
run->next->count--;
}
}
print(run,i);
} else {
finish(run,i);
if(run->next) {
run->next->count--;
run->next->t++;
}
}
i++;
}
}
如果觉得《linux进程调度时间片算法 进程调度算法–时间片轮转调度算法》对你有帮助,请点赞、收藏,并留下你的观点哦!