⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os.cpp

📁 模拟操作系统的先来先服务算法
💻 CPP
字号:
#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("还在在就绪队列中的进程:\n");
		   fprintf(f,"还在在就绪队列中的进程:\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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -