execute a program

i would like to start another program from within my program (like system()) but i dont want it to wait for the program to return before continuing (does that make sense?).
im using linux (X.. gtk+ if it matters).
thanks.
VickioAsked:
Who is Participating?
 
fstabConnect With a Mentor Commented:
Use the exec line of functions (exec, execl, execve ... etc.) These are POSIX std. functions which should be available on LINUX.
0
 
VickioAuthor Commented:
i cant seem to get any of the exec function to work..
either nothing happens or i get a Seg Fault.
can you give me a little example code?
0
 
fstabCommented:
This was compiled on NT - should work on LINUX too.

#include <stdio.h>
#include <process.h>

main(int argc, char *argv[])
{
  int count;
  char **cmd_to_exec;
  int flag=0;
  printf("argc is %d\n",argc);
  for(count=0;count < argc; count++) {
     printf("%s ", argv[count]);
     if (!strcmp(argv[count],"-exec") && (count+1) == argc-1) {
        cmd_to_exec = &argv[count+1];
       flag = 1;
     }
  }
  if (flag)
      execvp(cmd_to_exec[0],cmd_to_exec);
}
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
VickioAuthor Commented:
hmm..
i dunno if its the exec i need..
(this is an X program)
i want it to be able to start a program and still stay running itself.
i looked at the source code of a couple programs that do this and they used system() to start the programs... it doessnt work right for me though..
0
 
VickioAuthor Commented:
ive also seen the use of an exec and fork to do it.. i cant really figure out what they do there either though..
0
 
fstabCommented:
OK here's what you do -

main()
{
 int pid;
 pid = fork();
 if (pid == 0) { // child
   exec(program);
 } else if (pid) { // parent
   // do your stuff ..
 } else if (pid == -1)
   printf("fork failed");
}

Doing a fork *and* an exec ensures that the parent and the exec'ed program stay together.
0
 
VickioAuthor Commented:
ok, got it.

thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.