4月4日(时间片轮转算法)
typedef struct node
{
char name[20]; /*进程的名字*/
int prio; /*进程的优先级*/
int round; /*分配CPU的时间片*/
int cputime; /*CPU执行时间*/
int needtime; /*进程执行所需要的时间*/
char state; /*进程的状态,W——就绪态,R——执行态,F——完成态*/
int count; /*记录执行的次数*/
struct node *next; /*链表指针*/
}PCB;
今天做操作系统的实验,用c语言,很陌生,链表的操作,
void TimeCreate() /*时间片输入函数*/
{
PCB *tmp;
int i;
printf("输入进程名字和进程时间片所需时间:\n");
for(i = 0;i < num; i++)
{
if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
{
perror("malloc");
exit(1);
}
scanf("%s",tmp->name);
getchar();
scanf("%d",&(tmp->needtime));
tmp ->cputime = 0;
tmp ->state ='W';
tmp ->prio = 0;
tmp ->round = 2; /*假设每个进程所分配的时间片是2*/
tmp ->count = 0;
InsertTime(tmp);//按顺序插入队尾
}
}
void RoundRun() /*时间片轮转调度算法*/
{
int flag = 1;
GetFirst();
while(run != NULL)
{
Output();
while(flag)
{
run->count++;
run->cputime++;
run->needtime--;
if(run->needtime == 0) /*进程执行完毕*/
{
run ->state = 'F';
InsertFinish(run);
flag = 0;
}
else if(run->count == run->round)/*时间片用完*/
{
run->state = 'W';
run->count = 0; /*计数器清零,为下次做准备*/
InsertTime(run);//时间片用完 插到队尾
flag = 0;
}
}
flag = 1;
GetFirst();
}
}
void InsertTime(PCB *in) /*将进程插入到就绪队列尾部*/
{
PCB *fst;
fst = ready;
if(ready == NULL)
{
in->next = ready;
ready = in;
}
else
{
while(fst->next != NULL)
{
fst = fst->next;
}
in ->next = fst ->next;
fst ->next = in;
}
}
浙公网安备 33010602011771号