ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
system() and execvp() both work by creating a new process.
Question:
Do they create new processes by internally calling fork() or there is some other way ?
system() calls fork(). execvp() doesn't call fork(), but that's not because execvp() creates a new process by another way. It's because execvp() doesn't create a new process at all. It uses the old process, taking place of the program which did the execvp().
Well then does it mean execvp() is less costly than system() ?
And does it mean fork() and system() cost the same ?
Then why to use system() when it calls fork() internally, we can use fork() directly ?
First, let me correct myself. You may get slightly better performance by using vfork() instead of fork(), but be sure you read the fine print in the man page.
Ignoring that issue, when you want to create a subprocess and have it run something, you can either:
use system(); or
use fork(), and in the child process use one of the members of the exec*() family, such as execvp.
This means that if you're going to use execvp(), you had better use fork() (or vfork()) with it, and not system(), not for performance reasons, but for bug avoidance reasons.
You can run a program for a while, and then use something from the exec*() family to run a different program, replacing the one you're running, in the same process. You want a different process? Use fork() or vfork() first.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.