问题描述
我在写一个简单的shell作为操作系统的课程作业,我需要在PATH搜索找到该程序用户键入,一旦我找到合适的目录,我的malloc一块内存只够保存目录名加程序名称,我通过它作为execv()的第一个参数。
I'm writing a simple shell as an OS course assignment, I need to search in the PATH to find the program user typed in, once I find the right directory, I malloc a piece of memory just enough to hold the directory name plus the program name, and I pass it as the first argument to execv().
我可以静态分配100个字符左右,但有一个限制,使我感到不舒服。所以,当execv()执行,是堆清理或者是一块内存失去了什么?
I could have statically allocated 100 characters or so, but having a limit makes me feel uncomfortable. So when execv() executes, is the heap cleaned up or is that piece of memory lost?
这也许不是一个很大的内存,但我只是好奇。
It's maybe not a lot of memory but I'm just curious.
推荐答案
在执行exec()
,整个过程是(A)结束,因此所有的资源,包括动态存储器和一些FD的如下,由操作系统回收,和(b)所取代:code,数据,线程,...
When you exec()
, the entire process is (a) ended, so all resources including dynamic memory and some fd's as below, are reclaimed by the operating system, and (b) replaced: code, data, threads, ...
重新文件描述符,从男人的execve
:
文件描述符调用的进程映像开放新照常开放
过程映像,除了那些为其近距离on-exec标志设置
(请参阅close(2)和fcntl(2))。即保持开放描述不受影响
通过调用execve()。
这篇关于调用execv()的时候时,动态分配的内存呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!