Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to system() or fork()/exec()?

Tags:

c++

c

fork

There appear to be two common ways of running an external executable from C in unix, the

system()

call and

pid = fork()
switch(pid)
//switch statement based on return value of pid, 
//one branch of which will include and exec() command

Is there any reason to prefer a fork/exec over system in the case where they are functionally equivalent (parent process waits for child to finish, no complex information is returned from child)?.

like image 750
Sparky Avatar asked Feb 12 '13 10:02

Sparky


People also ask

What is the difference between fork () and exec () system calls?

The fork() returns the PID of the child process. If the value is non-zero, then it is parent process's id, and if this is 0, then this is child process's id. The exec() system call is used to replace the current process image with the new process image.

Does system () use fork?

The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child.

What is exec () and system ()?

system() will execute the supplied command in a child process that it spawns. exec() will replace the current process with the invocation of the new executable that you specify.

What is the use of fork () and exec () in Unix?

In a UNIX operating system, the fork is a command that allows a process to copy itself. However, in a UNIX operating system, exec is a command that creates a new process by replacing the existing one. The fork() makes a child's process equal to the parent's process.


2 Answers

system executes a command-interpreter, i.e. a shell, which (a) is slower than a direct fork/exec, (b) may behave differently on different systems and (c) is a potential security hazard if you pass it a string from an untrusted source. Also, system waits for the child process to exit, while you might want it to run concurrently with the parent process.

More in general, the low-level fork/exec gives you additional control: before or in between the two operations, you might want to chdir, open pipes, close file descriptors, set up shared memory, etc.

(By different systems, I don't mean Windows vs. Unix (as Windows doesn't even have fork): I'm talking Red Hat Linux vs. Ubuntu. The former uses Bash to execute what is passed to system, the latter a lightweight POSIX-compatible shell.)

like image 182
3 revs Avatar answered Oct 20 '22 21:10

3 revs


fork() creates a new process. If you don't need to do that, just use system() (or popen()). You might want a second process to achieve parallelism, or for finer-grained control over the job, but often you just don't care for that if the job is meant to be synchronous.

On the other hand, I find that 95% of uses of system() are unnecessary or would somehow be better off done another way (e.g. using zlib instead of system("gzip")). So maybe the best answer is to use neither!

like image 33
John Zwinck Avatar answered Oct 20 '22 20:10

John Zwinck