Re: Spawning process with environment variables
- From: Rainer Weikusat <rweikusat@xxxxxxxxxxx>
- Date: Thu, 24 Jul 2008 18:54:51 +0200
Sasha <agalkin@xxxxxxxxxxx> writes:
On Jul 24, 10:56 am, Rainer Weikusat <rweiku...@xxxxxxxxxxx> wrote:
Sasha <agal...@xxxxxxxxxxx> writes:
I am not UNIX programmer and I need to do quite a simple task - launch
process with command line and custom env variables and get return code
from spawn process' main. I tried using execle but it seems when child
process exits parent process exits too.
The exec-calls cause the processing performing them to execute the new
file. The simple way to solve this is to call fork before. This
creates a new process executing the same program the calling process
executed with the same state. The fork-call will return both in the
old and in the new process, returning the pid of the newly created
process to the orignal one and 0 to the other.
What point in the original parent process does the control flow return
into? I need to do the following:
- call another process with command line arguments and env variables
- wait until child process exits
- read its exit return code (what child process' main returns)
How can I do this in UNIX?
Uncompiled code sample:
int run_command(char **cmdv, char **envp)
{
pid_t pid;
int status;
pid = fork();
switch (pid) {
case -1:
perror("fork");
return -1;
case 0:
/*
this is the child
*/
execve(*cmdv, cmdv, envp);
perror("execve");
_exit(123);
}
/*
and this the parent, pid contains child pid
*/
waitpid(pid, &status, 0);
if (WIFEXITED(status)) return WEXITCODE(status);
/*
process terminated 'abnormally'
*/
return -1;
}
Minus errors, this should either return the exit code
of the command or -1 to indicate that 'something went wrong'
.
- Follow-Ups:
- Re: Spawning process with environment variables
- From: Rainer Weikusat
- Re: Spawning process with environment variables
- References:
- Spawning process with environment variables
- From: Sasha
- Re: Spawning process with environment variables
- From: Rainer Weikusat
- Re: Spawning process with environment variables
- From: Sasha
- Spawning process with environment variables
- Prev by Date: Re: Spawning process with environment variables
- Next by Date: Re: Xlib: XQueryPointer() and Button4Mask/Button5Mask
- Previous by thread: Re: Spawning process with environment variables
- Next by thread: Re: Spawning process with environment variables
- Index(es):
Relevant Pages
|