关于SHM_OPEN和SHM_UNLINK的使用问题
来源:百度文库 编辑:神马文学网 时间:2024/06/30 20:41:04
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
#include
#include
#include
#include
#include
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.c
其实就是要连接库的原因。
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
#include
#include
#include
#include
#include
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.c
其实就是要连接库的原因。
关于SHM_OPEN和SHM_UNLINK的使用问题
关于存储过程的使用问题
关于外出经营税收管理证明使用和管理存在的问题与对策
国家税务总局关于外商投资企业和外国企业征收城镇土地使用税问题的批复
关于土地使用税若干具体问题的解释和暂行规定2
关于缺口技术的分析和使用
关于塑料瓶底的标志和使用
关于缺口技术的分析和使用
关于房产税城镇土地使用税有关问题的通知
关于《事业单位法人证书》使用问题的通知
关于购买使用过的固定资产相关问题
关于使用cuteswfget下载FLASH动画的问题
关于房产税城镇土地使用税有关问题的通知
316、 关于购买使用过的固定资产相关问题
关于中餐和快餐的问题
关于LMHOSTS和APACHE之间的问题
关于“媚西”和“中国的问题”
关于矿业权问题的认识和思考
关于矿业权问题的认识和思考
我.关于变频器的安装和使用的一点笔记
关于变频器的安装和使用的一点笔记
关于bloglines,Flickr和del.icio.us的使用
关于bloglines,Flickr和del.icio.us的使用
关于资金担保和使用授信额度的规定