LinuxQuestions.org
Visit Jeremy's Blog.
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 08-01-2003, 12:55 PM   #1
Alf829
Member
 
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.

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

Rep: Reputation: 33
hi,
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....

simple...
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
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
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:
Code:
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
Alf829
Member
 
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

Cheers,
alf829
 
  


Reply


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


All times are GMT -5. The time now is 02:05 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration