linux posix 读写锁+共享内存demo

读写锁主要用于数据读操作比写操作频繁,且数据的实时性要求不高的场景
使用gcc 编译的时候加上参数 -lrt -lpthread 

////////////////////////////////////////////////////server.c//////////////////////////////////////////////////////////////////////

#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SHMFILENAME "rwlockdata.d"
struct RDWRLockData
{
    pthread_rwlock_t rwLock;
    int currentIndex;
};
int main(void)
{
    int fd;
    pthread_rwlockattr_t rwLockAttr;
    struct RDWRLockData *sharedMemoryPtr;
    int sharedmemoryLength=0;
    sharedmemoryLength=sizeof(struct RDWRLockData);
    fd=shm_open(SHMFILENAME,O_RDWR|O_CREAT|O_TRUNC,0777);
    if(fd==-1)
    {
        printf("open shared memory failed!\n");
        printf("error reason is :%s!\n",strerror(errno));
        exit(0);
    }
    if(ftruncate(fd,sharedmemoryLength)!=0)
    {
        printf("set share memory size is failed! error reason is %s!\n",strerror(errno));
        exit(0);
    }
    sharedMemoryPtr=mmap(NULL,sharedmemoryLength,PROT_WRITE|PROT_READ,MAP_SHARED,fd,0);
    if(sharedMemoryPtr==MAP_FAILED)
    {
        printf("create share memory is failed!error reason is %s!\n",strerror(errno));
        exit(0);
    }
    close(fd);
    if(pthread_rwlockattr_init(&rwLockAttr)!=0)
    {
        printf("init rwlockattr attribute is failed ! erro reason is %s!\n",strerror(errno));
        exit(0);
    }
    if(pthread_rwlockattr_setpshared(&rwLockAttr,PTHREAD_PROCESS_SHARED)!=0)
    {
        printf("set rw lockattrite value is failed ! error reason is %s!\n",strerror(errno));
        exit(0);
    }
    if(pthread_rwlock_init(&sharedMemoryPtr->rwLock,&rwLockAttr)!=0)
    {
        printf("init rw lock  attribute is failed !error reason is %s\n",strerror(errno));
        exit(0);
    }
    int  maxMsgSize=20;
    int i=0;
    while(i     {
        pthread_rwlock_wrlock(&sharedMemoryPtr->rwLock);
        printf("server change message sharedMemoryPtr->currentIndex=%d!\n",++sharedMemoryPtr->currentIndex);
        pthread_rwlock_unlock(&sharedMemoryPtr->rwLock);
        i=sharedMemoryPtr->currentIndex;
        sleep(5);
    }
    if(pthread_rwlockattr_destroy(&rwLockAttr)!=0)
    {
        printf("destroy rw lock attr is failed!error reason is %s\n",strerror(errno));
        exit(2);
    }
    if(pthread_rwlock_destroy(&sharedMemoryPtr->rwLock)!=0)
    {
        printf("destroy rw lock  is failed!error reason is %s\n",strerror(errno));
        exit(2);
    }
    if(munmap(&sharedMemoryPtr,sharedmemoryLength)!=0)
    {
        printf("unmout share memory is failed!error reason is %s\n",strerror(errno));
        exit(2);
    }
    if(shm_unlink(SHMFILENAME)!=0)
    {
        printf("distroy share memory is failed!error reason is %s\n",strerror(errno));
        exit(2);
    }
    exit(3);

    return 0;

}

/////////////////////////////////////////////////////////////client.c/////////////////////////////////////////////////////////////////////////

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SHMFILENAME "rwlockdata.d"
struct RDWRLockData
{
    pthread_rwlock_t rwLock;
    int currentIndex;
};
int main(void)
{
    struct RDWRLockData *sharedMemoryData;
    int fd;
    fd=shm_open(SHMFILENAME,O_RDWR,0777);//注意权限
    if(fd==-1)
    {
        printf("open shared memory failed!\n");
        printf("error reason is :%s!\n",strerror(errno));
        exit(0);
    }
    sharedMemoryData=mmap(NULL,sizeof(struct RDWRLockData),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);//注意权限PROT_READ|PROT_WRITE
    if(sharedMemoryData==MAP_FAILED)
    {
        printf("create share memory is failed!error reason is %s!\n",strerror(errno));
        exit(0);
    }
    close(fd);
    int i=0;
    while(i<20)
    {
        pthread_rwlock_rdlock(&sharedMemoryData->rwLock);
        printf("read data is sharedMemoryData->currentIndex=%d\n",sharedMemoryData->currentIndex);
        pthread_rwlock_unlock(&sharedMemoryData->rwLock);
        i=sharedMemoryData->currentIndex;
        sleep(1);
    }

    exit(0);
    return 0;
}


你可能感兴趣的:(linux,IPC)