Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

execute a program

Posted on 1998-11-12
7
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 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