Re: Spawning process with environment variables



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'
.



Relevant Pages

  • Re: Spawning process with environment variables
    ... process with command line and custom env variables and get return code ... I tried using execle but it seems when child ... process exits parent process exits too. ...
    (comp.unix.programmer)
  • Re: 7.1RC1: system hang
    ... pid ppid pgrp uid state wmesg wchan cmd ... Tracing command vmstat pid 96381 tid 100276 td 0xc756caf0 ...
    (freebsd-stable)
  • Re: Killing a process that takes too long
    ... You may instead use fork and exec; this lets you use the process-ID to ... kill 'INT', $pid; ... and it does not guarantee that the child ... So we need a way to kill several processes of the process group of the parent, ...
    (perl.beginners)
  • Re: SBCL just turned 1.0!
    ... Lisp implementation means either lazyness or technical incompetence ... "Return any available status information on child processed. ... (multiple-value-bind (pid status) ... ;; Terminate the child process. ...
    (comp.lang.lisp)
  • Re: I can now run EMC2 and it kind of "works"
    ... try to reduce the following error (if I of PID is used). ... forward, if you command a speed of about 50% of the maximum speed, it ... get the velocity correct and the PID control takes care of the errors. ... With VFF the control tells the amp to run at 20 inches/minute ...
    (rec.crafts.metalworking)