共享堆内存叉() [英] Sharing heap memory with fork()

查看:120
本文介绍了共享堆内存叉()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的实施在C数据库服务器将处理来自多个客户端的请求。为了做到这一点,我用叉子()来处理各个客户端的连接。

服务器存储其中包含一个根指针的散列的动态分配记录的表堆的数据。记录是具有指针的各种数据类型结构。我想对于流程要能够共享这些数据,以便当客户端进行了更改堆的变化将是其他客户端可见。

我已经了解到,fork()的使用 COW(写时复制)和我的理解是,它会复制堆(和栈)父进程的内存中,当孩子会尝试修改内存中的数据。

我发现我可以使用SHM库共享内存。

-Would它足以共享数据库的根指针或做我必须让所有分配的内存作为共享?

- 如果一个孩子分配内存将父/其他的孩子能够访问它?

- 同样,如果一个孩子分配内存,并且以后被杀害将所分配的内存还停留在堆?

因此​​,例如将低于code是共享堆内存(在shared_string)一个有效的方法是什么?如果孩子是使用类似code(即,从开始//启动)将其他孩子能够子运行时读/写它,它死了吗?以后

 的key_t键;
INT的shmid;键= ftok(/ tmp目录,'R');
的shmid = shmget的(键,1024,0644 | IPC_CREAT);//开始
字符*串;
字符串=的malloc(sizeof的(字符)* 10);的strcpy(字符串,字符串);字符* shared_string;shared_string =的shmat(的shmid,串,0);的strcpy(shared_string,字符串);


解决方案

首先,是你想达到什么目的完全不合适的。即使你可以把它的工作,这是一个可怕的黑客。一般情况下,只适用于非常简单的程序,无论如何,我会这么远,说去了应从未除非 EXEC 快速跟进,但是从点是一边在这里使用。你真的应该使用线程。

随着中说,只有这样才能有一个年代后,并在同一个三分球都是有效的,是<$的父母和孩子之间共享内存C $ C> MMAP (或的shmat ,但是这是一个很大fuglier)的文件或匿名地图 MAP_SHARED 之前的叉后,将无法创建这样新的共享内存,因为没有保证,这将在两个相同的地址范围被映射。

只要不使用。这不是对工作的工具。

I am working on implementing a database server in C that will handle requests from multiple clients. In order to do so I am using fork() to handle connections for individual clients.

The server stores data in the heap which consists of a root pointer to hash tables of dynamically allocated records. The records are structs that have pointers to various data-types. I would like for the processes to be able to share this data so that when a client makes a change to the heap the changes will be visible for the other clients.

I have learned that fork() uses COW (Copy On Write) and my understanding is that it will copy the heap (and stack) memory of the parent process when the child will try to modify the data in memory.

I have found out that I can use the shm library to share memory.

-Would it suffice to share the root pointer of the database or do I have to make all allocated memory as shared?

-If a child allocates memory will the parent / other children be able to access it?

-Also if a child allocates memory and is later killed will the allocated memory still stay on the heap?

So for example would the code below be a valid way to share heap memory (in shared_string)? If a child were to use similar code (i.e. starting from //start ) would other children be able to read/write to it while the child is running and after it's dead?

key_t key;
int shmid;

key = ftok("/tmp",'R');
shmid = shmget(key, 1024, 0644 | IPC_CREAT);

//start
char * string;
string = malloc(sizeof(char) * 10);

strcpy(string, "a string");

char * shared_string;

shared_string = shmat(shmid, string, 0);

strcpy(shared_string, string);

解决方案

First of all, fork is completely inappropriate for what you're trying to achieve. Even if you can make it work, it's a horrible hack. In general, fork only works for very simplistic programs anyway, and I would go so far as to say that fork should never be used except followed quickly by exec, but that's aside from the point here. You really should be using threads.

With that said, the only way to have memory that's shared between the parent and child after fork, and where the same pointers are valid in both, is to mmap (or shmat, but that's a lot fuglier) a file or anonymous map with MAP_SHARED prior to the fork. You cannot create new shared memory like this after fork because there's no guarantee that it will get mapped at the same address range in both.

Just don't use fork. It's not the right tool for the job.

这篇关于共享堆内存叉()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆