?? os.cpp
字號(hào):
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define N 20
static char R='r',C='c';
typedef struct pcb
{
char pname[N];
int runtime;
int arrivetime;
char state;
struct pcb * next;
}PCB;
//PCB *head_ready;
FILE *f;
void input();
void run();
PCB *head_ready=(PCB *)malloc(sizeof(pcb));
void input()
{
PCB *p1,*p2;
int num;
unsigned long max=0;
printf("How many processes do you want to run: ");
fprintf(f,"How many processes do you want to run:");
scanf("%d",&num);
fprintf(f,"%d\n",num);
p1=head_ready;
head_ready->next=NULL;
for(int i=0;i<num;i++)
{
p2=(PCB*)malloc(sizeof(pcb));
p2->next=NULL;
printf("No.%3d process input pname:",i+1);
fprintf(f,"No.%d process input pname:",i+1);
scanf("%s",p2->pname);
fprintf(f,"%s\n",p2->pname);
printf(" runtime:");
fprintf(f," runtime:");
scanf("%d",&(p2->runtime));
fprintf(f,"%d\n",p2->runtime);
printf(" arrivetime:");
fprintf(f," arrivetime:");
scanf("%d",&(p2->arrivetime));
fprintf(f,"%d\n",p2->arrivetime);
p2->state=R;
fprintf(f," state: %c\n",p2->state);
while(p1)
{
if(!p1->next) {p1->next=p2; break;}
else
{
if(p2->arrivetime<p1->next->arrivetime) {p2->next=p1->next;p1->next=p2;goto K;}
else p1=p1->next;
}
}
K:;
} //for
} //input
void run()
{
PCB *p1,*p2;
int currenttime=0;
while((head_ready->next)!=NULL)
{
p1=head_ready->next;
p1->runtime -=1;
if(currenttime<p1->arrivetime) currenttime=p1->arrivetime;
printf("current time is %3d : process %s start.\n",currenttime,p1->pname);
p2=p1->next;
if(p2)
{
printf("還在在就緒隊(duì)列中的進(jìn)程:\n");
fprintf(f,"還在在就緒隊(duì)列中的進(jìn)程:\n");
}
while(p2)
{
printf("%s\n",p2->pname);
fprintf(f,"%s\n",&(p2->pname));
p2=p2->next;
}
fprintf(f,"current time is %3d : process %s start.\n",currenttime,p1->pname);
currenttime +=p1->runtime;
printf("current time is %3d : process %s end.\n",currenttime,p1->pname);
fprintf(f,"current time is %3d : process %s end.\n",currenttime,p1->pname);
head_ready->next=p1->next;
free(p1);
}
free(head_ready);
}
int main()
{
f=fopen("result.txt","w");
input();
run();
fclose(f);
return 0;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -