Results 1 to 3 of 3
Hi, folks,
Can anyone help me to figure out a way to detect the failure of system call in c/c++ code if the command is suggested to run in the ...
- 02-06-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 1
how to detect failure of system call if & is added to the command
Hi, folks,
Can anyone help me to figure out a way to detect the failure of system call in c/c++ code if the command is suggested to run in the way like "firefox index.html &", src code might be like:
int ret = system("firefox index.html &");
failure could take place if firefox is not installed or index.html does not exist. More, '&' charactor has to be added for the program need to go on running after the code is triggered.
I am a student and working on a program project, finding that system is related to fork information. However, & will make fork success but acturally may fail.
Great Thanks!
- 02-06-2010 #2Just Joined!
- Join Date
- Mar 2006
- Posts
- 29
It is better to use fork and an IPC system than use just system call in your case.
But if you want to stick with your system call solution with '&', you can make it like this :
int ret = system("(firefox index.html; echo $?>/tmp/errcode) &");
and then try to read /tmp/errcode file with a timeout. If the timeout is reached and there is no file, then the command is a success, if the file was found before timeout, if it contains 0 then the command was successfull else it is a failure.
- 02-06-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I agree with keikun2004 that it is better to fork, and then use one of the exec() calls in the child process. You can then use one of the wait() calls in the parent with the WNOHANG options (returns immediately, not blocking execution of the parent process) when you want, allowing you to continue with other stuff, and when wait() returns with a 0 status, then you can get the exit status and/or other information if desired, such as whether or not the child process dumped core, was signalled, etc.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote