关于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. PHP-手册阅读笔记

    1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...

  2. js firstChild 、nextSibling、lastChild、previousSibling、parentNode

    nextSibling下一个兄弟节点 previousSibling上一个兄弟 parentNode父亲节点 <select><option value="zs" ...

  3. Android-----完全隐藏软键盘

    隐藏软键盘一直是我头痛的事情,没有找到一种真正能隐藏的方法.点击EditText的时候总是弹出软键盘.-----杯具 杯具(一): InputMethodManager im =(InputMetho ...

  4. RabbitMQ概念及环境搭建(三)RabbitMQ cluster

    测试环境:VMS00781 VMS00782 VMS00386 (centos5.8) 1.先在三台机器上分别安装RabbitMQ Server 2.读取其中一个节点的cookie,并复制到其他节点( ...

  5. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  6. Android用shareUserID实现多个Activity显示在同一界面

    近来整理文档,发现两年前研究Android多个Activity叠加显示的方案.时光荏苒,一去不回. 虽然后来没有用上,但还是整理如下,Android版本还是2.2的: ActivityGroup描画方 ...

  7. Mosquitto ---学习笔记

    Mosquitto的安装,可以参见:https://mosquitto.org/download/ [基础配置]Mosquitto服务的配置文件为/etc/mosquitto/mosquitto.co ...

  8. oracle各种常用管理sql及其他 ---待续

    启动客户端工具:sqlplus /nolog 使用sysdba链接:conn / as sysdba; select * from dba_users; --查看数据库里面所有用户,前提是你是有dba ...

  9. eclipse软件与git配合使用创建git仓库

    一.在eclipse上安装git,和安装其他插件一样 help->Install new software->add... 在弹出框中输入name:git,location:http:// ...

  10. Centos设置静态IP及修改Centos配置文件的方法

    通常,如果我们想更改主机地址为静态地址或者更改主机名,需要修改的几个文件包括: /etc/sysconfig/network Centos设置主机名和网络配置 /etc/sysconfig/netwo ...