LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2006, 09:28 AM   #1
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Rep: Reputation: 0
Question Memory leak of fork()


Today, I wrote a test code for fork/execvp/waitpid. In the parent process, it fork 100 child processes which only execute "date" to print the current datetime. When any child process die, the parent process will receive a SIGCHLD signal. Then, the parent process will re-fork-execvp the child process again.

I find the system memory(NOT includes the buffers and caches) will go higher and higher and even I shutdown all of the parent process and the child processes, it will not go down. Why?

My environment is Linux 2.6.9 and g++ 3.4.2

The following is my code:

//////////////////////////////////////////////////////////////////////////////////
// fork.cpp
// g++ -Wall fork.cpp -o fork

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

char* argv[] = { "data", (char*)0 };

void run()
{
// Child process
if (fork() == 0)
if (execvp("date", argv) < 0)
exit(0);
}

// SIGCHLD signal handler
void sig_child(int sig)
{
pid_t pid;
int stat;
while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
run();
}

int main()
{
// Setup signal handler
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sig_child;
sigemptyset(&act.sa_mask);
if (sigaction(SIGCHLD, &act, 0) < 0)
return -1;

// Run 100 child processes
for (int n = 0; n < 100; n++)
run();

// Zzz...
for (;
pause();
return 0;
}

//////////////////////////////////////////////////////////////////////////////////

Thanks,
Richard
 
Old 05-09-2006, 03:50 PM   #2
Michael_S
Member
 
Registered: Oct 2004
Location: Pennsylvania, USA
Distribution: Debian
Posts: 87

Rep: Reputation: 36
http://man.he.net/man2/waitpid
[EDIT] I'm wrong, forget it.

[EDIT 2] This is a fun one. Are you sure you want the WNOHANG flag? I think that indicates the function should return even if the found process is not finished.

Last edited by Michael_S; 05-09-2006 at 04:01 PM.
 
Old 05-09-2006, 05:18 PM   #3
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks.

From "man waitpid"

WNOHANG which means to return immediately if no child has exited.

If I don't use WNOHANG, "while ((pid = waitpid(-1, &stat, 0)) > 0)" will run for ever except -1 return.

From lots of examples and books, it looks "while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)" is OK. For example, Richard Stevens's UNIX Netowrk Programming's example.

Also, I tested without WNOHANG, the memory still go higher.
 
Old 05-09-2006, 06:59 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What processes are still running?
Are the processes from your execvp still running when you're looking at your memory?
 
Old 05-10-2006, 08:25 AM   #5
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
No child processes are running because it is just "date" which only prints the current datetime and exit immediately.

=============================================
char* argv[] = { "date", (char*)0 };

void run()
{
// Child process
if (fork() == 0)
if (execvp("date", argv) < 0)
exit(0);
}

=============================================
 
Old 05-10-2006, 12:03 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by whererush
No child processes are running because it is just "date" which only prints the current datetime and exit immediately.
Did you check that no processes are running with a ps -Al ?

because execvp replaces the running process I was wondering if any zombies are left hanging about
 
Old 05-10-2006, 01:52 PM   #7
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by graemef
Did you check that no processes are running with a ps -Al ?

because execvp replaces the running process I was wondering if any zombies are left hanging about
Yes, I already check. There is not any child processes and no zombies ( I already call waitpid in a loop).

When I kill parent process, even there are some child processes which are not "waited" by parent process, they will be "waited" by init daemon. So I don't think the memory "disappeared" are used by both parent process or child processes.
 
Old 05-11-2006, 09:19 AM   #8
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
After check /proc/meminfo, I found the memory gone is LowFree. I think this means kernel memory leak, right?
 
Old 05-11-2006, 10:51 AM   #9
whererush
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
When I test on other linux with version 2.6.9-34, everything is fine. The leaking version is 2.6.9-1.

Thanks guys.
 
Old 05-12-2006, 04:36 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Never trust a .1 release
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is memory leak blackzone Programming 6 11-05-2004 10:32 AM
memory leak mfitzpat Linux - Newbie 1 09-24-2004 02:58 PM
memory leak mdk Mandriva 1 09-17-2004 10:54 AM
possible memory leak?? matt80 Linux - General 2 07-01-2004 11:07 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration