Question about memory leak happening in the child process
Assume that the function func has bugs so that it leaks memory.
pid_t childPid;
int status;
childPid = fork();
if (childPid == -1)
errExit("fork");
if (childPid == 0) /* Child calls func() and */
exit(func(arg)); /* uses return value as exit status */
/* Parent waits for child to terminat开发者_运维知识库e. It can determine the
result of func() by inspecting 'status'. */
if (wait(&status) == -1)
errExit("wait");
Question 1> If a program leaks memory, after the program exits in the end, does it still leak memory or system will collect all memory allocated by the program and there is no more leaked memory?
Question 2> After the parent process calls the wait, how is the leaked memory caused by the func in child process?
If a program leaks memory, after the program exits in the end, does it still leak memory or system will collect all memory allocated by the program and there is no more leaked memory?
The system will collect all the memory resources from the child, and there will no longer be any leaked memory from the child process. Also a call to fork()
separates the memory space of the parent and child, therefore a leak in the child process will not leak in the parent process unless you call the same buggy function in both.
After the parent process calls the wait, how is the leaked memory caused by the func in child process?
Calling wait()
in the parent, and the child leaking memory really have nothing to-do with each other. The call to wait()
in the parent merely causes the parent to block waiting for a signal indicating that a child process has completed. The child will still have to call func()
first before it can complete, since it must pass the return value of func()
to exit()
. Therefore func()
can still technically "leak" memory in that it allocates some memory on the heap, but doesn't clean it up, even though the clean-up actions by the OS takes place almost immediately after func()
is called. In other words after the call to exit()
is complete, the OS has released the resources used by the child, but func()
itself can still fail to free any memory it tried to allocate.
Any modern operating system won't allow the child process (or any process) to leave the system in an inconsistent state after its termination.
If the function is buggy in the sense that it leads to a segmentation fault, the OS will usually kill the process with the signal SIGSEGV
right after the offence and the parent will be acknowledged through the wait
that the child has exited with a signal, not normally.
Memory leak is just one kind of resource leak.
The memory pages allocated with, for example malloc() are called "Private pages" because they belong to just one process (they might be copy-on-write shared with a parent or child, but they are still its own pages).
However, there are lots of other ways of leaking resources. Some types of shared object don't get automatically cleaned up; files in the filesystem won't be automatically cleaned up, nor will child processes created by your child get automatically reaped when it exits.
精彩评论