#include <string.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
/*
* Copyright (C) 2013 yangguo <yangguo@yangguo-ubuntu 12.4LTS>
*******************************************************************
* project name:
* Function:
*******************************************************************
* created time:2013-08-19-11.15
* company:dbc.com.ltd
* version:1.0
*******************************************************************
* support email:guoyang2011@gmail.com
* main function:
* modify information:None
*******************************************************************
*/
#define MAX_FREEDATANUM 50
//条件变量+互斥锁
struct BaseWriteData
{
struct BaseWriteData *next;
};
struct BaseReadData
{
struct BaseReadData *next;
};
struct BaseWritePool
{
struct BaseWriteData *header;
struct BaseWriteData *freeHeader;
int freeCondition;
int condition;
int sockfd;
int msgLength;
pthread_cond_t cond;
pthread_mutex_t mutex;
};
struct BaseReadPool
{
struct BaseReadData *header;
struct BaseReadData *freeHeader;
int freeCondition;
int condition;
int sockfd;
int msgLength;
pthread_cond_t cond;
pthread_mutex_t mutex;
};
void WritePool_Init(struct BaseWritePool *writePool)
{
writePool->msgLength=sizeof(struct BaseWriteData);
writePool->sockfd=-1;
writePool->header=NULL;
writePool->freeHeader=NULL;
writePool->freeCondition=0;
writePool->condition=0;
pthread_mutexattr_t mutexattr;
pthread_condattr_t condattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&writePool->mutex,&mutexattr);
pthread_condattr_init(&condattr);
pthread_condattr_setpshared(&condattr,PTHREAD_PROCESS_SHARED);
pthread_cond_init(&writePool->cond,&condattr);
}
void ReadPool_Init(struct BaseReadPool *readPool)
{
readPool->msgLength=sizeof(struct BaseReadData);
readPool->sockfd=-1;
readPool->header=NULL;
readPool->freeHeader=NULL;
readPool->freeCondition=0;
readPool->condition=0;
pthread_mutexattr_t mutexattr;
pthread_condattr_t condattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&readPool->mutex,&mutexattr);
pthread_condattr_init(&condattr);
pthread_condattr_setpshared(&condattr,PTHREAD_PROCESS_SHARED);
pthread_cond_init(&readPool->cond,&condattr);
}
struct BaseWriteData *WriteMallocResource(struct BaseWritePool *writePool)
{
if(writePool->freeCondition<=0)
{
return ((struct BaseWriteData *)malloc(sizeof(struct BaseWriteData)));
}
else
{
struct BaseWriteData *result=writePool->freeHeader;
writePool->freeHeader=writePool->freeHeader->next;
writePool->freeCondition--;
return result;
}
}
struct BaseReadData *ReadMallocResource(struct BaseReadPool *readPool)
{
if(readPool->freeCondition<=0)
{
return ((struct BaseReadData *)malloc(sizeof(struct BaseReadData)));
}
else
{
struct BaseReadData *result=readPool->freeHeader;
readPool->freeHeader=readPool->freeHeader->next;
readPool->freeCondition--;
return result;
}
}
void WriteFreeResource(struct BaseWritePool *writePool,struct BaseWriteData *tempData)
{
if(writePool->freeCondition>MAX_FREEDATANUM)
{
free(tempData);
}
else
{
writePool->freeCondition++;
tempData->next=writePool->freeHeader;
writePool->freeHeader=tempData;
}
}
void ReadFreeResource(struct BaseReadPool *readPool,struct BaseReadData *tempData)
{
if(readPool->freeCondition>MAX_FREEDATANUM)
{
free(tempData);
}
else
{
readPool->freeCondition++;
tempData->next=readPool->freeHeader;
readPool->freeHeader=tempData;
}
}
void *WriteThreadRun(void *WritePool)
{
int i;
struct BaseWriteData *writePtr;
struct BaseWritePool *writePool=(struct BaseWritePool *)WritePool;
for(;;)
{
pthread_mutex_lock(&(writePool->mutex));
while(writePool->condition==0)
{
pthread_cond_wait(&(writePool->cond),&(writePool->mutex));
}
//获取所有待传输数据
for(i=writePool->condition;i>0;i--)
{
writePtr=writePool->header;
writePool->header=writePool->header->next;
if(writePtr!=NULL)
{
send(writePool->sockfd,writePtr,sizeof(struct BaseWriteData),0);
WriteFreeResource(writePool,writePtr);
}
}
writePool->condition=0;
pthread_mutex_unlock(&(writePool->mutex));
}
}
void *ReadThreadRun(void *ReadPool)
{
int readNum=-1;
struct BaseReadData tempData,*readPtr;
struct BaseReadPool *readPool=(struct BaseReadPool *)ReadPool;
printf("read thread starting!\n");
for(;;)
{
//tempdata=(struct BaseReadData *)malloc(sizeof(struct BaseReadData));
readNum=recv(readPool->sockfd,&tempData,readPool->msgLength,0);
if(readNum>0)
{
pthread_mutex_lock(&readPool->mutex);
readPtr=ReadMallocResource(readPool);
if(memcpy(readPtr,&tempData,sizeof(struct BaseReadData))!=NULL)
{
readPool->condition++;
readPtr->next=readPool->header;
readPool->header=readPtr;
}
pthread_mutex_unlock(&readPool->mutex);
pthread_cond_signal(&readPool->cond);
}
}
pthread_exit(0);
}
int main(void)
{
int i;
pthread_t *tid=(pthread_t *)malloc(sizeof(pthread_t)*2);
// pid_t rpid;
int clientSockfd=-1;
int serverListenPort=23231;
char *serverAddr="192.168.66.111";
struct sockaddr_in serverSockAddr;
struct BaseReadPool *readPool=(struct BaseReadPool *)malloc(sizeof(struct BaseReadPool));
struct BaseWritePool *writePool=(struct BaseWritePool *)malloc(sizeof(struct BaseWritePool));
WritePool_Init(writePool);
ReadPool_Init(readPool);
bzero(&(serverSockAddr.sin_zero),0);
serverSockAddr.sin_family=AF_INET;
serverSockAddr.sin_port=htons(serverListenPort);
serverSockAddr.sin_addr.s_addr=inet_addr(serverAddr);
clientSockfd=socket(AF_INET,SOCK_STREAM,0);
if(clientSockfd==-1)
{
printf("create connection is failed!\n");
return 0;
}
if(connect(clientSockfd,(struct sockaddr *)&serverSockAddr,sizeof(struct sockaddr))==-1)
{
printf("connection server is failed!error reason is %s\n",strerror(errno));
return -2;
}
if(pthread_create(tid,NULL,ReadThreadRun,readPool)!=0)
{
printf("create read Thread failed!error reason is :%s\n",strerror(errno));
}
if(pthread_create(tid+1,NULL,WriteThreadRun,writePool)!=0)
{
printf("create write Thread failed! error reason is :%s\n",strerror(errno));
}
for(i=0;i<2;i++)
{
pthread_join(tid[i],NULL);
}
free(tid);
close(readPool->sockfd);
close(writePool->sockfd);
free(readPool);
free(writePool);
/*
rpid=fork();
if(rpid)
{
//作为ReadPool链表生产者
printf("child read pipe process starting!\n");
wait(NULL);
}
else
{
//作为WritePool链表消费者,当cond大于0时写入数据
printf("parent write pipe process starting!\n");
exit(0);
}
*/
exit(0);
}
interProcessCommunication client Communication Model
最新推荐文章于 2025-06-21 10:02:36 发布