首页 新闻 会员 周边 捐助

求大神解答程序中的疑惑

0
悬赏园豆:10 [待解决问题]

下面是我写的一个生产者和消费者程序中的消费者部分,我是想等生产者都生产完之后消费者再根据产生的不同的随机数去随机消费生产者,而这个随机的要求是1,3,5,7,9号生产者的产品能被所有奇数号的消费者消费,2,4,6,8,10被所有的偶数号消费者消费,11-20只能被对应编号相同的消费者消费,其他不做要求。

我的错误在于,当用if语句判断一个生产者不能被该消费者消费,而重新产生一个与前面都不同的随机数,可是运行的时候,运行一段时间之后就停止不动了,求大神告知该怎么办!

void *prochase()
{
    //sleep(5);
         sem_wait(&full_sem);           //获取信号量
         pthread_mutex_lock(&mutex);    //加锁
                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++;
         pthread_mutex_unlock(&mutex);
         sem_post(&empty_sem);
}

运行之后,有几个是没问题的,但是一段时间之后程序就停止不前了

尘_的主页 尘_ | 初学一级 | 园豆:185
提问于:2013-12-27 21:13
< >
分享
所有回答(1)
0

感觉太乱,好像是学生时代的代码

还是把各个功能分开写吧

Albert Fei | 园豆:2102 (老鸟四级) | 2013-12-30 10:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册