fork()系统调用和进程的内存空间 [英] fork() system call and memory space of the process

查看:272
本文介绍了fork()系统调用和进程的内存空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用当进程使用fork()调用创建新进程时,在父进程和新创建的子进程之间仅共享共享的内存段.为新创建的堆栈创建堆和堆的副本Silberschatz的操作系统概念"解决方案中的流程".

I quote "when a process creates a new process using fork() call, Only the shared memory segments are shared between the parent process and the newly forked child process. Copies of the stack and the heap are made for the newly created process" from "operating system concepts" solutions by Silberschatz.

但是当我尝试该程序时

#include  <stdio.h>
#include  <sys/types.h>

#define   MAX_COUNT  200

void  ChildProcess(void);                /* child process prototype  */
void  ParentProcess(void);               /* parent process prototype */

void  main(void)
{
         pid_t  pid;
         char * x=(char *)malloc(10);

         pid = fork();
         if (pid == 0) 
            ChildProcess();
         else 
            ParentProcess();
        printf("the address is %p\n",x);
}

void  ChildProcess(void)
{
          printf("   *** Child process  ***\n");
}

void  ParentProcess(void)
{
         printf("*** Parent*****\n");
}

结果就像:

*** Parent*****
the address is 0x1370010
   *** Child process  ***
the address is 0x1370010

父母和孩子都打印出堆中相同的地址.

both parent and child printing the same address which is in heap.

有人可以在这里解释我的矛盾吗?请清楚说明父母和孩子在存储器空间中共享的所有东西.

can someone explain me the contradiction here. please clearly state what are all the things shared by the parent and child in memory space.

推荐答案

从另一个线程引用自己.

Quoting myself from another thread.

发出fork()系统调用时,将复制所有页面 对应于父进程的创建,加载到一个单独的 操作系统为子进程分配的内存位置.但这不是 在某些情况下需要.考虑孩子执行死刑的情况 "exec"系统调用或在fork()之后不久退出.当...的时候 仅需要孩子就可以执行父流程的命令, 由于执行是没有必要复制父进程的页面 将调用它的进程的地址空间替换为 要执行的命令.

When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. Consider the case when a child executes an "exec" system call or exits very soon after the fork(). When the child is needed just to execute a command for the parent process, there is no need for copying the parent process' pages, since exec replaces the address space of the process which invoked it with the command to be executed.

在这种情况下,使用了一种称为写时复制(COW)的技术.和 这种技术,当发生派生时,父进程的页面不会 为子进程复制.而是,页面之间共享 子进程和父进程.每当一个过程(父母或孩子) 修改页面,单独制作该页面的单独副本 对于执行修改的那个过程(父母或孩子). 然后,此过程将使用新复制的页面,而不是 在以后的所有参考资料中共享一个.另一个过程( 未修改共享页面)继续使用的原始副本 页面(现在不再共享).这项技术称为 写时复制,因为当某些进程写入页面时会复制该页面.

In such cases, a technique called copy-on-write (COW) is used. With this technique, when a fork occurs, the parent process's pages are not copied for the child process. Instead, the pages are shared between the child and the parent process. Whenever a process (parent or child) modifies a page, a separate copy of that particular page alone is made for that process (parent or child) which performed the modification. This process will then use the newly copied page rather than the shared one in all future references. The other process (the one which did not modify the shared page) continues to use the original copy of the page (which is now no longer shared). This technique is called copy-on-write since the page is copied when some process writes to it.

  • 此外,要理解为什么这些程序似乎使用相同的内存空间(事实并非如此),我想引用《操作系统:原则与实践》一书的一部分. /p>

  • Also, to understand why these programs appear to be using the same space of memory (which is not the case), I would like to quote a part of the book "Operating Systems: Principles and Practice".

    大多数现代处理器都引入了一种称为间接"的级别 虚拟地址.使用虚拟地址,每个进程的内存 从相同"的位置开始,例如零. 每个进程都认为自己拥有整个计算机,尽管 显然,事实并非如此.

    Most modern processors introduce a level of indirection, called virtual addresses. With virtual addresses, every process's memory starts at the "same" place, e.g., zero. Each process thinks that it has the entire machine to itself, although obviously that is not the case in reality.

    因此,这些虚拟地址是物理地址的转换,并且不表示相同的物理内存空间,作为一个更实际的示例,如果我们多次编译并运行一个显示a方向的程序,则可以进行测试.静态变量,例如此程序.

    So these virtual addresses are translations of physical addresses and doesn't represent the same physical memory space, to leave a more practical example we can do a test, if we compile and run multiple times a program that displays the direction of a static variable, such as this program.

    #include <stdio.h>
    
    int main() {
        static int a = 0;
    
        printf("%p\n", &a);
    
        getchar();
    
        return 0;
    }
    

    不可能两次获得相同的内存地址 如果我们直接处理物理内存,则使用不同的程序.

    It would be impossible to obtain the same memory address in two different programs if we deal with the physical memory directly.

    多次运行该程序获得的结果是...

    And the results obtained from running the program several times are...

    这篇关于fork()系统调用和进程的内存空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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