关于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. SDL 2.0 API by Category

    Basics View information and functions related to... View the header Initialization and Shutdown SDL. ...

  2. C1控件的破解步骤

    最近接触了一个系统中的打印和报表控件用到了C1控件,控件在以前就注册了的,可是在这次修改了系统后,系统却提示C1的控件没有注册. 怎么回事呢?我们这次并没有修改报表,而且也没有和C1控件相关的改动.怎 ...

  3. C#单线程内存占用过大导致无法创建新的对象

    https://msdn.microsoft.com/zh-cn/library/8cxs58a6.aspx  按照csdn原文 默认分配的堆栈大小为1mb  可以通过maxstacksize改变默认 ...

  4. 锻造完美U盘小偷:活用消息机制

    锻造完美U盘小偷:活用消息机制作者:灰狐来源:灰狐's Blog 注:本文已发表在<黑客防线>2008年第1期,转载请注明出处. 以前经常看到有人做出一些蛮有意思的小工具,其中最多的似乎就 ...

  5. Python 列表 remove() 方法

    描述 Python 列表 remove() 方法通过指定元素的值来移除列表中某个元素的第一个匹配项,如果这个元素不在列表中会报一个异常. 语法 remove() 方法语法: L.remove(obj) ...

  6. sklearn 中的交叉验证

    sklearn中的交叉验证(Cross-Validation) sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sklearn中关于交叉验证的 ...

  7. 深入浅出Node.js--数据通讯,NET模块运行机制

    互联网的运作,最根本的驱动就是信息的交互,NodeJS 在数据交互这一块做的很带感,异步编程让人很惬意,关于 NodeJS 的数据通信,最基础的两个模块是 NET 和 HTTP,前者是基于 TCP 的 ...

  8. unity, multi pass shader中的surface pass

    今天尝试写一个multi pass shader,但其中有一个Pass是surface pass,总是莫名奇妙地报错.后来看到下面帖子: http://forum.unity3d.com/thread ...

  9. Spring Cloud 通过代码自定义配置Ribbon

    我们还是先从官网文档开始学习,如下图所示,我们可以搞一个测试配置类,来验证是否真的可以通过代码来自定义配置Ribbon,但文档明确给出了警告:即这个测试配置类不能放在@ComponentScan所扫描 ...

  10. Spring cloud子项目

    目前来说spring主要集中于spring boot(用于开发微服务)和spring cloud相关框架的开发,我们从几张图着手理解,然后再具体介绍: spring cloud子项目包括: Sprin ...