生产者消费者问题--进程并发性体现得不太好,请帮我看看...
在Qt3.0下进行的开发,主要代码如下:
#include <qmessagebox.h>
#include <qstring.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
/*
pv_slot函数用信号量实现生产者和消费者的pv操作.
*/
void sem::pvSlot()
{
int sem_set_id;
int rc,child_pid;
QString str;
sem_set_id=semget(290,6,IPC_CREAT|0600);
if(sem_set_id==-1) {QMessageBox::information( this, "error ",tr( "创建失败 "),QMessageBox::Ok);exit(-1); }
rc=semctl(sem_set_id,0,SETVAL,1); //设置互斥信号量初值
if(rc==-1) {QMessageBox::information( this, "error ",tr( "设置失败 "),QMessageBox::Ok);exit(-1);}
rc=semctl(sem_set_id,1,SETVAL,0); //设置满缓冲池初值
if(rc==-1) {QMessageBox::information( this, "error ",tr( "设置失败 "),QMessageBox::Ok); exit(-1);}
rc=semctl(sem_set_id,2,SETVAL,20); //设置空缓冲池初值
if(rc==-1) {QMessageBox::information( this, "error ", "设置失败 ",QMessageBox::Ok); exit(-1);}
rc=semctl(sem_set_id,3,SETVAL,1); //设置通知对方退出信号量的初值
if(rc==-1) {QMessageBox::information( this, "error ", "设置失败 ",QMessageBox::Ok); exit(-1);}
rc=semctl(sem_set_id,4,SETVAL,0); //设置生产产品数初值
if(rc==-1) {QMessageBox::information( this, "error ", "设置失败 ",QMessageBox::Ok); exit(-1);}
rc=semctl(sem_set_id,5,SETVAL,0); //设置消费产品数初值
if(rc==-1) {QMessageBox::information( this, "error ", "设置失败 ",QMessageBox::Ok); exit(-1);}
child_pid=fork(); //创建子进程
switch(child_pid){
/*
子进程--消费者.
*/
case -1:exit(-1);
case 0: while(semctl(sem_set_id,3,GETVAL)){
cwait(sem_set_id,1); //同步p操作
cwait(sem_set_id,0); //互斥p操作
if(!semctl(sem_set_id,3,GETVAL)) exit(0);
&