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)
{

本文介绍如何在Linux环境下使用POSIX共享内存API进行进程间通信。文章提供了具体的代码示例,并解释了如何解决链接错误的问题,包括如何正确指定-lrt选项。
最低0.47元/天 解锁文章
3828

被折叠的 条评论
为什么被折叠?



