does system funtion create fork in linux

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adamSpline
Forum Newbie
Posts: 11
Joined: Fri Sep 16, 2011 1:30 pm

does system funtion create fork in linux

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: does system funtion create fork in linux

Post 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?
adamSpline
Forum Newbie
Posts: 11
Joined: Fri Sep 16, 2011 1:30 pm

Re: does system funtion create fork in linux

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: does system funtion create fork in linux

Post 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.
Post Reply