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

📄 process.c

📁 本代码为C编写的进程管理。基本能够实现操作系统中进程的管理。
💻 C
字号:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>      //为下面随机函数产生基准 
#include<conio.h>
#define null 0
#define ID  15      //最大进程号,根据需要可以修改
FILE * f;
int flag=1;
/*保存现场部分*/
typedef struct sce{
            int midresult;
            int times;
             }scen;
 
/*进程控制块*/
typedef struct pcb_type{
             char name[20];
             int pid;
             int (*fp)();
             scen scene;
             int priority;
             int status;/*4为未结束,5为结束*/
             struct pcb_type *next;
               }pcb;
/*按优先级入队列*/
pcb *inqueue(pcb *head,pcb *apcb)
{ pcb *p=head;
  while(p->next!=null)
  { if(p->next->priority<apcb->priority)
       break;
      p=p->next;
  }/*while*/
  if(p->next==null)
     p->next=apcb;
  else
      {
       apcb->next=p->next;
       p->next=apcb;
      }
  return head;
}/*inqueue*/


/*出队列*/
pcb *outqueue(pcb *head)
{
  pcb *p=head->next;
  head->next=p->next;
  p->next=null;
  return p;
}/*outqueue*/
  
/*判断队列是否为空*/
int checkqueue(pcb *head)
{
 if(head->next==null)
  return 0;
  else return 1;
}
 
float Random()      //此处用随机函数产生模拟时间片
{
  int a;
  a=rand();     //1000为种子
  //a=a/1000.0;
  return (a/1000);
}
/*为进程产生不重复的ID号*/
int Get_PID(void)
{
	int i,a[ID],n,j;
    srand(time(NULL));
    for (i=0;i<ID;i++)
	{
      n=rand()%ID+1;
      if(i==0)a[i]=n;
      for(j=0;j<i;j++)
	  {
	    if(a[j]==n){i--;break;}
	  }
     if(j==i)
	 {
		 a[i]=n;
		 return n;
	 }
	}
   return n; 
}

/*进程一*/
int process1(int *midresult,int *times)
{ int allTheTimes=3;
  int temp=*midresult;    //已用的时间片
  int n=0;
  int i=0;
  for(;i<allTheTimes-(*times);i++)
  {  temp++;
     n++;
   if(Random()>0.33)       //模拟时间片
   {
    *midresult=temp;
    *times=*times+n;
    return 4;         //未结束
   }
  }
  *midresult=temp;
  return 5;                //此进程结束
}
 
/*进程二*/
int process2(int *midresult,int *times)
{
  int allTheTimes=4;
  int temp=*midresult;
  int n=0;
  int i=0;
  for(;i<allTheTimes-(*times);i++)
     {
     temp++;
     n++;
       if(Random()<0.33||Random()>0.66)
          {
            *midresult=temp;
        *times=*times+n;
             return 4;
          }
     }
*midresult=temp;
return 5;
}
  
/*进程三*/
int process3(int *midresult,int *times)
{
  int allTheTimes=5;
  int temp=*midresult;
  int n=0;
  int i=0;
  for(;i<allTheTimes-(*times);i++)
     {
       temp++;
       n++;
       if(Random()<0.66)
          {
            *midresult=temp;
            *times=*times+n;
            return 4;
          }
      }
 *midresult=temp;
 return 5;
}
  
/*进程调度器*/
void schedule(pcb **head1,pcb **head2)
{ pcb *p,*r;
  pcb *temp_head;
  //检验就绪队列是否为空
  if(!checkqueue(*head1))
    {
        //空则交换两个优先级队列
        temp_head=*head1;
        *head1=*head2;
        *head2=temp_head;
        //如果仍为空,则退出
       if(!checkqueue(*head1))
	   {
           fprintf(f,"All processes are complete!");
           flag=0;
           return;
	   }
    }
      //以下为对队列的现状的显示
    r=*head1;
    fprintf(f,"\nhead1  ");
    while(r->next!=null)
    { 
		r=r->next;
	    fprintf(f,"PID_[%d]\t",r->pid);
        fprintf(f,"[%s]\t",r->name);
      
     }
    fprintf(f,"null\n");
    r=*head2;
    fprintf(f,"head2  ");
    while(r->next!=null)
    {
      r=r->next;
	  fprintf(f,"PID_[%d]\t",r->pid);
      fprintf(f,"[%s]\t",r->name);
	  
    }
    fprintf(f,"null\n");
  //以上为对队列的现状的显示
 
  //不为空,从高优先级队列队头取一个PCB
    p=outqueue(*head1);
 
  //检查进程的状态,恢复现场 
    if(p->status!=5)
	{  
	   
       fprintf(f,"%s",p->name);
       fprintf(f," is using CPU\t");
       p->status=p->fp(&(p->scene.midresult),&(p->scene.times));
	}
  //进程结束,打印结果
    if(p->status==5)
	{ 
		fprintf(f,"PID_[%d]\t",p->pid);
		fprintf(f,"%s",p->name);
        fprintf(f," is complete,the result is ");
        fprintf(f,"%d\n",p->scene.midresult);
      //释放进程控制块
       free(p);
       //结束本次调度
        return;
	}
  //进程未结束,入队列
	
    fprintf(f,"%s",p->name);
    fprintf(f," is in queue2");
    inqueue(*head2,p);
 
 //以下为对队列的现状的显示
    r=*head1;
    fprintf(f,"\nhead1  ");
    while(r->next!=null)
	{ 
		r=r->next;
		fprintf(f,"PID_[%d]\t",r->pid);
        fprintf(f,"[%s]\t",r->name);
     }
    fprintf(f,"null\n");
    r=*head2;
    fprintf(f,"head2  ");
    while(r->next!=null)
    {
        r=r->next;
		fprintf(f,"PID_[%d]\t",r->pid);
        fprintf(f,"[%s]\t",r->name);
    }
    fprintf(f,"null\n");
  //以上为对队列的现状的显示

}/*schedule*/
 

/*初始化进程控制块*/
void init_process(pcb *apcb)
{
  apcb->scene.midresult=0;
  apcb->scene.times=0;
  //-------设置进程名
  printf("Please give a name for the process:");
  fflush(stdin);
  gets(apcb->name);
  //-------设置进程ID号
  apcb->pid=Get_PID();
  //-------设置进程优先级
  printf("Please give ");
  printf("%s",apcb->name);
  printf(" a priority: ");
  fflush(stdin);
  scanf("%d",&apcb->priority);
  //-------初始化进程状态
  apcb->status=4;
  apcb->next=null;
}/*init_process*/


/*主函数*/
void main()
{ 
  pcb *head1=(pcb *)malloc(sizeof(pcb));
  pcb *head2=(pcb *)malloc(sizeof(pcb));
  pcb *p1=(pcb *)malloc(sizeof(pcb));
  pcb *p2=(pcb *)malloc(sizeof(pcb));
  pcb *p3=(pcb *)malloc(sizeof(pcb));
  f=fopen("a.txt","w");        //a文本为进程日志
  head1->next=null;
  head2->next=null;
  p1->fp=process1;
  p2->fp=process2;
  p3->fp=process3;
  init_process(p1);
  init_process(p2);
  init_process(p3);
  head1=inqueue(head1,p1);
  head1=inqueue(head1,p2);
  head1=inqueue(head1,p3);
  while(flag)
     {
       fprintf(f,"\nBEGIN");
       schedule(&head1,&head2);//调度
       fprintf(f,"END\n");
     }
  fclose(f);
  getch();
}/*main*/ 

⌨️ 快捷键说明

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