using exec to execute a system command in a new process using exec to execute a system command in a new process linux linux

using exec to execute a system command in a new process


You're missing a call to fork. All exec does is replace the current process image with that of the new program. Use fork to spawn a copy of your current process. Its return value will tell you whether it's the child or the original parent that's running. If it's the child, call exec.


Once you've made that change, it only appears that you need to press Enter for the programs to finish. What's actually happening is this: The parent process forks and executes the child process. Both processes run, and both processes print to stdout at the same time. Their output is garbled. The parent process has less to do than the child, so it terminates first. When it terminates, your shell, which was waiting for it, wakes and prints the usual prompt. Meanwhile, the child process is still running. It prints more file entries. Finally, it terminates. The shell isn't paying attention to the child process (its grandchild), so the shell has no reason to re-print the prompt. Look more carefully at the output you get, and you should be able to find your usual command prompt buried in the ls output above.

The cursor appears to be waiting for you to press a key. When you do, the shell prints a prompt, and all looks normal. But as far as the shell was concerned, all was already normal. You could have typed another command before. It would have looked a little strange, but the shell would have executed it normally because it only receives input from the keyboard, not from the child process printing additional characters to the screen.

If you use a program like top in a separate console window, you can watch and confirm that both programs have already finished running before you have to press Enter.


The Exec family of functions replaces the current process with the new executable.

To do what you need, use one of the fork() functions and have the child process exec the new image.

[response to update]

It is doing exactly what you told it: You don't have to press "enter" to finish the program: It has already exited. The shell has already given a prompt:

[wally@zenetfedora ~]$ ./zbefore22397done[wally@zenetfedora ~]$ 0               << here is the prompt (as well as the pid)total 5102364drwxr-xr-x.  2 wally wally       4096 2011-01-31 16:22 Templates...

The output from ls takes awhile so it buries the prompt. If you want output to appear in a more logical order, add sleep(1) (or maybe longer) before the "done".


You're missing the part where execl() replaces your current program in memory with /bin/ls

I would suggest looking at popen() which will fork and exec a new process, then let you read or write to it via a pipe. (Or if you need read and write, fork() yourself, then exec())