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

📄 p&&c.c

📁 消费代码.用于操作系统中的课程设计使用.愿大家提供建议我也好改进算法
💻 C
字号:
#define M 15
#define N 4           /* the size of buffer */
struct record
  {char who,how;
   int array[N],result;}record[M];
int buffer[N],in=0,out=0,empty=4,full=0;  /*  define buffer and paramater,'in' is urrent posation */
int doing, L[6],H,T=0,l=0;           /*  define block queues and paramater */
int input[6],i=0,p;              /*  current process 'p'and posation 'i' */
int j=0,mutex=1;                       /*  the current posation of record */
wakeup()                  /*  ...need to carry out...? */
 {p=L[H];  H=(H+1)%6;}
block()                   /*  'n' is a process */
 {L[l]=p;  l=(l+1)%6;
  mutex++; }
int wait1()               /* wait(mutex) */
 { if(mutex==1)
    {mutex--;
     return 1;}
   return 0; }
signal1()            /* signal(mutex) */
 { mutex++; }
int wait2()            /* wait(empty/full) */
 {if(p)
   { if(T==0&&empty>0)   /* lest to get space repeatly */
      {empty--;
       return 1;}
     else if(T==2)
       return 1;
     else
      {record[j].who='P';
       record[j].how='F';
       record[j].result=0;
       copy();
       block(); return 0;}  }
  else
   { if(full>0)
      {full--; return 1;}
     else
      {record[j].who='C';
       record[j].how='E';
       record[j].result=0;
       copy();
       block(); return 0;} }
  }
signal2()               /* wait(empty/full) */
 {if(p)
   {full++;
     if(full>=N)
       wakeup(); }
  else
   {empty++;
     if(empty>=N)
	wakeup(); }
     }
int producter()
 { if(!wait1())            /* wait(mutex) */
     { record[j].who='P';
       record[j].how='W';
       record[j].result=0;
       copy();
       p=doing; doing=0;
       j++; i--; T++;}             /* because p have had a space at the first time*/
   if(wait2(p)==1)           /* wait(empty) */
      {  record[j].who='P';
	 record[j].how='Y';
	 if(T==2)               /* last times */
	   {buffer[in]=1;
	    record[j].result=buffer[in];
	    copy();
	    in=(in+1)%N;    T=0;
	    signal1(mutex);
	    signal2(full);    }         /* signal(full) */
	  else                         /* first time */
	   { record[j].result=-1;
	     copy();
		 T++;
	     doing=1;  } }
   j++; }
copy()
 {int k;
  for(k=0;k<N;k++)
   record[j].array[k]=buffer[k]; }
consumer()
  { if(wait2(p)==1)      /* There is wait(mutex) in check; here is wait( full) */
     {record[j].who='C';
      record[j].how='Y';
      buffer[out]=2;
      record[j].result=buffer[out];
      copy();
      out=(out+1)%N;
      signal1(mutex);
      signal2(empty);  }
     j++;  }
print()
 {int k1,k2;
  for(k1=0;k1<M;k1++)
   {printf("%2c%4c%6d    ", record[k1].who,record[k1].how,record[k1].result);
      for(k2=0;k2<N;k2++)
	printf("%d,",record[k1].array[k2]);
   printf("\n"); }
}
check()
 { if(!wait1(mutex))
   {record[j].who='C';
    record[j].how='W';
    record[j].result=0;
    copy();
    p=doing; doing=0;
    i--;   j++;
    mutex=1;  T=2;
    producter();}
   else consumer(); }
main()
 {int k,t;
  printf("input data:\n");
  for(k=0;k<6;k++)
   scanf("%d",&input[k]);
  printf("input times:\n");
  scanf("%d",&t);
  while(i<t)
  { p=input[i];
    if(!p)
      check();
    else if(p==1)
      producter(); i++; }
  printf("who how result   array \n");
  print();
 }

⌨️ 快捷键说明

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