I am using fork and execv to spawn child programs. How can I make these programs run in the background? I tried not waiting for them, but they still send output to the screen.
Asked
Active
Viewed 1,839 times
0
-
What's your definition of "background"? – Gabe Apr 27 '11 at 08:22
-
Running a process in the background is the same thing as program &. – node ninja Apr 27 '11 at 08:24
-
your terminology is not consistent - `program&` will still write its output to the screen, "background" in this case is just releasing the shell prompt. `program > /dev/null&` will do what you want – davka Apr 27 '11 at 08:43
-
No it won't. `top &` will not show anything on the screen. – node ninja Apr 27 '11 at 08:51
2 Answers
3
Redirect stdout/stderr to /dev/null
before you exec:
freopen("/dev/null", "w", stdout);
freopen("/dev/null", "w", stderr);
exec....

Erik
- 88,732
- 13
- 198
- 189
-
I get this error: cannot convert ‘FILE*’ to ‘const char*’ for argument ‘2’ to ‘FILE* freopen(const char*, const char*, FILE*)’ – node ninja Apr 27 '11 at 08:28
-
-
-
@z-buffer: "back to normal"? If you want to hide/discard output, redirect it to /dev/null as above. exec will never return anyway – Erik Apr 27 '11 at 09:31
1
You can use pipe to redirect output of child process. Take a look to this queston. There is code what doing redirection of stdout and stderr to pipe.

Community
- 1
- 1

Mihran Hovsepyan
- 10,810
- 14
- 61
- 111