# 求大神解答程序中的疑惑

0

void *prochase()
{
//sleep(5);
sem_wait(&full_sem);           //获取信号量
int n=0,l=0,p=0;
j=(rand()%30)+1;
if(count_rnd==0)
{
a[m]=j;
m++;
}
else
{
while(n<m)
{
if(a[n]!=j)
n++;
else
{
j=(rand()%30)+1;
n=0;
}
}
}
a[m]=j;
m++;
if(consumer_num%2!=0)          //奇数号消费者
{
if(consumer_num==29)   //所有奇数号消费者都消费之后，1，3，5，7，9号产品从缓冲区撤销
{
buffer[0]=' ';
buffer[2]=' ';
buffer[4]=' ';
buffer[6]=' ';
buffer[8]=' ';
}
if(j==1||j==3||j==5||j==7||j==9)   //1，3，5，7，9号生产者供奇数号消费者消费
{
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
}
else if((j>1&&j<11&&j%2==0)||(j>10&&j<21&&consumer_num!=j))
{
printf("conusmer %d can not consum producer %d.\n",consumer_num,j);
j=(rand()%30)+1;
printf("fffffffffffffffffffff   %d\n",j);
while((j>1&&j<11&&j%2==0)||(j>10&&j<21&&consumer_num!=j))
{
j=(rand()%30)+1;
}
printf("hhhhhhhhhhhhhhhhhhhhhhhh   %d\n",j);
while(l<m)
{
if(a[l]!=j)
l++;
else
{
j=(rand()%30)+1;
l=0;
}
}
printf("aaaaaaaaaaaaaaaaaaa    %d\n",j);
a[m]=j;
m++;
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
else if(j>10&&j<21&&consumer_num==j)//11-20号生产者只供对应编号相同的消费者消费
{
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
else   //其他任意编号的生产者被随意消费
{
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
}
else   //偶数号消费者
{
if(j==2||j==4||j==6||j==8||j==10)              //2，4，6，8，10号生产者被所有偶数号消费者消费
{
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
else if((j>0&&j<10&&j%2!=0)||(j>10&&j<21&&consumer_num!=j))
{
printf("consumer %d can not consum producer %d.\n",consumer_num,j);
j=(rand()%30)+1;
printf("kkkkkkkkkkkkkkkkkkkkkkkkk   %d\n",j);
while((j>0&&j<10&&j%2!=0)||(j>10&&j<21&&consumer_num!=j))
{
j=(rand()%30)+1;
}
printf("nnnnnnnnnnnnnnnnnnnnn     %d\n",j);
while(p<m)
{
if(a[p]!=j)
p++;
else
{
j=(rand()%30)+1;
p=0;
}
}
printf("eeeeeeeeeeeeeeeeee   %d\n",j);
a[m]=j;
m++;
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
else if(j>10&&j<21&&consumer_num==j)           //11-20编号的生产者供对应编号相同的消费者消费
{
printf("consumer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
else
{
printf("conusmer %d consum %c of producer %d\n",consumer_num,buffer[j-1],j);
buffer[j-1]=' ';
}
}
print();
out++;
out=out%M;
consumer_num++;
count_rnd++;
sem_post(&empty_sem);
}

0

Albert Fei | 园豆：2102 (老鸟四级) | 2013-12-30 10:10

您需要登录以后才能回答，未注册用户请先注册