Linux system V IPC 信号灯和共享内存实例(2)

printf("The ConsumerA get number from No.%d\n",(*get)%MAXSHM);
               
                (*get)++;                   //读计数加1

semop(mutexid,&V,1);        //对mutexid执行 V 操作
                semop(emptyid,&V,1);        //对fullid执行 V 操作

sleep(1);
            }
           
            printf("ConsunerA is over\n");
            exit(0);
        }
        else
        {
            /*消费者B进程  */
            if(fork()==0)
            {
                while(1)
                {
                    if(*get==10)
                    {
                        break;
                    }

semop(fullid,&P,1);       //对fullid执行 P 操作
                    semop(mutexid,&P,1);      //对mutexid执行 P 操作

printf("The ConsumerB get number from No.%d\n",(*get)%MAXSHM);

(*get)++;                 //读计数加1

semop(mutexid,&V,1);      //对mutexid执行 V 操作
                    semop(emptyid,&V,1);      //对emptyid执行 V 操作
                   
                    sleep(1);
                }
               
                printf("ConsunerB is over\n");
                exit(0);
            }
        }
    }

/*   父进程返回后回收3个子进程  */
   
    wait(0);
    wait(0);
    wait(0);

/*  断开并撤消2个共享内存  */

shmdt(array);
    shmctl(arrayid,IPC_RMID,0);
    shmctl(get);
    shmctl(getid,IPC_RMID,0);
   
    /*   撤消3个信号量集  */

semctl(emptyid,IPC_RMID,0);
    semctl(fullid,IPC_RMID,0);
    semctl(mutexid,IPC_RMID,0);

exit(0);
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxjdf.html