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 ...
- 03-02-2009 #1Just 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:
I don't think that's correct for ay least 2 reasons:Code:int status=system("program &"); printf("exited with status %d",WEXITSTATUS(status));
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?
- 03-02-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
Answer:
What «system()» actually does:
Originally Posted by Zipi
Thus it depends on the interpreted string, but basically this happens: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. […]
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"You are right. It is the same as performing
Originally Posted by Zipi
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.Code:$> command & echo $?; 0
Indeed you are. The return value is that of the invoking shell.
Originally Posted by Zipi
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.
«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()”¹.
Originally Posted by Zipi
Technically you are right. But because you already know that system is going to return 0, it doesnʼt really matter.
Originally Posted by Zipi
Footnotes:


Reply With Quote