打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
pipe函数使用

pipe函数使用  

2008-12-11 15:45:54|  分类: 天天向上 |字号 订阅

pipe(建立管道)
表头文件 #include
定义函数 int pipe(int filedes[2]);
函数说明
    pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
    filedes[0]为管道里的读取端,所以pipe用read调用的
    filedes[1]则为管道的写入端。
   
返回值:  若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码:
    EMFILE 进程已用完文件描述词最大量
    ENFILE 系统已无文件描述词可用。
    EFAULT 参数 filedes 数组地址不合法。
#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;
}
[root@localhost src]# gcc pipe.c
[root@localhost src]# ./a.out
This is in the child process,here read a string from the pipe.
This is in the father process,here write a string to the pipe.
Hello world , this is write by pipe.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux进程间通信
Linux的进程通讯之匿名管道
进程管道通信 - 多端写入lockf加锁
Linux系统管道和有名管道的通信机制
C语言进程间通信(一)
有名管道和无名管道
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服