我有这样的事情:
pipe
close(pipe[0]);
parent writes something to pipe
close(pipe[1]);
fork();
if(child)
{
close(pipe[1]);
child reads from pipe
close(pipe[0]);
child does some operations
child writes to pipe
close(pipe[1]);
}
else
{
back to parent
close(pipe[0]);
wait(&code);
parent tries to read what the terminated child just wrote but fails to do so
}
我不确定我能做些什么来让父母从终止的孩子那里阅读。我需要使用dup
吗?我不太确定在什么情况下dup
或dup2
有用。
写和读是使用write()
andread()
函数完成的。
我必须使用管道而不是 fifo 或其他方式在进程之间进行通信。