cceczjxy 大侠,看看我的有名管道程序,急啊
一个有名管道通信类,两个进程派生于这个类,互相对发,刚开始好好的,但过一段时间就不发了,怎么回事,万分着急
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;
}