Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, in my program i need to run an external program in background.I am aware that there are at least 2 alternatives for this: 1)fork+exec 2)system("program &"); I have read ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    9

    Run external program in background

    Hi,
    in my program i need to run an external program in background.I am aware that there are at least 2 alternatives for this:
    1)fork+exec
    2)system("program &");

    I have read several posts about this,and they all tend to suggest to use fork+exec (and that's what i am doing now).
    I have some questions though:
    a) I read somewhere that "system" actually uses fork+exec (and the man page for "system" implicitly seems to confirm it).Can you confirm this?

    b)in the original source code i started working on,the external program was run like this:
    Code:
    int status=system("program &");
    printf("exited with status %d",WEXITSTATUS(status));
    I don't think that's correct for ay least 2 reasons:
    a)it doesn't check WIFEXITED before checking WEXITSTATUS
    b)since i am interested in the exit value of the external program,i think that this code is wrong since it wil output only the "system" exit code,and not the external program's one(consider that the external program will run for a while,so its exit code will be outputted much later).
    Am i correct?

  2. #2
    Just Joined!
    Join Date
    Feb 2009
    Posts
    45
    Answer:

    Quote Originally Posted by Zipi
    I have some questions though:
    a) I read somewhere that "system" actually uses fork+exec (and the man page for "system" implicitly seems to confirm it).Can you confirm this?
    What «system()» actually does:
    Code:
    $> man system;
    […]
    DESCRIPTION
           system()  executes  a  command specified in command by calling /bin/sh -c command, and returns after the command has been completed.  During execution of the com-
           mand, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.
    […]
    Thus it depends on the interpreted string, but basically this happens:
    Code:
    system("$CMD_STRING")
    → fork()
    	→ parent: wait() (this is the process that continues when «system()» exits)
    	→ child: exec "/bin/sh", "$CMD_STRING"
    		→ "/bin/sh":
    			→ Is there a “&” at the end of "$CMD_STRING" ?
    				→ fork():
    					→ parent: exit(0);
    					→ child: exec "$CMD_STRING"
    			→ Is there no “&” at the end of "$CMD_STRING" ?
    				→ exec "$CMD_STRING"
    Quote Originally Posted by Zipi
    b)in the original source code i started working on,the external program was run like this:
    Code:
    Code:
    int status=system("program &");
    printf("exited with status %d",WEXITSTATUS(status));
    I don't think that's correct […]
    You are right. It is the same as performing
    Code:
    $> command & echo $?;
    0
    This will always return 0 (if your shell works properly), even if the command does not exist. However I do not know how this will react when the shell doesnʼt work properly.

    Quote Originally Posted by Zipi
    b)since i am interested in the exit value of the external program,i think that this code is wrong since it wil output only the "system" exit code,and not the external program's one […] Am i correct?
    Indeed you are. The return value is that of the invoking shell.

    The moment the parent of “program”, which would be the shell invoked by «system()», dies, “program”ʼs new parent is «init». From the userspace perspective «init» is now the only process which is capable of determining “program”ʼs return value. Thus this approach is not dedicated to work according to your needs.

    Quote Originally Posted by Zipi
    in my program i need to run an external program in background.I am aware that there are at least 2 alternatives for this:
    1)fork+exec
    «fork()» + «exec()» + «wait()» is indeed a valid way to check for the return value of terminating child processes. You might want to look at «vfork()”¹.

    Quote Originally Posted by Zipi
    a)it doesn't check WIFEXITED before checking WEXITSTATUS
    Technically you are right. But because you already know that system is going to return 0, it doesnʼt really matter.

    Footnotes:
    1. vfork(2): create child process/block parent - Linux man page

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...