发生malloc分配内存什么的exec()后改变程序的形象? [英] What happens to malloc'ed memory after exec() changes the program image?

查看:113
本文介绍了发生malloc分配内存什么的exec()后改变程序的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,当我称之为的一个EXEC()系统在Linux中称,它将以崭新的形象取代目前正在运行的进程。所以,当我叉一个新的进程并运行执行exec(),孩子会用新工艺所取代。

I know that when I call one of the exec() system calls in Linux that it will replace the currently running process with a new image. So when I fork a new process and run exec(), the child will be replaced with the new process.

正好我从堆中分配任何内存是什么?说我要来解析命令任意数量并将其发送到执行exec()。举行这次任意号码,我很可能不得不在某些时候分配内存,因为我不认为我能与静态大小的数组做是正确的,所以我很可能会使用的malloc()或等值的东西。

What happens to any memory I've allocated from the heap? Say I want to parse an arbitrary number of commands and send it into exec(). To hold this arbitrary number, I'll likely have to allocate memory at some point since I don't think I can do it correctly with static sized arrays, so I'll likely use malloc() or something equivalent.

我需要保持这种内存中分配,直到我打过电话后,执行exec(),但执行exec()永不再来。

I need to keep this memory allocated until after I've called exec(), but exec() never returns.

是否存储器获取由操作系统回收

Does the memory get reclaimed by the operating system?

推荐答案

当你调用叉(),创建调用进程的副本。这孩子的过程是(几乎)完全一样的父母,即通过分配的内存的malloc()是preserved,你可以自由地读取或修改。这些修改不会对父进程可见,虽然作为父和子进程是完全分开的。

When you call fork(), a copy of the calling process is created. This child process is (almost) exactly the same as the parent, i.e. memory allocated by malloc() is preserved and you're free to read or modify it. The modifications will not be visible to the parent process, though, as the parent and child processes are completely separate.

当你调用执行exec()中的孩子,孩子的过程是通过一个新的予以更换。从的execve(2):

When you call exec() in the child, the child process is replaced by a new process. From execve(2):

execve() does not return on success, and the text, data, bss, and stack
of the calling process are overwritten by that of the program loaded.

通过覆盖数据段中,执行exec()有效的调用回收,是由<之前分配的内存code>的malloc()。

By overwriting the data segment, the exec() call effectively reclaims the memory that was allocated before by malloc().

父进程是这一切的影响。假设你叉(),内存仍然是在父进程提供。

The parent process is unaffected by all this. Assuming that you allocated the memory in the parent process before calling fork(), the memory is still available in the parent process.

的现代实现的malloc()使用匿名内存映射,请参阅mmap(2)。据的execve(2),内存映射是不超过一个执行exec()调用preserved,所以这个内存也回收。

Modern implementations of malloc() use anonymous memory mappings, see mmap(2). According to execve(2), memory mappings are not preserved over an exec() call, so this memory is also reclaimed.

这篇关于发生malloc分配内存什么的exec()后改变程序的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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