当前位置: 技术问答>linux和unix
大家帮我看看,为什么我用有名管道,两个程序互发,发的发的就不发了,好像堵塞似的
来源: 互联网 发布时间:2016-02-24
本文导语: 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 serv...
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)m_PipeName,O_RDWR, 0 );
// dummyfd = open( pProcessCom->m_PipeName,O_WRONLY, 0 );
while(1)
{
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 )
{
CLogManager *pLogMgr = NULL;
CModuleLog *pLog = NULL;
Aa* pProCom = ( Aa* )(arg);
pthread_detach(pthread_self());
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;
}
{
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)m_PipeName,O_RDWR, 0 );
// dummyfd = open( pProcessCom->m_PipeName,O_WRONLY, 0 );
while(1)
{
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 )
{
CLogManager *pLogMgr = NULL;
CModuleLog *pLog = NULL;
Aa* pProCom = ( Aa* )(arg);
pthread_detach(pthread_self());
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;
}
|
Mark