在多个没有亲缘关系的进程中,想共享一块内存。不知道有什么办法,可以让其他进程知道共享内存的 shmid

yaoyuhang 2005-02-23 05:24:10
在多个没有亲缘关系的进程中,想共享一块内存。不知道有什么办法,可以让其他进程知道共享内存的 shmid

我想到,用一个物理文件来保存。是不是太笨了。

有没有什么其他方法。

请赐教。
...全文
301 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky_cool 2005-09-02
  • 打赏
  • 举报
回复
那个问“同步/互斥”的,原来你不是楼主啊!!
害我打了半天字
sky_cool 2005-09-02
  • 打赏
  • 举报
回复
结帖时,别忘记施舍10分给我
好歹为你打了半天字
:)
sky_cool 2005-09-02
  • 打赏
  • 举报
回复
方法一:
把传统的互斥锁加到共享内存的结构中去,每次使用数据前先加锁
struct shmstruct
{
TMutex mutex; //typedef pthread_mutex_t TMutex
struct limit_con check[10];
};
......
int fd;
struct shmstruct* ptr;
char *ipc_name=strdup("/tmp/ipc.shm");

shm_unlink(ipc_name);
fd=shm_open(ipc_name,O_RDWR|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
free(ipc_name);
ftruncate(fd,sizeof(struct shmstruct));
ptr=mmap(NULL,sizeof(struct shmstruct),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
MutexCreate(&(ptr->mutex));
......
MutexLock(&(ptr->mutex));
......
//do something
......
MutexUnlock(&(ptr->mutex));
方法二:
如果你的系统支持有名信号量,不妨在进程间使用它。用法和shm的用法类似。
包括:sem_open(),sem_close(),sem_wait(),sem_post(),sem_unlink()......
具体参见《UNIX网络编程》(第二版 W.R.Stevens著) 第二卷 第190页

daemeon 2005-09-01
  • 打赏
  • 举报
回复
在/dev/shm下建立一个文件也可以实现共享内存
darkstar21cn 2005-09-01
  • 打赏
  • 举报
回复
随便问一句,不需要同步/互斥吗?
如果我2个进程都要读写怎么办?
darkstar21cn 2005-09-01
  • 打赏
  • 举报
回复
学习ing
sky_cool 2005-09-01
  • 打赏
  • 举报
回复
尽量不要用shmget,shmat系列的库函数,它们是system V的标准,不是每个linux/unix系统都支持的
用posix标准的一系列共享内存的函数:shm_open(),ftruncate(),mmap(),shm_unlink()
功能基本和shmget......相同,你可以去man一下用法,或者参考《unix网络编程》第二版 第二卷W.R.Stevens著。
chillming 2005-08-10
  • 打赏
  • 举报
回复
shmid是用shmget(key,size,flag)得到的,key是一个常量。
key 可以用下面函数获取

#include <sys/types.h>
#include <sys/ipc.h>
key_t ftok(const char *path,int id)

path:物理路径,如:/root 或/etc/rc.conf
id:你喜欢什么数字都可以拉
chillming 2005-07-22
  • 打赏
  • 举报
回复
来源:http://www-128.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html
范例1:两个进程通过映射普通文件实现共享内存通信
范例1包含两个子程序:map_normalfile1.c及map_normalfile2.c。编译两个程序,可执行文件分别为map_normalfile1及map_normalfile2。两个程序通过命令行参数指定同一个文件来实现共享内存方式的进程间通信。map_normalfile2试图打开命令行参数指定的一个普通文件,把该文件映射到进程的地址空间,并对映射后的地址空间进行写操作。map_normalfile1把命令行参数指定的文件映射到进程地址空间,然后对映射后的地址空间执行读操作。这样,两个进程通过命令行参数指定同一个文件来实现共享内存方式的进程间通信。

下面是两个程序代码:


/*-------------map_normalfile1.c-----------*/
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
char name[4];
int age;
}people;

main(int argc, char** argv) // map a normal file as shared mem:
{
int fd,i;
people *p_map;
char temp;

fd=open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
lseek(fd,sizeof(people)*5-1,SEEK_SET);
write(fd,"",1);

p_map = (people*) mmap( NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );
close( fd );
temp = 'a';
for(i=0; i<10; i++)
{
temp += 1;
memcpy( ( *(p_map+i) ).name, &temp,2 );
( *(p_map+i) ).age = 20+i;
}
printf(" initialize over \n ");
sleep(10);

munmap( p_map, sizeof(people)*10 );
printf( "umap ok \n" );
}

/*-------------map_normalfile2.c-----------*/
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
char name[4];
int age;
}people;

main(int argc, char** argv) // map a normal file as shared mem:
{
int fd,i;
people *p_map;
fd=open( argv[1],O_CREAT|O_RDWR,00777 );
p_map = (people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
for(i = 0;i<10;i++)
{
printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age );

}
munmap( p_map,sizeof(people)*10 );
}


tengulre 2005-02-24
  • 打赏
  • 举报
回复
up
cangzhouwd 2005-02-24
  • 打赏
  • 举报
回复
同意楼上的.key写一样就可以了.
gettext 2005-02-24
  • 打赏
  • 举报
回复
shmid是用shmget(key)得到的,key是一个常量。
blueflame 2005-02-23
  • 打赏
  • 举报
回复
用命名管道进行通信
lswx 2005-02-23
  • 打赏
  • 举报
回复
如果你用的是多进程的话,用父进程保存和管理这些信息

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧