LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 02-17-2010, 07:11 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Internal working of system() and evecvp()


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 ?

Kindly guide !
 
Old 02-17-2010, 10:27 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by anishakaul View Post
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().
 
1 members found this post helpful.
Old 02-17-2010, 10:53 PM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks for the enlightening reply!
 
Old 02-18-2010, 12:23 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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 ?
 
Old 02-18-2010, 04:08 AM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by anishakaul View Post
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:
  1. use system(); or
  2. 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.
 
Old 02-18-2010, 05:08 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
the exec family are the process starters.

everything winds up being exec'd
 
Old 02-18-2010, 05:16 AM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by bigearsbilly View Post
everything winds up being exec'd
True.
Quote:
Originally Posted by bigearsbilly View Post
the exec family are the process starters.
False.

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.
 
1 members found this post helpful.
  


Reply



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
Internal pc speaker not working VGM Linux - General 4 02-14-2010 09:06 PM
An internal system error has occurred. maestro52 Fedora 1 09-12-2008 01:24 AM
Internal mail is not working kapil.kshirsagar Linux - Software 1 10-18-2007 08:36 PM
Internal Speaker isn't working Mistreated Linux - Software 4 02-20-2007 03:38 PM
Internal Winmodem Working? Soulful93 Linux - Hardware 7 05-27-2005 06:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 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
Open Source Consulting | Domain Registration