具体来说,如何fork()的手柄动态()在Linux中,从分配的malloc的内存? [英] Specifically, how does fork() handle dynamically allocated memory from malloc() in Linux?

查看:103
本文介绍了具体来说,如何fork()的手柄动态()在Linux中,从分配的malloc的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父和子进程的程序。叉()之前,父进程称为malloc()和填充在与一些数据的数组。叉()之后,孩子需要的数据。我知道,我可以用一个管道,但下面的code似乎工作:

I have a program with a parent and a child process. Before the fork(), the parent process called malloc() and filled in an array with some data. After the fork(), the child needs that data. I know that I could use a pipe, but the following code appears to work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main( int argc, char *argv[] ) {
    char *array;
    array = malloc( 20 );
    strcpy( array, "Hello" );
    switch( fork() ) {
    case 0:
        printf( "Child array: %s\n", array );
        strcpy( array, "Goodbye" );
        printf( "Child array: %s\n", array );
        free( array );
        break;
    case -1:
        printf( "Error with fork()\n" );
        break;
    default:
        printf( "Parent array: %s\n", array );
        sleep(1);
        printf( "Parent array: %s\n", array );
        free( array );
    }
    return 0;
}

输出是:

Parent array: Hello
Child array: Hello
Child array: Goodbye
Parent array: Hello

我知道,在栈上分配的数据是在孩子使用,但现在看来,在堆中分配的数据也可给孩子。同样,孩子不能修改堆栈父的数据,孩子不能修改堆父的数据。所以,我认为孩子都有自己的堆栈和堆数据的副本。

I know that data allocated on the stack is available in the child, but it appears that data allocated on the heap is also available to the child. And similarly, the child cannot modify the parent's data on the stack, the child cannot modify the parent's data on the heap. So I assume the child gets its own copy of both stack and heap data.

这是始终在Linux中的情况?如果是这样,这里的是支持这个文件?我查了fork()的手册页,但它并没有具体提到动态分配的堆内存。

Is this always the case in Linux? If so, where the is the documentation that supports this? I checked the fork() man page, but it didn't specifically mention dynamically allocated memory on the heap.

感谢您

推荐答案

这是分配过程中的每个页面(无论是在其上有或堆栈中的虚拟内存页面)复制的分叉过程中要能够访问它。

Each page that is allocated for the process (be it a virtual memory page that has the stack on it or the heap) is copied for the forked process to be able to access it.

其实,这是不对的在开始复制,它被设置为写入时复制,这意味着一旦某个进程(父母或子女)试图修改被复制的网页,使他们不会损害自己-another,而且还有来自fork()的访问点的所有数据给他们。

Actually, it is not copied right at the start, it is set to Copy-on-Write, meaning once one of the processes (parent or child) try to modify a page it is copied so that they will not harm one-another, and still have all the data from the point of fork() accessible to them.

例如,code页面,这些实际的可执行文件映射到内存中,通常是只读的,因此被所有的派生过程中重复使用 - 他们不会被再次复制,因为没有人写有,只读等写入时复制将永远是必要的。

For example, the code pages, those the actual executable was mapped to in memory, are usually read-only and thus are reused among all the forked processes - they will not be copied again, since no one writes there, only read, and so copy-on-write will never be needed.

更多信息,请这里和的这里

这篇关于具体来说,如何fork()的手柄动态()在Linux中,从分配的malloc的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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