私はいくつかの新しいプロセスをfork()/ exec()する必要のあるデーモンプロセスを持っています。デーモン内のexec()プロセスからstdout/stderrを読み込みます
私の問題は、親がstdoutとstderrが閉じているデーモンだということです。これを行うためにとにかくありますか?シェルを開く必要がありますか?
int status;
pipe(pipefd_stdout);
pipe(pipefd_stderr);
pid_t pid = fork();
if (pid == 0)
{
close(pipefd_stdout[0]); // close reading end in the child
close(pipefd_stderr[0]); // close reading end in the child
dup2(pipefd_stdout[1], 1); // send stdout to the pipe
dup2(pipefd_stderr[1], 2); // send stderr to the pipe
execvpe(cmd, (char**)args, (char**)env);
}
else
{
// parent ...
waitpid(pid, &status, 0);
close(pipefd_stderr[1]);
close(pipefd_stdout[1]);
}
_「シェルを開く必要はありますか?」_いいえ、代わりにパイプを作成してください。 –
私にも同様の問題があります。残念ながら、 'system()'関数を使用しているので、あなたが示唆したようにパイプを使用することはできません。 'stdout'をテンポラリファイルの正しい代替にリダイレクトしていますか? –