读书人

对于进程管道的有关问题~=3=抱歉没

发布时间: 2013-01-28 11:49:56 作者: rapoo

对于进程管道的问题~~=3=抱歉没分了,,

#include 
#include
int main( void )
{
int filedes[2];
char buf[80];
pid_t pid;

pipe( filedes );

if ( (pid=fork()) > 0 )
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
}

waitpid( pid, NULL, 0 );

return 0;
}


为什么可以不关闭管道就直接传送,那如果子进程运行到read时,父进程没来得及写入,子进程没东西可读不直接就错误了么?而且为什么要关闭管道?比如父进程关闭了读close( filedes[0] );,为什么接下来子进程还能读read?那关闭有什么用有什么用? c pipe
[解决办法]
好好看看unix环境高级编程,看看管道和fork函数的原来。
[解决办法]
一般情况下“读”是block的,即没东西可读时交出CPU时间,等待可读为止。

另外fork会使得文件描述符复制。

好好看APUE之类的书吧。

读书人网 >C语言

热点推荐