Re: weird fork problem



On Oct 31, 8:01 am, bel...@xxxxxxxxx wrote:
Finally it resulted to be a memory leak ... but it was funny because
neither the "free" command or "top" did not report excesive memory
usage.

Searching in ulimit, the only limitation that I may be breaking is the
stack size, which is limited to 8192 kB.

On 29 oct, 08:16, Amol Vaikar <amol.vai...@xxxxxxxxx> wrote:

On Oct 29, 2:22 am, bel...@xxxxxxxxx wrote:

Hi all,

I am having a strange issue with a fork call. After some runs, I
cannot call fork from my program. This is my code:

pid_t pID=-1;
while (pID<0)
{
cout << "trying to fork ... " << endl;
pID = fork ();
if (pID < 0) // failed to fork
{
cerr << "errno: " << strerror(errno) << endl;
cerr << "Failed to fork" << endl;
sleep(5);
}
}

After a number of runs that varies between 20 and 30, I have the
following error output:

trying to fork ...
errno: Cannot allocate memory
Failed to fork
trying to fork ...
errno: Cannot allocate memory
Failed to fork

And so on. But a look to free gives enough memory:

$ free -m
total used free shared
buffers cached
Mem: 1011 656 355 0 30
353
-/+ buffers/cache: 271 739
Swap: 1176 0 1176

I have about 355 MB of physical and more than 1GB of swap, which
should be enough.
I also had a look to the number of processes running:
$ ps aux | wc -l
111

And the system limitations:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
max nice (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 8183
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
max rt priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 8183
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

So I have no idea where the problem is ... any help please?

Thanks

I understand that you are running your program as a non-root user?
If that assumption is right, then does the number of child processes
that the parent process can spawn change if you are logged in as root
(or if the parent process is running on behalf of root)?
If the answer to that is yes, then as is mentioned in other comments
to this messae, you are probably reaching some resource limit while
running the parent process as non-root.
Try changing open files limit etc.
HTH.

A memory leak where? Concerning the fork() function or elsewhere in
your program?
It is a sad fact that malloc will almost never return NULL as a sign
there's no memory left,
nor will C++'s new operator, but instead they usually just block the
whole system, slow
it down or kill the process. Or is it just from my experience?

.



Relevant Pages

  • Re: fork and thread
    ... the parent process "read" access to the parent process' ... separate memory page /for that process/. ... Could you expand on the problems with Linux fork()? ...
    (comp.unix.programmer)
  • Re: weird fork problem
    ... Finally it resulted to be a memory leak ... ... Searching in ulimit, the only limitation that I may be breaking is the ... cannot call fork from my program. ... that the parent process can spawn change if you are logged in as root ...
    (comp.unix.programmer)
  • Re: why i value doesnt change ?
    ... parent process why? ... Looks like you have yet to understand what "process" means or fork() ... process has it's own memory, the OS sets things up that the memory ... Nobody pointing out that some of the ...
    (comp.lang.c)
  • Re: C# Fork Help
    ... If I remember correctly fork starts a new process on Unix systems, which has the same memory content as it's parent process and the application continues processing by returning from the fork function call in both processes. ... In contrast to Windows, where a new started process will start at it's main entry function and which hasn't "copied" or better said mapped all the memory of it's parent process. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Returning data from a sub-process to its parent.
    ... fork(), to its parent process. ... There are many ways to do that in the general case: pipes, ... of memory that would already be shared by these two processes (even ...
    (comp.unix.programmer)