首页 > 其他 > 详细

IPC with pipes, also dup2 redirect stream handle

时间:2014-03-22 02:36:03      阅读:370      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
        int fds[2];
        pid_t pid;

        pipe( fds );
        pid = fork();
        if( pid == (pid_t)0 )
        {
                close( fds[1] );
                dup2(fds[0], STDIN_FILENO );
                execlp( "sort", "sort", 0 );
        }
        else
        {
                FILE *stream;
                close(fds[0]);
                stream = fdopen( fds[1], "w" );
                fprintf( stream, "This is a test.\n" );
                fprintf( stream, "Hello, world.\n" );
                fprintf( stream, "My dog has fleas.\n" );
                fprintf( stream, "My dream will come true.\n");
                fflush( stream );
                close( fds[1] );
                waitpid( pid, NULL, 0 );
        }
}
bubuko.com,布布扣

IPC with pipes, also dup2 redirect stream handle,布布扣,bubuko.com

IPC with pipes, also dup2 redirect stream handle

原文:http://www.cnblogs.com/feika/p/3616613.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!