开发者

What happens to dynamic allocated memory when calling execv()?

开发者 https://www.devze.com 2023-01-16 00:25 出处:网络
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 开发者_如何学Cdirectory, I malloc a piece of memory just e

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 开发者_如何学Cdirectory, 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().

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.


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, ...

Re file descriptors, from "man execve":

File descriptors open in the calling process image remain open in the new process image, except for those for which the close-on-exec flag is set (see close(2) and fcntl(2)). Descriptors that remain open are unaffected by execve().

0

精彩评论

暂无评论...
验证码 换一张
取 消