📄 1.txt
字号:
#define N 20
#include<stdio.h>
#include<conio.h>
typedef struct pcb
{
char pname[N];
int runtime;
int arrivetime;
char state;
struct pcb *next;
}PCB;
PCB head_input;
PCB head_run;
PCB *pcb_input;
static char R='r',C='c';
unsigned long current;
void inputprocess();
int readyprocess();
int readydata();
int runprocess();
FILE *f;
int readyprocess()
{
while(1)
{
if(readydata()==0)
return 1;
else
runprocess();
}
}
int readydata()
{
if(head_input.next==NULL)
{
if(head_run.next==NULL)
return 0;
else
return 1;
}
PCB *p1,*p2,*p3;
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{
p2=p1;
p1=p2->next;
}
p1=p2;
p3=head_input.next;
p2=&head_input;
while(p3!=NULL)
{
if(((unsigned long)p3->arrivetime<=current)&&(p3->state==R))
{
printf("Time slice is %8d(time%4d); Process %s start,\n",
current,(current+500)/1000,p3->pname);
fprintf(f,"Time slice is %8d(time%4d); Process %s start,\n",
current,(current+500)/1000,p3->pname);
p2->next=p3->next;
p3->next=p1->next;
p1->next=p3;
p3=p2;
}
p2=p3;
p3=p3->next;
}
return 1;
}
int runprocess()
{
PCB *p1,*p2;
if(head_run.next==NULL)
{
current++;
return 1;
}
else
{
p1=head_run.next;
p2=&head_run;
while(p1!=NULL)
{
p1->runtime--;
current++;
if(p1->runtime<=0)
{
printf("Time slice is %8d time%4d; Process %s end.\n",
current,(current+500)/1000,p1->pname);
fprintf(f,"Time slice is %8d time%4d; Process %s end.\n",
current,(current+500)/1000,p1->pname);
p1->state=C;
p2->next=p1->next;
delete p1;
p1=NULL;
}
else
{
p2=p1;
p1=p2->next;
}
}
return 1;
}
}
void inputprocess()
{
PCB *p1,*p2;
int num;
unsigned long max=0;
printf("How many process do you want to run:");
fprintf(f,"How many process do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",&num);
p1=&head_input;
p2=p1;
p1->next=new PCB;
p1=p1->next;
for(int i=0;i<num;i++)
{
printf("No. %3d process input pname:",i+1);
fprintf(f,"No. %3d process input pname:",i+1);
scanf("%s",p1->pname);
fprintf(f,"%s\n",p1->pname);
printf(" runtime:");
fprintf(f," runtime:");
scanf("%d",&(p1->runtime));
fprintf(f,"%d\n",&(p1->runtime));
printf(" arrivetime:");
fprintf(f," arrivetime:");
scanf("%d",&(p1->arrivetime));
fprintf(f,"%d\n",&(p1->arrivetime));
p1->runtime=(p1->runtime)*1000;
p1->arrivetime=(p1->arrivetime)*1000;
p1->state=R;
if((unsigned long)(p1->arrivetime)>max)
max=p1->arrivetime;
p1->next=new PCB;
p2=p1;
p1=p1->next;
}
delete p1;
p1=NULL;
p2->next=NULL;
}
void main()
{
f=fopen("result.txt","w");
printf("\ntime1=1000 time slice\n");
fprintf(f,"\ntime1=1000 time slice\n");
current=0;
inputprocess();
readyprocess();
getch();
fclose(f);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -