Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-01-2003, 12:55 PM   #1
Registered: Jul 2003
Distribution: Fedora Core 2 & 5
Posts: 110

Rep: Reputation: 15
fork() and execve()

Hi all,

In C, if I fork() a process, how could I get execve() to execute the program I specify on the new child process rather than the parent process?

Does it have something to do with the return values?

Thanks in advance for any help.

Old 08-01-2003, 01:38 PM   #2
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
first a small piece of CODE
pid_t pid;

if ( (pid = fork()) == 0)
//in the child process
execve(desired proggy.....);//run the desired program in
//childs addresss space


now the explanation: fork returns twice , once in child (with value
'0') and once in the parent (with the pid of child process)..
when you are inide the child process, you can do an exec and run the desired program in the child process....

now...well..sort o yes, it has to do somethin with the return valuse...but those of fork,,bcoz, if fork returns '0' it means child is ready to run , otherwise the parent continues on its path....
Old 08-01-2003, 01:38 PM   #3
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
yes it is related to the return values. a simple check of the man page says that fork returns -1 on error, 0 to the child, and the pid of the child to the parent so something like the following:
pid_t child;

if((child = fork()) == -1) {
    //error occured

if(child == 0) {
    //we're inside the child so exec here
} else {
    //we're in the parent so continue with parent stuff
Old 08-02-2003, 11:18 AM   #4
Registered: Jul 2003
Distribution: Fedora Core 2 & 5
Posts: 110

Original Poster
Rep: Reputation: 15
Thank you both for your help

The thing I wasn't getting was how to reference to the child rather than the parent and you've provided the perfect answers



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
strace only shows execve Quigi Linux - Software 2 10-27-2005 02:44 PM
assembly execve (syscall 11) rblampain Programming 2 07-03-2005 10:23 AM
very urgent!details about execve()??????? vrdhananjay Programming 11 02-08-2005 06:07 PM
Keeping a lock across execve()? LogicG8 Programming 10 10-30-2003 01:19 PM
Using execve() not working cxel91a Programming 3 09-03-2003 07:26 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration