• Linux管道编程实例


    • 头文件: #include<unistd.h>
    • 使用方法: int pipe(int chan[2]);
    • 说明: pipe会建立管道,并将文件描述词通过chan返回。一般chan[0]为管道的读取端,chan[1]是写入端。
    • 返回值: 成功返回0,失败返回-1,错误信息保存在errno中
    • 错误信息:
      • EMFILE 进程已用完文件描述词最大量。
      • ENFILE 系统已无文件描述词可用。
      • EFAULT 参数filedes数组地址不合法
    #include <unistd.h>
    #include <signal.h>
    #include <stdio.h>
    
    char parent[] = "a message from parrent";
    char child[] = "a message from child";
    
    main ()
    {
        int chan1[2], chan2[2];
        int pid;
        char buf[100];
        pipe(chan1);
        pipe(chan2);
        pid=fork();
        if(pid > 0)
        {
            close(chan1[0]);
            close(chan2[1]);
            write(chan1[1], parent, sizeof(parent));
            close(chan1[1]);
            read(chan2[0],buf, 100);
            printf("%s
    ", buf);
            close(chan2[0]);
        }
        else if(pid == 0)
        {
            close(chan1[1]);
            close(chan2[0]);
            read(chan1[0], buf, 100);
            printf("%s
    ", buf);
            write(chan2[1], child, sizeof(child));
            close(chan2[1]);
            close(chan1[0]);
        }
    }
    
    
    
  • 相关阅读:
    8 Range 对象
    7 Worksheet 对象
    6 Workbook 对象
    5 Application 对象
    Windows路径
    windows 下操作目录(使用DOS命令)
    Windows 批处理
    6 WPF控件
    Lexer and parser generators (ocamllex, ocamlyacc)
    4.9 Parser Generators
  • 原文地址:https://www.cnblogs.com/svitter/p/4067141.html
Copyright © 2020-2023  润新知