- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
头脑中问题的答案:是的,这是给学校的。不,我不能为此使用线程。是的,我在寻找答案,有些人说"is",有些人说“不是”。我也在对我的教授进行事实核查,因为我不想在别人给它打分时不公平地丢分,而且他们要求“修复”。
话虽这么说……考虑一下 Linux 系统上的这个简单的 c 程序。我 malloc 一些东西然后 fork。我将我的项目归结为确切的问题:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int main( void )
{
char * args [] = { "someinvalidcommand", NULL };
// malloc before the fork (happens in parent process)
char * something = (char *)malloc(sizeof(char));
pid_t child_pid = fork();
// are there now two things that need to be freed:
// one for each process?
if(child_pid == 0) // child process
{
//free(something); // is this needed?
// execvp (it won't return if succeeded)
if(execvp(args[0], args) < 0)
{
// or do I only need to free it here?
printf("%s: No such file or directory\n", args[0]);
/*
* EDIT: Calling it here seems to fix the issue. It turns out
* that the system calls were the ones with the "still reachable"
* errors, so I guess it's not all that important that the
* memory be freed.
*
* free(something)
*/
_exit(1);
}
}
else // parent process
{
int status;
while(wait(&status) != child_pid);
if(status != 0)
{
printf("command status: %i\n", WEXITSTATUS(status));
}
}
free(something);
return 0;
}
现在这是有点令人困惑的地方。据我所知,fork 会在该特定状态(包括文本、数据等)创建父进程的精确副本。我在某处读到这包括任何 malloc'(因此,堆)。但是,我在其他地方读到它不是因为所谓的“写时复制”,但后来我在其他地方读到“写时复制”只是一种透明且无关紧要的优化。但后来我读到的最有意义的是,因为它是一个副本,所以它有自己的,好吧......一切。
但后来我记得当使用 fork() 时,无论是 malloc 的什么都会包含相同的内存地址,所以父子指向同一个东西吗?我是否也需要释放 child 的资源?是只复制指针,还是指针指向的数据也被复制?
我使用了 valgrind,当子进程退出时,它只是提示所有内存仍然可用。它到底有多“仍然可达”?它“仍然可以访问”这一事实是否回答了我的问题并说 parent 和 child 指向同一件事,而 parent 是唯一负责释放内存的人?
最佳答案
在没有调用 exec
系列的情况下,您必须 free()
它。 Parent 和 child 不指向同一个东西,因为它们是独立的进程并且不共享相同的地址空间。想象一下,如果父级 free()
执行了它,然后子级尝试访问它,那么在替代方案下会发生什么情况。
如果您确实调用了类似 execvp()
的方法,那么就像 tmyklebu 提到的那样,您的进程将被删除,而您无需执行任何操作。
“仍然可达”意味着您仍然可以引用它,但您还没有free()
d它。由于您的所有内存无论如何都会在终止时得到 free()
d,与您永久失去对已分配内存的跟踪的实际内存泄漏相比,这有时并不是什么大问题。 Valgrind's FAQ本身说“你的程序可能没问题——它没有释放它可能拥有的一些内存。这是很常见的,而且通常是合理的。”关于这个问题的观点各不相同——一些人说明确地释放所有东西是一种很好的形式,另一些人说它是一种毫无意义的浪费资源来完成程序终止本身将为你们做的事情。
关于c - fork after malloc in parent...子进程需要释放它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440132/
如果我需要选择第 10 个父级,是否有更简洁的方法,然后重复 .parent() 10 次? $('#element_id').parent().parent().parent().parent().
从 angularJS 指南中的“如何创建通信指令”开始,https://docs.angularjs.org/guide/directive , 我正在尝试使用该布局来制作可导航的表单。 问题在于指
我有一个 jQuery 函数,需要获取元素父元素的位置。 它看起来像: function show(e) { //debugger; var nextTab
我正在尝试修复这个难看的代码。 RadGrid gv = (RadGrid) (((Control) e.CommandSource).Parent.Parent.Parent.Parent.Pare
我有一个 A 标签,可以触发它的曾曾曾祖 parent 的动画。以下所有方法都可以,但哪一个最有效,为什么? $(this).parent().parent().parent().parent().p
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在尝试定位绝对定位的 div 时遇到了一些问题。我猜它的工作方式应该是这样,但是我希望它与父对象的父对象而不是父对象一起使用,因为我有一个下拉列表,当我希望它像第一个一样保持在顶部时,它会跟随父对象
我正在做一些非常基本的 jQuery 东西,真正开始,我经常通过做类似的事情来向上导航 dom $(this).parent().parent().addClass('hello'); 我只是想知道是
此 HTML 结构有一个 div#page,其中当前页面内容将通过 Ajax 加载。内容始终由 section 标记组成,这些标记可以具有动态高度(相对于浏览器的百分比)或静态高度(以像素为单位)。
在 javascript 中是否有一种简单的方法来定位父对象的父对象? 我使用 this.parentNode 作为函数的元素来选择父节点,我尝试了 this.parent.parentNode 和
当遍历 pager.Pages 对象的 foreach 循环时,$data 是 self(正如预期的那样)。但是,$parent 应该是寻呼机对象,但它返回的是 WaterQualityResultV
在架构中,我想根据父级的 sibling 调整架构。 例如:如果 toggleMonday 为真,那么 weekdays -> monday 应该有一个特定的验证模式。 现在下面的例子有效。但是,它非
我想要完成的是,当用户将焦点放在文本框上时,其中的字段集将添加一个类“active_fieldset”,以便提供用户在表单中的位置的良好视觉提示。使用以下 javascript,它确实会影响父字段集,
我创建了这个函数来保存我的taches sauverTache(tache:Tache){ this.editionEnCours = true; tache.estReelle =
所以..这是我的问题..我有以下代码(示例): var GameObject = function (posX, posY, width, height) { this.posX = posX;
所以,我是 jQuery 的新手,我正在尝试更改关于函数触发器的 2 个级别的 div: 这是我的第一次尝试:我尝试找到最接近的“.node”,它是所有其他 div 的父级并编辑子 div。 fun
我想了解为什么使用 ng-repeat在repeat 的item 上有某个controller,那个item 的parent 和那个item 的祖父是同一个controller。我期待祖父成为父 Co
我想从我的组件 Controller 之一将 jsonModel 设置为我的 SAPUI5 组件。在组件内,我使用应用程序或 splitapp。 我想避免通过 ID 获取元素。从组件内的某个位置获取层
我不确定如何在标题上准确地表达出来,因为问题在我的场景中太具体了,但无论如何基本上我有两个类似于下面的外部类: class Config { public level: number = 1;
在我正在编写的这个脚本中,我发现自己连续使用 .parent() 最多七次来获取元素。虽然这有效,但似乎可以/应该有一种更简单的方法来完成我不知道的这个/功能。除了更多元素上更具体的类/ID 之外,还
我是一名优秀的程序员,十分优秀!