LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-15-2007, 01:30 PM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

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


HI,

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

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
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, 12:09 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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, 05:36 PM   #4
ta0kira
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.
ta0kira
 
Old 09-18-2007, 05:16 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

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."
 
  


Reply


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 08:14 AM
Fork exec problem bhupeshchawda Programming 3 04-19-2007 05:58 AM
how to fork/ exec schneidz Programming 10 08-30-2005 12:50 PM
Programming: fork, exec and system memory atienza Programming 2 08-24-2005 08:26 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 03:31 AM


All times are GMT -5. The time now is 03:20 AM.

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