1. #include<sys/mman.h>    
  2. #include<sys/types.h>    
  3. #include<linux/sem.h>    
  4. #include<fcntl.h>    
  5. #include<unistd.h>    
  6. #include<stdio.h>    
  7. #include<errno.h>    
  8. #include<time.h>    
  9. #define MAXSEM 5    /*声明三个信号量ID*/     
  10. int fullid;     
  11. int emptyid;     
  12. int mutxid;        
  13. int main(){     
  14.     struct sembuf P,V;     
  15.     union semun arg;   /* 声明共享主存*/     
  16.     int *array;     
  17.     int *sum;     
  18.     int *set;     
  19.     int *get;   /*将array、sum、set、get映射到共享主存*/     
  20.     array=(int *)shmat(shmget(IPC_PRIVATE,sizeof(int)*MAXSEM,IPC_CREAT|0666),0,0);     
  21.     sum=(int *)shmat(shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666),0,0);     
  22.     set=(int *)shmat(shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666),0,0);     
  23.     get=(int *)shmat(shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0666),0,0);     
  24.     *get=0;   /*创建信号量fullid、emptyid和mutxid*/     
  25.       
  26.     fullid=semget(IPC_PRIVATE,0666|IPC_CREAT);     
  27.     emptyid=semget(IPC_PRIVATE,0666|IPC_CREAT);     
  28.     mutxid=semget(IPC_PRIVATE,0666|IPC_CREAT);      /*为信号量赋值*/     
  29.     arg.val=0;     
  30.     if(semctl(fullid,0,SETVAL,arg)==-1)   
  31.         perror("semctl setval error");     
  32.     arg.val=MAXSEM;     
  33.     if(semctl(emptyid,0,SETVAL,arg)==-1)   
  34.         perror("semctl setval error");     
  35.     arg.val=1;     
  36.     if(semctl(mutxid,0,SETVAL,arg)==-1)   
  37.         perror("semctl setval error");      /*初始化PV*/     
  38.     P.sem_num=0;     
  39.     P.sem_op=-1;     
  40.     P.sem_flg=SEM_UNDO;     
  41.     V.sem_num=0;     
  42.     V.sem_op=1;     
  43.     V.sem_flg=SEM_UNDO;      /*创建生产者进程*/     
  44.     if(fork()==0){      
  45.         int i=0;      
  46.         *set=0;      
  47.         while(i<100){       
  48.             semop(emptyid,&P,1);       
  49.             semop(mutxid,&P,1);//对emptyid、mutxid执行P操作            
  50.             array[(*set)%MAXSEM]=i+1;       
  51.             (*set)++;            
  52.             semop(mutxid,&V,1);         //对emptyid、mutxid执行V操作                
  53.             semop(fullid,&V,1);          //       
  54.             i++;    }      
  55.         sleep(3);      
  56.         printf("生产者结束\n");      
  57.         exit(0);      
  58.     }else{     /*创建消费者进程*/       
  59.         if(fork()==0){      while(1){         
  60.             semop(fullid,&P,1);        //对emptyid、mutxid执行P操作         
  61.             semop(mutxid,&P,1);       //           
  62.             if(*get==100)         
  63.                 break;         
  64.             *sum+=array[(*get)%MAXSEM];         
  65.             printf("The ConmsumerA Get Number %d\n",array[(*get)%MAXSEM]);         
  66.             (*get)++;         
  67.             if(get==100)         
  68.                 printf("The sum is %d\n",sum);         
  69.             semop(mutxid,&V,1);         
  70.             semop(emptyid,&V,1);         
  71.             sleep(1);             }        
  72.         printf("消费者结束\n");        
  73.         exit(0);      }else{     /*创建消费者进程B*/       
  74.             if(fork()==0){        
  75.                 while(1){         
  76.                     semop(fullid,&P,1);        //对emptyid、mutxid执行P操作         
  77.                     semop(mutxid,&P,1);               
  78.                     if(*get==100)            
  79.                         break;         
  80.                     *sum+=array[(*get)%MAXSEM];         
  81.                     printf("The ConmsumerB Get Number %d\n",array[(*get)%MAXSEM]);         
  82.                     (*get)++;         
  83.                     if(get==100)         
  84.                         printf("The sum is %d\n",sum);         
  85.                     semop(mutxid,&V,1);         
  86.                     semop(emptyid,&V,1);         
  87.                     sleep(1);             }        
  88.                 printf("消费者B结束!\n");        
  89.                 exit(0);      
  90.             }         
  91.         }     
  92.     }     
  93. }