In a process using lots of memory, how can I spawn a shell without a memory-hungry fork()?

By : kdt
Source: Stackoverflow.com
Question!

On an embedded platform (with no swap partition), I have an application whose main process occupies most of the available physical memory. The problem is that I want to launch an external shell script from my application, but using fork() requires that there be enough memory for 2x my original process before the child process (which will ultimately execl itself to something much smaller) can be created.

So is there any way to invoke a shell script from a C program without incurring the memory overhead of a fork()?

I've considered workarounds such as having a secondary smaller process which is responsible for creating shells, or having a "watcher" script which I signal by touching a file or somesuch, but I'd much rather have something simpler.

By : kdt


Answers

Instead of forking your process to spawn a shell, launch a shell within your process (in foreground) then fork it within the shell.

 system("/bin/ash /scripts/bgtask");

with /scripts/bgtask being:

 /bin/ash /scripts/propertask 
By : SF.


It sounds as if the prudent move in this case is to port your shell script (if possible) to C, and executing it within the process; so you don't have to fork at all.

Then again; I don't know what you are actually trying to do.



I see you've already accepted an answer, but you may want to read about posix_spawn and use if it if it's available on your target:

http://www.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html

By : R..


This video can help you solving your question :)
By: admin