关于shm_open和shm_unlink的使用问题
referencefunctionobjectsystembehaviorlinux
C programming in the UNIX environment的编程手册,一般都会为进程间用共享内存的方法通信提供两组方法:
. POSIX定义的:
int shm_open(const char *name, int oflag, mode_t mode);
int shm_unlink(const char *name);
int ftruncate(int fd, off_t length);
. SYSTEM V定义的
int shmget(key_t key, int size, int shmflg);
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
int shmctl(int shmid, int cmd, struct shmid_ds *buf); 由于POSIX标准比较通用,一般建议使用该标准定义的方法集。
但是在使用shm_open和shm_unlink两个函数时,你可能遇到和我同样的问题,见如下代码。
该代码旨在测试你的系统是否支持POSIX定义的共享内存函数集。 /* This is just to test if the function is found in the libs. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h> int
main (void)
{
int i; i = shm_open ("/tmp/shared", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
printf ("shm_open rc = %d/n", i); shm_unlink ("/tmp/shared"); return ();
} 假设它所在的文件为"test.c"
我这么编译:
gcc -o test test.c
结果为:
/tmp/ccaGhdRt.o(.text+0x23): In function `main':
: undefined reference to `shm_open'
/tmp/ccaGhdRt.o(.text+0x49): In function `main':
: undefined reference to `shm_unlink'
collect2: ld returned exit status 编译结果实际上是说,没include相应的头文件,或是头文件不存在(即系统不支持该库函数)
但我man shm_open是可以找到帮助文件的(说明系统支持),原因何在??? 请注意一下man shm_open的帮助文件的最后几行:
NOTES
These functions are provided in glibc 2.2 and later. Programs using
these functions must specify the -lrt flag to cc in order to link
against the required ("realtime") library. POSIX leaves the behavior of the combination of O_RDONLY and O_TRUNC
unspecified. On Linux, this will successfully truncate an existing
shared memory object - this may not be so on other Unices. The POSIX shared memory object implementation on Linux 2.4 makes use of
a dedicated file system, which is normally mounted under /dev/shm. 如果你注意到的话,这样编译就能通过了:
gcc -lrt -o test test.c
http://blog.csdn.net/sky_cool/article/details/469970

关于shm_open和shm_unlink的使用问题(要连接库的原因)的更多相关文章

  1. Linux学习笔记(14)-进程通信|共享内存

    在Linux中,共享内存是允许两个不相关的进程访问同一个逻辑内存的进程间通信方法,是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式. 不同进程之间共享的内存通常安排为同一段物理内存.进程可 ...

  2. 002:IPC与system函数简介

    1:IPC名字mq_XXX,sem_XXX,shm_XXX. 消息队列 信号量 共享内存区 <mqueue.h> <semaphore.h> <sys.mman.h> ...

  3. system v和posix的共享内存对比 & 共享内存位置

    参考 http://www.startos.com/linux/tips/2011012822078.html 1)Linux和所有的UNIX操作系统都允许通过共享内存在应用程序之间共享存储空间. 2 ...

  4. Instruments 使用指南

    Instruments 用户指南 http://cdn.cocimg.com/bbs/attachment/Fid_6/6_24457_90eabb4ed5b3863.pdf 原著:Apple Inc ...

  5. linux下修改/dev/shm tmpfs文件系统大小

    默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样.象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储.而且传统的 ...

  6. Envoy 基础教程:使用 Unix Domain Socket(UDS) 与上游集群通信

    Envoy Proxy 在大多数情况下都是作为 Sidecar 与应用部署在同一网络环境中,每个应用只需要与 Envoy(localhost)交互,不需要知道其他服务的地址.然而这并不是 Envoy ...

  7. gcc创建和使用静态库、动态库

    http://www.cnblogs.com/dyllove98/archive/2013/06/25/3155599.html 目录树结构: test/include/hello.h #ifdef ...

  8. gcc编译器创建和使用静态库、动态库

    http://www.cnblogs.com/dyllove98/archive/2013/06/25/3155599.html 目录树结构: test/include/hello.h #ifdef ...

  9. linux 共享内存shm_open实现进程间大数据交互

    linux 共享内存shm_open实现进程间大数据交互 read.c #include <sys/types.h> #include <sys/stat.h> #includ ...

随机推荐

  1. Mac下搭建svn服务器和XCode配置svn

    先打开命令行终端. 1.创建svn repository svnadmin create /yourpath/svnroot/repository 2.配置svn用户权限. / yourpath /s ...

  2. 保存已登录plsql developer的用户名和密码

    1.保存用户名 tools -> Preferences -> User Interface - Options 勾选 Autosave username . 保存 2.保存密码 tool ...

  3. Js操作Select大全(取值、设置选中等等)

    jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selecto ...

  4. Error_GL_KeyflexfieldDefinitionFactory.getStructureNumber无法找到应用产品

    2014-07-12 BaoXinjian

  5. json字符串序列化exception处理

    一.背景: 使用REST接口接收远端传送过来的Json格式String,需要把这个String序列化成响应的对象. 二.问题: 对方封装了一个错误的json格式过来,程序就挂了…… 三.似乎解决: 通 ...

  6. powerdesigner学习笔记【转载】

    转自:http://blog.itpub.net/11968859/viewspace-620440/ 谢谢! 1.做CDM模型的时候,因为开始定义ITEM的时候,没有注意把NAME和CODE全定义成 ...

  7. ubuntu 16.04 apt-get 更新使用中科大镜像源

    1 备份系统配置 sudo cp /etc/apt/sources.list /etc/apt/source.list.bak 2 编辑配置 sudo vi /etc/apt/sources.list ...

  8. win10 U盘安装ubuntu16.04双系统

    所需工具U盘,软件ultralISO.ubuntu16.04,自己使用的系统是win10 一.制作U盘启动盘 打开ultraISO软件 2 2  3 4 开始写入—>直到完成大概五分的样子 二. ...

  9. Oracle中查询表字段基本信息、主键、外键(整理)

    背景 因为项目某些模块的数据结构设计没有严格按照某规范设计,所以只能从数据库中查询数据结构,需要查询的信息如下:字段名称.数据类型.是否为空.默认值.主键.外键等等. 在网上搜索了查询上述信息的方法, ...

  10. 使用Frame控件设计Silverlight的导航

    这里所说的导航其实就是在Silverlight的页面之间前进后退以及跳转.通过Frame控件配合后台NavigationService类可以很容易的做到页面之间的导航. 这就是工具箱中的Frame控件 ...