I am writing a shell program for a UNIX system for a project in an OS design class. I am now at the stage where i can call fork() and exec(). But the following just come into mind.
Simply put, in C/C++. the main function is decleared
This has never occured to me before (or i just had crappy instructors that did not talk about these things)
more precisely, the following comes into mind...
When i call fork(), memory space of the caller is mirrored in the child process. and so is the argv[] that is prepared before calling exec(). Once I call, say, exec("huh.exe", argv[]), the memory of that child process is wiped and replaced with huh.exe. The parent process (assuming the shell program) of course has argv[] on hand, which it should probably free up. But how about the child process? where does the argv[] that i passed to it go? how am i gonna free that up?
Some of you is going tell me that UNIX's memory system is going to reclaim those memory once the process ends, which is fine, but is there a way to gracefully free them up? (probably not from the parent process, coz the child gets a duplicate from the parent)
The memory leak really bothers me because i need to make the assumption that the memory subsystem would take care of it...
I appologize for the long post. I am not really good at expressing myself. Thank you for any advice.
edit: spelling...
Simply put, in C/C++. the main function is decleared
who is responsible for cleaning up the memory allocated for argv[]? the caller? (the shell) or the main function itself?code:<HR>Code:int main (int argc, char* argv[])
This has never occured to me before (or i just had crappy instructors that did not talk about these things)
more precisely, the following comes into mind...
When i call fork(), memory space of the caller is mirrored in the child process. and so is the argv[] that is prepared before calling exec(). Once I call, say, exec("huh.exe", argv[]), the memory of that child process is wiped and replaced with huh.exe. The parent process (assuming the shell program) of course has argv[] on hand, which it should probably free up. But how about the child process? where does the argv[] that i passed to it go? how am i gonna free that up?
Some of you is going tell me that UNIX's memory system is going to reclaim those memory once the process ends, which is fine, but is there a way to gracefully free them up? (probably not from the parent process, coz the child gets a duplicate from the parent)
The memory leak really bothers me because i need to make the assumption that the memory subsystem would take care of it...
I appologize for the long post. I am not really good at expressing myself. Thank you for any advice.
edit: spelling...