关于shm_open和shm_unlink的使用问题(要连接库的原因)

关于shm_open和shm_unlink的使用问题
referencefunctionobjectsystembehaviorlinux
C programming in the UNIX environment的编程手册,一般都会为进程间用共享内存的方法通信提供两组方法:
1.      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);
2.      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 (0);
}

假设它所在的文件为"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 1 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.chttp://blog.csdn.net/sky_cool/article/details/469970
时间: 2024-08-04 09:09:46

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

通过共享内存进行进程间通信

共享内存的工作方式 顾名思义,共享内存让一段内存可供多个进程访问.用特殊的系统调用(即对 UNIX 内核的请求)分配和释放内存并设置权限:通过一般的读写操作读写内存段中的数据. 共享内存并不是从某一进程拥有的内存中划分出来的:进程的内存总是私有的.共享内存是从系统的空闲内存池中分配的,希望访问它的每个进程连接它.这个连接过程称为映射,它给共享内存段分配每个进程的地址空间中的本地地址. 假设在同一系统上有两个进程 A 和 B 正在运行(见 图 1),它们可以通过共享内存进行协作和共享信息.在图中

002:IPC与system函数简介

1:IPC名字mq_XXX,sem_XXX,shm_XXX. 消息队列 信号量 共享内存区 <mqueue.h> <semaphore.h> <sys.mman.h> 创建,打开或删除 mq_open mq_close mq_unlink sem_open sem_close sem_unlink shm_open shm_close shm_unlink sem_init sem_destroy 控制IPC操作的函数 mq_getattr mq_setattr ftr

system v和posix的共享内存对比

参考 http://www.startos.com/linux/tips/2011012822078.html 1)Linux和所有的UNIX操作系统都允许通过共享内存在应用程序之间共享存储空间. 2)有两类基本的API函数用于在进程间共享内存:System v和POSIX.  (当然,还有mmap,属于POSIX的) 3)这两类函数上使用相同的原则,核心思想就是任何要被共享的内存都必须经过显示的分配. 4)因为所有进程共享同一块内存,共享内存在各种进程间通信方式中具有最高的效率. 5)内核没有

[linux]进程(七)——进程通信

进程间通信一,管道,管道的限制:(1)半双工,数据只能在一个方向上流动(2)管道一般只在具有公共祖先的进程之间使用,通常一个管道由一个进程创建,然后该进程调用fork()函数,此后父子进程可以使用该管道管道的创建: [cpp] view plaincopy #include <unistd.h> int pipe(int fileds[2]);  //filedes[0]为读而打开,filedes[1]为写而打开 向一个没有读进程关联的管道写数据,会产生SIGPIPE,内核对于SIGPIPE的

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

转自:http://loofeer.blog.51cto.com/707932/791065 默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样.象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储.而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备:您只是安装它,它就可以使用了. tmpfs有以下优势: 1.动态文件系统的大小. 2.tmpf

ios性能测试Instruments

Instruments 用户指南 Instruments用户指南介绍 Instruments 是应用程序用来动态跟踪和分析 Mac OS X 和 iOS 代码的实用工具.这是一个灵活而强大的工具,它让你可以跟踪一个或多个进程,并检查收集的数据.这样,Instruments 可以帮你更好的理解应用程序和操作系统的行为. 使用 Instruments 应用,你可以使用特殊的工具(即 instruments 工具)来跟踪同一进程不同方面的行为.你也可以使用该应用来记录一系列用户界面的动作并响应它们,同

Kubernetes中Pod间共享内存方案

摘要:一些公共服务组件在追求性能过程中,与业务耦合太紧,造成在制作基础镜像时,都会把这些基础组件都打包进去,因此当业务镜像启动后,容器里面一大堆进程,这让Kubernetes对Pod的管理存在很大隐患.为了让业务容器瘦身,更是为了基础组件自身的管理更独立和方便,将基础组件从业务镜像中剥离并DaemonSet容器化部署.然而一些基础组件Agent与业务Pod之间通过共享内存的方式进行通信,同一Node中跨Pod的共享内存方案是首先要解决的问题. 为什么要将公共基础组件Agent进行DaemonSe

undefined reference to `shm_unlink&#39;

1.问题描述: 在编译一个程序的时候提示这样的错误: BLog.cpp:(.text+0x5fc): undefined reference to `shm_unlink'DBLog.cpp:(.text+0x610): undefined reference to `shm_open'/home/SCS/install/lib/liblog4cplus.a(timehelper.o): In function `log4cplus::helpers::Time::gettimeofday()'

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

linux 共享内存shm_open实现进程间大数据交互 read.c #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <sys/mman.h> #include <string.h> #include <errno.h> #include <unistd.h> /* int