Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-15-2007, 12:30 PM   #1
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
fork() exec() or system()


so is it any disadvantage using system("program &") than a fork and a exec?
Old 09-15-2007, 01:14 PM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Yup: when you execute system() you're giving a command line to the shell for execution and there is an enormous amount of overhead associated with each system() call; it's easy, but it's inefficient.

The fork() and exec() routines are vastly more efficient but you do have to do a little fooling around with getting the arguments right.
Old 09-16-2007, 11:09 AM   #3
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 76
There is also a slight security problem with using system(). This is because POSIX has left the default shell’s path unspecified (which usually means there is no historical agreement). With all modern glibc systems (i.e., most linux systems) the path is hardcoded as “/bin/sh” (and usually that’s a symlink to the desired interpreter—e.g., bash). This is all well and good, since you’d need superuser permissions to fiddle with the symlink. Some braindead designers decided it would be a good idea to glean the shell’s path from the SHELL environment variable (in modern POSIX, this has been officially discouraged at the end of the RATIONALE section of the linked POSIX spec). As such, when you move your application to these platforms, you now have a security risk when using system(): An attacker may change the environment variable (this is still nontrivial, but much easier than changing the symlink). There are other subtler security risks that come with executing your program through a shell. Moral of the story: don’t use system() for setuid/gid applications (this is also stated in the RATIONALE section of the POSIX spec for system()).

Also, a fork() followed by an exec() will create a new process from the parent, then replace the child with the image of a process started by exec(). Thus there will be two concurrent processes doing concurrent activities. With system(), a similar thing happens, except the parent process is halted, some signals are being ignored, and the parent waits for the child to complete before proceeding. So you basically have sequential execution (your program pauses to execute another program, and continues when the other program is done).
Old 09-16-2007, 04:36 PM   #4
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Aside from the security implications, 'system' will allow you to expand command line expressions such as *, ?, ~, and `` and 'exec' won't. You can also change the user and group IDs after forking before the 'exec' call, making a su program potentially safer.

When you call 'system' you get a 'fork' and 'exec' anyway, but the thread of control blocks until that call completes.

About the only reason to use 'system' is that you can execute a command line as it would be typed by hand without having to parse it and/or manage a fork/exec operation: One line does it all. The problem with that is not all shells interpret things the same way. My "default" default shell on FreeBSD is quite a bit different from that on my Slackware system.
Old 09-18-2007, 04:16 AM   #5
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Thanks a lot.

I have to use fork and exec since I have a critical process and it ca not ignor any signal.

"With system(), a similar thing happens, except the parent process is halted, some signals are being ignored, and the parent waits for the child to complete before proceeding."


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
the difference of using fork() and exec() with system() jaepi Programming 12 06-05-2007 07:14 AM
Fork exec problem bhupeshchawda Programming 3 04-19-2007 04:58 AM
how to fork/ exec schneidz Programming 10 08-30-2005 11:50 AM
Programming: fork, exec and system memory atienza Programming 2 08-24-2005 07:26 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration