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

📄 czxt.cpp

📁 支持多个进程并发运行的简单进程管理模拟系统。本系统的同步机构采用的信号量上的P
💻 CPP
字号:
#include<iostream>
#include<stdio>
#define true 1
#define false 0
#define maxpri  100
#define nil-1
struct{
  int id;
  char status;
  int nextwr;
  int  priority;
  }pcb[3];
  struct{
     int value;
     int firstwr;
     }sem[2];
  char savearea[3][4],addr;
  int  i,s1,s2,seed,exe=nil;
  init ()  /*initialization*/
  {int j;
   for (j=0;j<3;j++)
     {pcb[j].id=j;
      pcb[j].status='r';
      pcb[j].nextwr=nil;
      printf("\n process%d priority?",j+1);
      scanf("%d",&i);
      pcb[j].priority=i;
      }
      sem[0].value=1;sem[0].firstwr=nil;
      sem[1].value=1;sem[1].firstwr=nil;
      for(i=1;i<3;i++)
       for(j=0;j<4;j++)
       savearea[i][j]='0';
       }
    float random()
    {int m;
    if (seed<0)m=--seed;
    else m=seed;
   seed=(25173*seed+13849)%65536;
    return(m/32767.0);
    }
    timeint(ad)  /*time slice interrupt*/
    char ad;
    {
      float x;
      x=random();
      if((x<0.33)&&(exe==0))return(false);
      if((x<0.66)&&(exe==1))return(false);
      if((x<1.0)&&(exe==2))return(false);
      savearea[exe][0]=i;
      savearea[exe][1]=ad;
      pcb[exe].status='t';
      printf("time silce interrupt'\n process%d enter inro ready.\n",exe+1);
      exe=nil;
      return(true);
      }
      scheduler()
      { int pd;
      if((pd=find())==nil&&exe==nil)
      return(nil); /*quit stystem*/
      if(pd!=nil){
       if(exe==nil){
	pcb[pd].status='e';
      exe=pd;
	  printf("process%d is executing.\n",exe+1);
	  }
	  else if(pcb[pd].priority<pcb[exe].priority)
	  {pcb[exe].status='r';
	  printf("process%d enter into read\n",exe+1);
	  pcb[pd].status='e';
	  exe=pd;
	  printf("process%d is executing\n",exe+1);
	  }
       }
       i=savearea[exe][0];
       addr=savearea[exe][1];
       return(exe);
       }
       find()
       { int j,pd=nil,w=maxpri;
	  for (j=0;j<3;j++)
	  if(pcb[j].status=='r')
	   if(pcb[j].priority<w){
	   w=pcb[j].priority; pd=j;
	   }
	   if(pd==nil)
	   for(j=0;j<3;j++)
	   if(pcb[j].status=='t')
	   if(pcb[j].priority<w){
	   w=pcb[j].priority;pd=j;
	   }
	   return(pd);
      }
      p(se,ad)
      int se;
      char ad;
      {if(--sem[se].value>=0) return(false);
      block(se);
      savearea[exe][0]=i;
      savearea[exe][1]=ad;
      exe=nil;
      return(true);
      }
      block(se)
      int se;
      {int w;
      printf("process%d is block\n",exe+1);
      pcb[exe].status='w';
      pcb[exe].nextwr=nil;
      if((w=sem[se].firstwr)==nil)
      sem[se].firstwr=exe;
      else{
       while(pcb[w].nextwr!=nil)
       w=pcb[w].nextwr;
       pcb[w].nextwr=exe;
       }
    }
    v(se,ad)
    int se;
    char ad;
    {
     if(++sem[se].value>0)  return(false);
     wakeup(se);
     savearea[exe][1]=ad;
     savearea[exe][0]=i;
     return(true);  /*scheduler*/
     }
     wakeup(se)
     int se;
     {
      int w;
      w=sem[se].firstwr;
      if(w!=nil){
       sem[se].firstwr=pcb[w].nextwr;
       pcb[w].status='r';
       printf("process%d is waken up\n",w+1);
       }
     }
  process1()
  {
    if(addr=='a')goto a1;
    if(addr=='b')goto b1;
    if(addr=='c')goto c1;
    if(addr=='d')goto d1;
    if(addr=='e')goto e1;
    if(addr=='f')goto f1;
    for(i=1;i<6;i++)
     {
       printf("process1 calls p on the semaphore 1\n");
       if(p(0,'a')) break; /*process 1 is blocked*/
       a1: printf("process1 is executing in the cretical section1\n");
       if(timeint('b')) break;
       b1: printf("s1%d\n",++s1);
       printf("process1 calls v on semaphore1 and quit cretical section 1.\n");
       if(v(0,'c')) break;
       c1: printf("process1 calls p on semaphore1 2.\n");
       if(p(1,'d')) break;
       d1: printf("process1 is executing cretical section 2.\n");
       if(timeint('e'))break;
       e1: printf("s2=%d\n",++s2);
       printf("process1 calls v on semephore2 and quit cretical section2.\n");
       if(v(1,'f')) break;
       f1:printf("process1 cycle count=%d\n",i);
       }
     if(i<6) return;
     eexit(0);
     }
  process2()
  {
    if(addr=='a')goto a2;
    if(addr=='b')goto b2;
    if(addr=='c')goto c2;
    if(addr=='d')goto d2;
    if(addr=='e')goto e2;
    if(addr=='f')goto f2;
    for(i=0;i<6;++i)
    {
     printf("process2 call pon semephore2\n");
     if(p(1,'a')) break;
     a2: printf("process2 is executing on the cretical section2\n");
	if(timeint('b')) break;
     b2: printf("s2=%d\n",++s2);
	 printf("process2 calls v on semephore2 and quit cretical section2.\n");
	 if(v(1,'c')) break;
     c2: printf("process2 calls p in semaphore1.\n");
	if(p(0,'d')) break;
    d2: printf("process2 calls p on semaphore1.\n");
       if(timeint('e')) break;
    e2:printf("s1=5d\n",++s1);
       printf("process2 v on semphore1 and quit cretical section1.\n");
       if(v(0,'f')) break ;
    f2: printf("process2 cycle count=%d\n",i);
       }
	if(i<6) return;
	eexit(1);
	}
    process3()
    {if(addr=='a')goto a3;
     if(addr=='b')goto b3;
     if(addr=='c')goto c3;
     for(i=1;i<6;i++)
     {
       printf("process3 calls p on semaphore2\n");
       if(p(1,'a'))break;
    a3: printf("process3 is executing ion the creticalsection \n");
       if(timeint('b'))break;
    b3: printf("s2=%d\n",++s2);
       printf("process3 calls v in semaphore2 and quit cretical section.\n");
       if(v(1,'c'))break;
   c3: printf("process3 cycle count=%d\n");
       }
       if(i<6) return;
       eexit(2);
       }
       eexit(n)
       int n;
       { pcb[n].status='c';
	printf("process%d is comleted !\n",n+1);
	exe=nil;
	}
	main()
	{ int k;
	 printf("****process management******\n\n");
	 init();
	 printf("s1=%d,s2=%d\n",s1,s2);
	 printf("process1,process2,process3 are all in ready1\n");
	 for( ; ;)
	 if((k=scheduler())!=nil)
	 switch(k){
	  case 0: process1();
	  break;
	  case 1: process2();
	  break;
	  case2: process3();
	  break;
	  default: printf("process identifer error\n");
	  break;
	  }
	  else break;
	  printf("s1=%d,s2=%d\n",s1,s2);
	  printf("\n *********end***********\n");
      }

⌨️ 快捷键说明

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