Solved

execute a program

Posted on 1998-11-12
7
177 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 70 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now