Sign in to follow this  

Quick Question

Recommended Posts

I'm writing a front end for a project i'm working on and need to launch programs from it. I totaly do not remember howto do this(or know how to do this, as long as this doesn't start a flame war). So can anyone please post on howto run executables in windows and/or linux? THANKS,

Share this post

Link to post
Share on other sites
For the sake of completeness, if you are trying to accomplish this under UNIX based operating systems the function you are most likely looking for is:

int execve(const char *path, char *const argv[], char *const envp[]);

located in unistd.h


Share this post

Link to post
Share on other sites
Original post by twanvl
actually you would probably want to use spawn.. instead of exec.., exec replaces the current progran, spawn creates a new process. Both work on windows too.

Under windows ShellExecute/ShellExecuteEx can be used to start processes, however CreateProcess gives much more fine grained control.

Execve will do what he needs to do, however he must note that it will not return to the original calling process as it replaces the current process image with a new one. In order to mitigate this to create a multi-use application launcher you would have to fork the process before using execve.


#include <unistd.h>

int main(int argc, char **argv)
int x;
/* you need to provide the parameters to pass to the
* application you are executing. The first parameter
* should be the name of the application you are execve'ing
* and corresponds to argv[0] in the new process.

char *c[] = { "ls", "-l", 0 };
/* you can also provide an environment as follows:
* I have included sample values based on my systems

char *e[] = { "HOME=/cluster5/home/yboily", "HOST=cluster1", 0 };

x = fork();
/* fork is wierd; it returns "twice"; when you are writing your
* code it is important to process the return value for fork
* a return value of -1 indicates that an error occured
* a return value of 0 indicates that you are in the child process
* a return value other than this is the PID of the original process
* what we want to do is execve in the child process to launch our
* new process while leaving the orginal intact.

case -1:
printf("error, unable to fork process!\n");
case 0:
printf("This is the forked process!\n");
execve ("/bin/ls", c, e);
/* should not be reached */
printf("Error executing process!\n");
printf("This is the original process!\n");

This results in the following:

Script started on Thu Sep 2 11:30:43 2004
yboily@yboily:~/demo> gcc -o exectest test.c
yboily@yboily:~/demo> ./exectest
This is the original process!
This is the forked process!
yboily@yboily:~/demo> total 20
-rwxr-xr-x 1 yboily yboily 5122 Sep 2 11:30 exectest
-rw-r--r-- 1 yboily yboily 1320 Sep 2 11:26 test.c
-rw-r--r-- 1 yboily yboily 43 Sep 2 11:30 typescript

yboily@yboily:~/demo> exit

Script done on Thu Sep 2 11:31:01 2004

For the record, I was unable to find documentation on spawn() as a means to create a process under Win32 Platform SDK, the FreeBSD man pages, or Red Hat Linux man pages

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this