cceczjxy 大侠,看看我的有名管道程序,急啊

xiyupiaopiao 2007-12-12 09:29:41
一个有名管道通信类,两个进程派生于这个类,互相对发,刚开始好好的,但过一段时间就不发了,怎么回事,万分着急
Aa::Aa(void)
{
init();
}
Aa::~Aa()
{
m_bStop = true;
sem_destroy(&m_semFull);
sem_destroy(&m_semEmpty);
}

bool Aa::init()
{
sem_init(&m_semFull, 0, 0);
sem_init(&m_semEmpty, 0, 1);
// add end
// Create a thread to serve each pipe instance
//sleep(1);
int ret= pthread_create( &fn, NULL, thr_rev, (void*)this );
if(ret!=0)
{

return false;
}
else
{

}
ret= pthread_create( &fn, NULL, thr_snd,(void*)this);
if(ret != 0)
{

return false;
}
else
{

}
return true;
}
void * Aa::thr_rev( void *arg )
{

int readfifo,writefifo,readfd,writefd, dummyfd;

if((mkfifo(”abcd”,O_CREAT ¦0666) <0))
{
return NULL;
}


readfd = open( pProcessCom-> m_PipeName,O_RDWR, 0 );
// dummyfd = open( pProcessCom-> m_PipeName,O_WRONLY, 0 );
while(1)
{
readfd = open( pProcessCom-> m_PipeName,O_RDWR, 0 );

if(read( readfd, &recbuf, sizeof(recbuf.msgHead.iLength))> 0 )
{
if( read( readfd, &(recbuf.msgHead.iMsgID), recbuf.msgHead.iLength+sizeof(int))> 0 )
{


}
memset( &recbuf ,0 , sizeof(sRecbuf) );
}
}
//close(readfd);
// unlink(pProcessCom-> m_PipeName);
return NULL;
}


bool Aa::send(sf_pid pid, int siCmd, const char* text, int length)
{

sem_wait( &m_semEmpty );

sprintf(m_pSndPara-> tempPipeName, "%s%da",m_pathName,pid);
sem_post( &m_semFull );
return true;
}


void * Aa::thr_snd( void *arg )
{

while( true )
{

sem_wait( &(pProCom-> m_semFull) );
printf("pipe is null!\n");

int writefd = open( pProCom-> m_pSndPara-> tempPipeName, O_WRONLY ¦O_NONBLOCK, 0 );

if( writefd == -1 )
{

sem_post( &(pProCom-> m_semFull) );
continue;
}


int ret = write( writefd,pTemp, pProCom-> m_pSndPara-> msg.msgHead.iLength + 2*sizeof(int));

close(writefd);
// sleep(1);
sem_post( &(pProCom-> m_semEmpty) );
}
return NULL;
}

...全文
104 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cceczjxy 2007-12-12
  • 打赏
  • 举报
回复
看了一下,感觉问题可能在这
int writefd = open( pProCom->m_pSndPara->tempPipeName, O_WRONLY|O_NONBLOCK, 0 );

根据你的send函数和sendByProcType感觉tempPipeName可能是变化的名字.这样就可能存在读写的管道不是同一个管道的情况.
另外,有可能是你消费着函数send.
还有就是bool CProcessCom::sendByProcType( PT_HANDLE iProcessType, int siCmd, char* text, int length )这个函数是在怎么回事?
在声明中没有它,但它调用了send(生产者)函数.

send函数怎么定义两遍呀?
再者,send是个public,如果是有外部来调用这个生产者函数,是不是外部的调用中止了?

我没用c++做过,了解不是太多.
建议你跟踪一下,看卡在那了.
xiyupiaopiao 2007-12-12
  • 打赏
  • 举报
回复
信号灯操作只在生产者当中使用,当把消息写到管道内后,就通知下一个写动作,消费者阻塞在哪儿,只要管道内有消息,它就处理
xiyupiaopiao 2007-12-12
  • 打赏
  • 举报
回复
你有邮箱或者QQ没啊,我把完整代码给你发过去
cceczjxy 2007-12-12
  • 打赏
  • 举报
回复
根据你贴出的,对象启动了两个线程,thr_rev和thr_send,在thr_send内有对信号量的操作,而在thr_rev内没有对应的操作,生成消费过程就被卡住了.

你贴的内容不全

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧