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

📄 1.c

📁 这是一个模拟调度程序
💻 C
字号:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/stat.h>//include<fcntl.h>
#define MAX 5
#define MAXSIZE 10struct pcb * creat();struct pcb *insert(struct pcb *head,struct pcb *newpcb);
struct pcb{
  pid_t pid;
  char * args[MAXSIZE];
  int arrivaltime;
  int remainingcputime;
  struct pcb * next;
};

struct test
{
  int arrivaltime;
  int remainingcputime;
}testdata[MAX];

#define SIZE 8;
char *delim=" \n";

int main(int argc,char *argv[])
{
  FILE *fp;
  int i=0;    
  int j=0;
  int id=0;
  int counter=0;
  int flag=0;                        //running 1,no running 0
  int timer=0;
  int rrqueueflag=0;  char  buffer[80];
  char* Area[16];
  struct pcb *head,*p,*rrqueue,*q;  rrqueue=NULL;
  if(argc !=2 )
  {
	  printf("Usage : ./3 filename \n");
	  return 1;
  }
  fp=fopen(argv[1],"r+");
  if(fp==NULL)
  {
	  printf("Open file failure!\n");
	  return 1;
  }
  while(fgets(buffer,80,fp)!=NULL)
  {
      ++counter;
      printf("The %d line:%s",counter,buffer);
      Area[i]=strtok(buffer,delim); 
     while(Area[i]!=NULL)
      Area[++i]=strtok(NULL,delim);
     
     if(j<MAX)
      {
      testdata[j].arrivaltime=atoi(Area[0]);
      testdata[j].remainingcputime=atoi(Area[2]);
      }
     else return ;
    j++;
    i=0;
    memset(Area,0,16);
  }
  printf("\n");
  for(i=0;i<MAX;i++)
  { 
     printf("%d:\t%d , %d\n",i,testdata[i].arrivaltime,testdata[i].remainingcputime);
  }
  fclose(fp);
  head=creat();  p=head;  printf("OK!\n");  counter=0;
  while(head!=NULL||rrqueue!=NULL)
  {             printf("%d,:  \n",timer);          if(head!=NULL) 	  {	  if(head->arrivaltime<=timer)      
	  {
          if(rrqueue==NULL)  //调度队列为空
		{rrqueue=head;head=head->next;rrqueue->next=NULL;}
          else     //调度队列不为空
		{
           p=rrqueue;
           while(p->next!=NULL)    //p指向调度队列的队尾
               p=p->next;   
           p->next=head;
           head=head->next; 
           p->next->next=NULL;	        }                   }	  }
          if(rrqueue!=NULL&&flag==1)   //有进程在运行,需要挂起或者释放
	  { 
            rrqueue->remainingcputime--;
            if(rrqueue->remainingcputime==0)    //队列cpu时间用完,因此释放
		{
		  kill(rrqueue->pid,SIGINT);
		  p=rrqueue;
		  rrqueue=rrqueue->next;                  p->next=NULL;
		  free(p);
		}
	    else
		{
		  kill(rrqueue->pid,SIGTSTP);
		  p=rrqueue;
                  if(rrqueue->next==NULL)   //调度队列只有一个,不必循环                    rrqueueflag=1;                  if(rrqueueflag==0)      //调度队列两个以上		  {                  while(p->next!=NULL)     //p指向rrqueue的队尾			  p=p->next;
		  q=rrqueue;          //q指向对首,即要放到最后执行
                  rrqueue=rrqueue->next;
		  p->next=q;                  q->next=NULL;
		  }
                }		flag=0;
                rrqueueflag=0;	  }




        if(rrqueue!=NULL&&flag==0) 
	{
           if(rrqueue->pid==0)
	   {
           id=fork();
           switch(id)
	   {
           case 0:
                  execvp(rrqueue->args[0],rrqueue->args); 
           case -1:
                  perror("fork");
	          break;
           default:                  rrqueue->pid=id;
	          break;
	    }
	    }
        else
          kill(rrqueue->pid,SIGCONT);
        flag=1;
	}	
      sleep(1);      timer++;
  }      //while的结束   return 0; }



struct pcb * creat()
{
	struct pcb *newpcb,*head;
	int i;
	head=NULL;
	for(i=0;i<MAX;i++)
	{
		newpcb=(struct pcb *)malloc(sizeof(struct pcb));
		newpcb->arrivaltime=testdata[i].arrivaltime;
		newpcb->remainingcputime=testdata[i].remainingcputime;
		newpcb->args[0]="./process";
		newpcb->args[1]=NULL;
		newpcb->next=NULL;
                newpcb->pid=0;
		head=insert(head,newpcb);
	}
	return(head);
}

struct pcb *insert(struct pcb *head,struct pcb *newpcb)
{
   struct pcb *p0,*p1,*p2;
   p1=head;
   p0=newpcb;
   if(head==NULL)
   {
	   head=p0;
	   p0->next=NULL;
   }
   else
   {
	   while((p0->arrivaltime>p1->arrivaltime)&&(p1->next!=NULL))
	   {
		   p2=p1;
		   p1=p1->next;
	   }
	   if(p0->arrivaltime<=p1->arrivaltime)
	   {
		   if(head==p1)
			   head=p0;
		   else
			   p2->next=p0;
		   p0->next=p1;
	   }
	   else
	   {
		   p1->next=p0;
		   p0->next=NULL;
	   }
   }
   return(head);
}
	   

⌨️ 快捷键说明

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