C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2008, 04:48 PM   #1
Registered User
 
Join Date: Aug 2008
Posts: 2
Calling other binaries, use fork/execl or system?

I know of two approaches
1) fork, dup2, execl (you can see this approach in minicom or busybox telnet)
2) system(command)

I'm used to going through method #1 but a friend pointed out that you can do the redirection within the shell via "1>&<file descriptor>" in the command string. This moves about 30 lines of code down to aout 2-3.

Can anyone lend some insight as to why one might prefer method #1 over method #2 or vice versa? Even thoguht I first learned to do this kind of thing via method #1, I'm favoring method #1.

Thoughts?

-spot
spotvt01 is offline   Reply With Quote
Old 08-15-2008, 05:06 PM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
There's also popen(). It really depends what you want.

fork/exec is the low-level variant. You have a lot of control over the child process this way, such as the ability to freely manipulate its file descriptors, its environment, etc.

system is not suitable if you want to communicate with the child, since it blocks until the child exits. If that's fine with you, it's the easiest way of starting a subprocess.

popen is best if you want a child process to communicate with but you don't need much control. It basically does the fork/exec thing, passing the first argument as the command line to an interpreting shell (/bin/sh on Linux), and capturing the read or write stream in a C FILE*. The biggest limitation of popen is that you cannot both input and output - the resulting FILE* is never read-write.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 08-15-2008, 05:53 PM   #3
Registered User
 
Join Date: Aug 2008
Posts: 2
rather, let me say I'm favoring method #2 ... a lot less code

Thanx for responding CornedBee ....
Are we talking about a large difference in overhead b/n system / fork&exec ?

-spot
spotvt01 is offline   Reply With Quote
Old 08-16-2008, 04:20 AM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
Well, system() forks once to start a shell, and the shell will probably fork again to execute your program.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux database system needed BobS0327 Tech Board 7 06-11-2006 03:56 PM
measuring system resources used by a function Aran C Programming 1 03-13-2006 05:35 PM
New system build wont boot lightatdawn Tech Board 7 12-02-2005 06:58 AM
BIOS system and memory allocation problem beely Tech Board 9 11-25-2003 07:12 AM
Problem Reporting System. Need Advide! brunomiranda Tech Board 9 09-25-2003 09:21 PM


All times are GMT -6. The time now is 04:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22