Page 1 of 1
does system funtion create fork in linux
Posted: Fri Apr 27, 2012 12:01 am
by adamSpline
Hi all,
I am trying to figure out how the system() function works in Linux. I imagine that it creates a fork of a process. If so, what process does it fork? What would the parent process be? Does it fork the entire apache server (I hope not, and I know this sounds odd, but this is exactly what happens in Java to the tomcat server, see
http://stackoverflow.com/questions/2876 ... ternatives). Or does it just do a small fork on the process that is handling the request? Any help would be great. Thanks.
-Adam
Re: does system funtion create fork in linux
Posted: Fri Apr 27, 2012 12:35 am
by requinix
system() is a wrapper for exec() which invokes the program using
popen(3); that's where the actual
forking happens. Meanwhile Apache is either using
worker (child processes have a single thread for each request) or
prefork (whole child process for the request).
It sounds like you might have a more specific question?
Re: does system funtion create fork in linux
Posted: Fri Apr 27, 2012 1:13 pm
by adamSpline
Thanks for your reply. You are correct, my question could have been more specific, but my lack of experience with Apache led to the vague question. You clarification of worker vs prefork was helpful.
In Java/JSP, when you do a runtime.exec call (roughly equivalent to php system exec), it performs a fork on the parent process. With Java/JSP on the tomcat server, the parent process is actually the tomcat server, so it forks the entire tomcat java process just to do a simple system call. This is bad because the fork requests the amount of memory equal to the parent process (in this case the large tomcat server process). This can at times lead to memory problems just to perform a simple shell command.
In general, my question is: will I have the problem with php on apache2? It appears I am using prefork. So, I assume the system call will simple request a fork on the child request process. This fork should have a small memory request because the parent process would just be the small child request process. Is this accurate? Am I missing anything?
-Adam
Re: does system funtion create fork in linux
Posted: Fri Apr 27, 2012 3:49 pm
by requinix
Sounds about right. I don't know a whole lot about the Apache/prefork internals but yes: forking will clone most/all of the current process and the thread that did the fork. But IIRC Linux's fork uses copy-on-write, and since the thread will just exec itself to something else that shouldn't really result in much extra memory used.