LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-08-2005, 10:06 AM   #1
vrdhananjay
Member
 
Registered: May 2004
Distribution: red hat 9
Posts: 56

Rep: Reputation: 15
very urgent!details about execve()???????


hello,
my app needs gnuplot as a backend.someone suggested that i use execve()...
i was reading the man pages and noticed that execve() does not return on success...
i dont know how to use it yet.....my argument is going to be an array of integers....
i dint get what env[] was for?

can someone please explain me how to use this?in a really simple way...
also i want something that returns on sucess cos' i want to display the graph and then continue with my app?

pls help...project due day after...
thanks
dhananjay
 
Old 02-08-2005, 10:13 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You'll need to fork() first. Call execve() in the child process and in the parent process use one of the wait() functions to wait for the child to exit.
 
Old 02-08-2005, 10:19 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
More information: You can't pass an array of integers to execve(). It needs to be an array of strings like it says in the man page. You'll need to do some conversion on the array it sounds like.

If you don't want to pass env[] to the program then use execv() instead of exeve().
 
Old 02-08-2005, 10:19 AM   #4
vrdhananjay
Member
 
Registered: May 2004
Distribution: red hat 9
Posts: 56

Original Poster
Rep: Reputation: 15
hey,
thanks for that ....
would you happen to have a snippert of code with both fork() and execve() used....cos i dont know much aboutr these...
thanks
djhananjay
 
Old 02-08-2005, 10:26 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's a simple example:
Code:
itsme@dreams:~/C$ cat p1.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
  char *args[] = { "./p2", "beefy", "meat", "is", "good", NULL };

  if(!fork())
    execv("./p2", args);

  puts("Waiting for child to exit...");
  wait(NULL);
  puts("Child exited.");

  return 0;
}
Code:
itsme@dreams:~/C$ cat p2.c
#include <stdio.h>

int main(int argc, char **argv)
{
  puts("Program started. Arguments are:");
  while(*argv)
  {
    printf("  %s\n", *argv);
    argv++;
  }

  return 0;
}
Code:
itsme@dreams:~/C$ ./p1
Waiting for child to exit...
Program started. Arguments are:
  ./p2
  beefy
  meat
  is
  good
Child exited.
 
Old 02-08-2005, 01:10 PM   #6
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
Just so you understand how fork works: Fork creates a copy of your process then returns zero (0) to the child process and returns nonzero (the child's pid) to the parent. This is how all processes are created in Linux (everything forks off of init pretty much).

I thought it was important to put an exit in the child code to prevent zombies. No?

Code:
if( !fork()) {
  // Child Code
  execv("./p2",args);
  exit(0);
}
 
// Parent Code
...
 
Old 02-08-2005, 01:26 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
I thought it was important to put an exit in the child code to prevent zombies. No?
Not really in this case anyway:
Code:
DESCRIPTION
       The exec family of functions replaces the current  process
       image  with  a new process image.  The functions described
       in this  manual  page  are  front-ends  for  the  function
       execve(2).   (See  the manual page for execve for detailed
       information about the replacement of the current process.)
Since the process is replaced with the new image, the exit() call is never reached anyway.

Usually, I think it's wait()'ing for the child to exit that reaps the zombies.
 
Old 02-08-2005, 01:39 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
Usually, I think it's wait()'ing for the child to exit that reaps the zombies.
My suspicions are confirmed. Here's an excerpt from man wait:
Code:
DESCRIPTION
       The  wait  function suspends execution of the current pro-
       cess until a child has exited, or until a signal is deliv-
       ered  whose  action is to terminate the current process or
       to call a  signal  handling  function.   If  a  child  has
       already  exited by the time of the call (a so-called "zom-
       bie" process), the function returns immediately.  Any sys-
       tem resources used by the child are freed.
 
Old 02-08-2005, 02:07 PM   #9
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 31
Ok that makes sence. The exec() exits the process automatically (assuming the replacing process exits correctly)

However, rdhananjay, just for reference: you will want the exit() if you are forking and not using an exec (or similiar function) in the child code.

Peace
 
Old 02-08-2005, 03:00 PM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
your question is *not* urgent for any of our members who kindly donate their time to LQ.org, plese do not mark threads as urgent in the future. thanks
 
Old 02-08-2005, 05:04 PM   #11
vrdhananjay
Member
 
Registered: May 2004
Distribution: red hat 9
Posts: 56

Original Poster
Rep: Reputation: 15
ll keep that in mind...
 
Old 02-08-2005, 05:07 PM   #12
vrdhananjay
Member
 
Registered: May 2004
Distribution: red hat 9
Posts: 56

Original Poster
Rep: Reputation: 15
This is what i used eventually,seems to work okay....
will replace the return 0; with exit(0) and try it...

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{ pid_t me, a;
char *args[] = {"/usr/bin/gnuplot","gpl.txt",NULL};

me = fork();
if(!me)
execv("/usr/bin/gnuplot", args);

puts("Waiting for child to exit...");
wait(NULL);
puts("Child exited.");
return 0;
}

thanks guys
dhananjay
 
  


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
strace only shows execve Quigi Linux - Software 2 10-27-2005 01:44 PM
Linux syscall 11 (execve) Genjix Programming 1 12-29-2004 11:38 PM
Keeping a lock across execve()? LogicG8 Programming 10 10-30-2003 12:19 PM
Using execve() not working cxel91a Programming 3 09-03-2003 06:26 AM
fork() and execve() Alf829 Programming 3 08-02-2003 10:18 AM

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

All times are GMT -5. The time now is 07:06 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