Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

execute a program

Posted on 1998-11-12
7
Medium Priority
?
190 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:Vickio
  • 4
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
fstab earned 210 total points
ID: 1177696
Use the exec line of functions (exec, execl, execve ... etc.) These are POSIX std. functions which should be available on LINUX.
0
 

Author Comment

by:Vickio
ID: 1177697
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
 
LVL 1

Expert Comment

by:fstab
ID: 1177698
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Vickio
ID: 1177699
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
 

Author Comment

by:Vickio
ID: 1177700
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
 
LVL 1

Expert Comment

by:fstab
ID: 1177701
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
 

Author Comment

by:Vickio
ID: 1177702
ok, got it.

thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question