共享内存 - 读写锁

zhuyf87 2014-04-30 10:51:24
问题描述:整个项目采用多进程设计,多进程之间采用共享内存进行通信。通信进程会将采集数据更新到共享内存,其它多个进程都需要从共享内存中读取数据。这样的“一个进程写、多个进程读”的模型,需要采用读写锁来提高访问效率。请问大家用过这种进程间的读写锁吗?单个进程内的多个线程的读写锁比较容易,那多个进程如何操作同一个读写锁?类似于互斥量可以有一个名字参数,这样多个进程可以保证操作的是同一个互斥量。
...全文
410 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luotuo44 2014-04-30
  • 打赏
  • 举报
回复
《APUE》和《Windows核心编程》都没有说到可以用于进程间的读写锁
我还在迷路 2014-04-30
  • 打赏
  • 举报
回复
进来学习~~
shenchenman 2014-04-30
  • 打赏
  • 举报
回复
用一个唯一进程控制内存读写,需要资源的都同该进程交互。参考linux管道进程的实现
赵4老师 2014-04-30
  • 打赏
  • 举报
回复
_locking Locks or unlocks bytes of a file. int _locking( int handle, int mode, long nbytes ); Routine Required Header Optional Headers Compatibility _locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values: EACCES Locking violation (file already locked or unlocked). EBADF Invalid file handle. EDEADLOCK Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts. EINVAL An invalid argument was given to _locking. Parameters handle File handle mode Locking action to perform nbytes Number of bytes to lock Remarks The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file. mode must be one of the following manifest constants, which are defined in LOCKING.H: _LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error. _LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. _LK_NBRLCK Same as _LK_NBLCK. _LK_RLCK Same as _LK_LOCK. _LK_UNLCK Unlocks the specified bytes, which must have been previously locked. Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. Example /* LOCKING.C: This program opens a file with sharing. It locks * some bytes before reading them, then unlocks them. Note that the * program works correctly only if the file exists. */ #include <io.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/locking.h> #include <share.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, numread; char buffer[40]; /* Quit if can't open file or system doesn't * support sharing. */ fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ); if( fh == -1 ) exit( 1 ); /* Lock some bytes and read them. Then unlock. */ if( _locking( fh, LK_NBLCK, 30L ) != -1 ) { printf( "No one can change these bytes while I'm reading them\n" ); numread = _read( fh, buffer, 30 ); printf( "%d bytes read: %.30s\n", numread, buffer ); lseek( fh, 0L, SEEK_SET ); _locking( fh, LK_UNLCK, 30L ); printf( "Now I'm done. Do what you will with them\n" ); } else perror( "Locking failed\n" ); _close( fh ); } Output No one can change these bytes while I'm reading them 30 bytes read: /* LOCKING.C: This program ope Now I'm done. Do what you will with them File Handling Routines See Also _creat, _open
tbwork 2014-04-30
  • 打赏
  • 举报
回复
之前写驱动的时候用到过共享内存,是把系统内存和自己的程序关联起来。 大致步骤是,好像有个Map函数,把某个共享内存映射到不同进程的自己的空间中。 至于读写锁,应该可以自己控制。 如果只有一个写进程,写进程在开始写时,就把该段内存的权限设置为只写,不允许读,读可以多个一起读。 如果有多个写进程,我抛砖引玉下吧,可行度不一定高: 可以设置一个唯一逻辑写进程,其他的写进程全部发送给这个写进程,以队列的形式,进行写。
本PDF电子书包含上下两册,共1576页,带目录,高清非扫描版本。 作者: 毛德操 胡希明 丛书名: Linux内核源代码情景分析 出版社:浙江大学出版社 目录 第1章 预备知识 1.1 Linux内核简介. 1.2 Intel X86 CPU系列的寻址方式 1.3 i386的页式内存管理机制 1.4 Linux内核源代码中的C语言代码 1.5 Linux内核源代码中的汇编语言代码 第2章 存储管理 2.1 Linux内存管理的基本框架 2.2 地址映射的全过程 2.3 几个重要的数据结构和函数 2.4 越界访问 2.5 用户堆栈的扩展 2.6 物理页面的使用和周转 2.7 物理页面的分配 2.8 页面的定期换出 2.9 页面的换入 2.10 内核缓冲区的管理 2.11 外部设备存储空间的地址映射 2.12 系统调用brk() 2.13 系统调用mmap() 第3章 中断、异常和系统调用 3.1 X86 CPU对中断的硬件支持 3.2 中断向量表IDT的初始化 3.3 中断请求队列的初始化 3.4 中断的响应和服务 3.5 软中断与Bottom Half 3.6 页面异常的进入和返回 3.7 时钟中断 3.8 系统调用 3.9 系统调用号与跳转表 第4章 进程与进程调度 4.1 进程四要素 4.2 进程三部曲:创建、执行与消亡 4.3 系统调用fork()、vfork()与clone() 4.4 系统调用execve() 4.5 系统调用exit()与wait4() 4.6 进程的调度与切换 4.7 强制性调度 4.8 系统调用nanosleep()和pause() 4.9 内核中的互斥操作 第5章 文件系统 5.1 概述 5.2 从路径名到目标节点 5.3 访问权限与文件安全性 5.4 文件系统的安装和拆卸 5.5 文件的打开与关闭 5.6 文件的写与读 5.7 其他文件操作 5.8 特殊文件系统/proc 第6章 传统的Unix进程间通信 6.1 概述 6.2 管道和系统调用pipe() 6.3 命名管道 6.4 信号 6.5 系统调用ptrace()和进程跟踪 6.6 报文传递 6.7 共享内存 6.8 信号量 第7章基于socket的进程间通信 7.1系统调用socket() 7.2函数sys—socket()——创建插口 7.3函数sys—bind()——指定插口地址 7.4函数sys—listen()——设定server插口 7.5函数sys—accept()——接受连接请求 7.6函数sys—connect()——请求连接 7.7报文的接收与发送 7.8插口的关闭 7.9其他 第8章设备驱动 8.1概述 8.2系统调用mknod() 8.3可安装模块 8.4PCI总线 8.5块设备的驱动 8.6字符设备驱动概述 8.7终端设备与汉字信息处理 8.8控制台的驱动 8.9通用串行外部总线USB 8.10系统调用select()以及异步输入/输出 8.11设备文件系统devfs 第9章多处理器SMP系统结构 9.1概述 9.2SMP结构中的互斥问题 9.3高速缓存与内存的一致性 9.4SMP结构中的中断机制 9.5SMP结构中的进程调度 9.6SMP系统的引导 第10章系统引导和初始化 10.1系统引导过程概述 10.2系统初始化(第一阶段) 10.3系统初始化(第二阶段) 10.4系统初始化(第三阶段) 10.5系统的关闭和重引导

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧