C programming question

Duston, Hal hdusto01 at sprintspectrum.com
Fri Jan 4 17:53:58 CST 2002


Jeremy Fowler [mailto:jfowler at westrope.com] wrote:
> 
> This question is a bit off topic and I don't know if there 
> are any C programmers here, but I thought I would start here 
> before I posted to a C newsgroup.
> 
> How do you clean up dynamic memory in a child process after 
> calling execve? Or should I not even worry about it since 
> execve loads a new program into that memory space anyway.

execve() completely replaces the memory, so you have no 
worries here.

> Say you have a command string your going to pass to execve. 
> After I fork a new process, the command string is parsed into 
> an array of strings allocated from dynamic memory. Since that 
> process is then replaced with the process called by execve, 
> do I have to worry about that memory I allocated? Does the 
> Kernel clean that up for me when the process terminates, or 
> do I have to clean that memory up from the parent process?

See above.

> Does anyone know how fork() handles dynamically assigned 
> memory (malloc) that existed _before_ the call to fork()? 
> Does the new child process get a complete copy of the memory 
> allocated or just a copy of the address of the original
> allocated memory?

fork() makes copies of pretty much _everything_ in the process
at the time of the fork().  This includes the open fd's, and 
all memory.  execve() replaces the memory, but inherits the 
fd's unless the were opened/fctl'd to be "close_on_exec".

> Thanks, -Jeremy

Hal




More information about the Kclug mailing list