From bug-request at octave dot org Wed Apr 5 09:50:42 2006 Subject: Re: system() fails in high memory situation From: "Tom Holroyd (NIH/NIMH) [E]" To: "John W. Eaton" CC: Paul Koufalas , bug@octave.org Date: Wed, 05 Apr 2006 10:48:49 -0400 John W. Eaton wrote: > Octave just calls the system() function from the C library. So if > this is a bug, then I think the place to fix it is in the C library. > I would expect that you should be able to duplicate the problem with a > simple C program that allocates a lot of memory (you may have to > actually store some values in it in addition to allocating it) and > then calls system ("date"). Then you'd have something to report to > the maintainers of the C library on your system. You're right, of course. Here is such a program. --- 8< --- #include #include #include int main(int argc, char **argv) { int i, j, *p; for (i = 0; i < 500; i++) { p = malloc(1024 * 1024 * sizeof(int)); if (p == NULL) { exit(1); } for (j = 0; j < 1024 * 1024; j++) { *p++ = j; } } #if 0 system("date"); #else if (vfork() == 0) { execl("/bin/sh", "sh", "-c", "date", NULL); } #endif printf("Bgawk!\n"); } --- 8< --- My box has 2 GB real and 2 GB swap. So the loop allocates about 2 GB, plus with other stuff running, a true fork() becomes impossible. So with #if 1, the system() fails, since it uses fork(). But with #if 0, the program works fine. There are other ways of running "date" of course, but I chose the execl() line to approximate what system() does. Perhaps system() could be changed to be more robust in this case, yes, and I might mention that to the Fedora people, but it's not so unreasonable to do something like the above in Octave, since large workspaces are common and modern boxes often have configs like mine, with 2 GB of stuff (mostly in swap) and the rest of the system is still working fine. However, I haven't investigated the other paths that Octave might use to fork/exec. In other words, use of system() may not really be appropriate in many cases for programs that can expect to work with large amounts of data, like Octave. There is also the question, however, of what happens on other systems. Mac OS 9? X? Cygwin? Do they even have vfork()? -- Tom Holroyd, Ph.D. I would dance and be merry, Life would be a ding-a-derry, If I only had a brain. -- The Scarecrow ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------