📄 cpu 调度1.c
字号:
#include "stdio.h"
#include "string.h"
#define N 5
struct process
{int time;
char name[8];
struct process *next;
};
struct process pro[N];
struct process *head=NULL;
int Creat(struct process *ph);
int FCFS();
int busort (struct process *ph);
int SJF(struct process *ph);
void putin();
int list(void)
//定义PCB控制块
typedef struct PCB{
long id;
int prio;
int lest;
long longth;
long alest;
}PCB;
//定义进程就绪队列
typedef struct ProList{
int count;
int PrioHigh;
typedef struct PCB * head;
typedef struct PCB * end;
}ProList;
main( )
{
putin();
list();
Creat(head);
if(!Creat(head))
printf("error!");
else
SJF(head);
}
int Creat(struct process *ph)
{struct process *ps,*pt;
if(ph==NULL)
{ph=(struct process*)malloc(sizeof(struct process));
ph->next=NULL;
}
( if((pc=(struct process*)malloc(sizeof(struct process)))==NULL)
{printf("It is error!");
return(0);
}
{ scanf("%s,%d,%d",pt->name,&(pt->time));
for(ps=ph;ps->next!=NULL&&ps->next->time<=pt->time;ps=ps->next)
{if (ps->next ==NULL)
{ps->next=pt;
pt->next=NULL;
}
else{
pt->next=ps->next;
ps->next=pt;
}
head=ph;
return(0);
}
int SJF(struct process *ph)
{struct process *ps,*pt;
busort (ph);
if(ph==NULL)
{ph=(struct process*)malloc(sizeof(struct process));
ph->next=NULL;
}
if((pt=(struct process*)malloc(sizeof(struct process)))==NULL)
{printf("It is error!");
return (0);
}
scanf("%s,%d,%d",pt->name,&(pt->time));
FCFS();
busort (ph);
}
int busort (struct process *ph)
{ struct process *ps,*pt3
char name1[3];
int time1;
for(pt=ph;pt->next!=NULL;pt=pt->next)
for(ps=pt;ps>pt-1&&ps->next!=NULL;ps=ps->next)
if(ps->time>ps->next->time)
{
strcpy(name1,ps->name);
strcpy(ps->name,ps->next->name);
strcpy(ps->next->name,name1);
time1=ps->time;
ps->time=ps->next->time;
ps->next->time=time1;
}
return(1);
}
int list(void)
{ int k;
printf("*****************************************\n");
printf(" 1:FCFS 2:SCF\n");
printf("*****************************************\n");
printf("please choose one num");
scanf("%d",&k);
while(k<1&&k>2);
switch(k)
{
case 1:SJF(head);
break;
case 2: SJF(head);
break;
}
}
int FCFS()
{ struct process *pt,*ps;
pt=(struct process*)malloc(sizeof(struct process));
scanf("%s,%d",pt->name,&(pt->time));
for(ps=head;ps->next!=NULL;ps=ps->next)
ps->next=pt;
pt->next=NULL;
return(1);
}
void putin()
{ int i;
for(i=0;i<N;i++)
{printf("please input the mun:");
scanf(":%s,%d\n",(pro+i)->name,&(pro+i)->time);
}
for(i=0;i<N;i++)
printf(" The input mun is:%s,%d\n",(pro+i)->name,&(pro+i)->time);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -