linux 共享内存 读写锁,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; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值