生产者和消费者问题,共享多个缓冲区。
生产者3个
消费者3个
缓冲区3个

具体程序如下:可是有很多错误,请大家指点下

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
//#include <sys/ipc.h>
//#include <sys/sem.h>
#include <linux/sem.h>
#include <errno.h>
#define SEMKEY 301
#define MUTEX

int semid;
int res;
int full=0;
int empty=3;
int mutex=1;

struct buf
{ int flg;  //flg=0表示此缓冲区为空,flg=1表示此缓冲区有值 
  int num; //num表示各轿车厂家生产的轿车编号
  char com[10];}buf[3];

void producer1();
void producer2();
void producer3();
void consumer1();
void consumer2();
void consumer3();

void p(int id,int semno,int kind)
{
#ifdef MUTEX    
struct sembuf sop;
sop.sem_num = semno;
sop.sem_op = -1;
sop.sem_flg = 0;
semop(id,&sop,kind);
//printf("%d pass\n",getpid()); 
#endif
}

void v(int id,int semno,int kind)
{
#ifdef MUTEX    
struct sembuf sop;
sop.sem_num = semno;
sop.sem_op = 1;
sop.sem_flg = 0;
semop(id,&sop,kind);
#endif
}

main()
{
    int i;
        union semun semopts;
    pthread_t  p1,p2,p3,c1,c2,c3;
    

    for(i=0;i<3;i++)
           { buf[i].flg=0;
             buf[i].num=-1;
           }
    
        semid = semget(IPC_PRIVATE,3,IPC_CREAT|IPC_EXCL|0666);
    if (semid<0){
        printf("%s\n",strerror(errno));
        return;
    }
    semopts.val = 1;
    res = semctl(semid,0,SETVAL,semopts);
    if (res<0) {
        printf("res=%d\n",res);
    return;
    }
        
    pthread_create(&p1,NULL,producer1,NULL);sleep(1);
    pthread_create(&p2,NULL,producer2,NULL);sleep(2);
        pthread_create(&p3,NULL,producer3,NULL);
        pthread_create(&c1,NULL,consumer1,NULL);sleep(2);
    pthread_create(&c2,NULL,consumer2,NULL);
        pthread_create(&c3,NULL,consumer3,NULL);

        pthread_join(p1,NULL);
    pthread_join(p2,NULL);
        pthread_join(p3,NULL);
    pthread_join(c1,NULL);
        pthread_join(c2,NULL);
    pthread_join(c3,NULL);
        semctl(semid,0,IPC_RMID,0);
}

void producer1()
{    int i,j;
    for(j=1;j<10;j++)
    {       printf("---sheng chan car1---%d\n",j);sleep(2);
            p(semid,2,empty);
            p(semid,0,mutex);
            for(i=0;i<3;i++)              //生产
            {  if(buf[i].flg==0)
               { buf[i].num=j;  //number的值为轿车的编号
                strcpy(buf[i].com,"car1");
                buf[i].flg=1;}
            }
            v(semid,0,mutex);
            v(semid,1,full);
     }
}

void producer2()
{    int i,j;
     for(j=1;j<10;j++)
     {      printf("---sheng chan car2---%d\n",j);
            p(semid,2,empty);
            p(semid,0,mutex);
            for(i=0;i<3;i++)              //生产
            {  if(buf[i].flg==0)
               { buf[i].num=j;  //number的值为轿车的编号
                strcpy(buf[i].com,"car2");
                buf[i].flg=1;}
            }
           v(semid,0,mutex);
           v(semid,1,full);
     }
}

void producer3()
{    int i,j;
     for(j=1;j<10;j++)
     {      printf("---sheng chan car3---%d\n",j);
            p(semid,2,empty);
            p(semid,0,mutex);
            for(i=0;i<3;i++)              //生产
            {  if(buf[i].flg==0)
               { buf[i].num=j;  //number的值为轿车的编号
                strcpy(buf[i].com,"car3");
                buf[i].flg=1;}
            } 
            v(semid,0,mutex);
            v(semid,1,full);
     }
}


void consumer1()
{    int i,j,k;
     char truck1[10];
     for(k=1;k<10;k++)
     {     p(semid,1,full);
           p(semid,0,mutex);
           for(i=0;i<3;i++)              //消费
           { if(buf[i].flg==1)
             { j=buf[i].num;  //number的值为轿车的编号
             strcpy(truck1,buf[i].com);
             buf[i].flg=0;}
           }
           v(semid,0,mutex);
           v(semid,2,empty);
           printf("---tuo yun truck1---: %s>>>%d\n",truck1,j);
      }
}

void consumer2()
{    int i,j,k;
     char truck2[10];
     for(k=1;k<10;k++)
     {     p(semid,1,full);
           p(semid,0,mutex);
           for(i=0;i<3;i++)              //消费
           { if(buf[i].flg==1)
             { j=buf[i].num;  //number的值为轿车的编号
             strcpy(truck2,buf[i].com);
             buf[i].flg=0;}
           }
           v(semid,0,mutex);
           v(semid,2,empty);
           printf("---tuo yun truck2---: %s>>>%d\n",truck2,j);
      }
}

void consumer3()
{    int i,j,k;
     char truck3[10];
     for(k=1;k<10;k++)
     {     p(semid,1,full);
           p(semid,0,mutex);
           for(i=0;i<3;i++)              //消费
           { if(buf[i].flg==1)
             { j=buf[i].num;  //number的值为轿车的编号
             strcpy(truck3,buf[i].com);
             buf[i].flg=0;}
           }
           v(semid,0,mutex);
           v(semid,2,empty);
           printf("---tuo yun truck3---: %s>>>%d\n",truck3,j);
     }
}