Linux系统编程 82 管道的基本用法

学习笔记

pipe函数
用于创建并打开管道
管道有两个口。
#include <unistd.h>
int pipe(int pipefd[2]);

参数1:
pipefd[0] :读端
pipefd[1] :写端

返回值:
成功:0
失败:-1 errno

管道的写端: 是指进程写到管的的端口
管道的读端: 是指进程从管道读取的的端口
管道的读写端个人感觉是站在进程角度来描述。

父进程创建管道的时候,相当于打开一个读端,一个写端

父进程创建子进程的时候,父子进程共享文件描述符。
子进程也有一个读端一个写端


管道是单向流动

父进程要做写操作的,那么父进程要关闭读端
子进程要做读操作的,那么子进程要关闭写端

$cat mypipe.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>

void sys_err(const char *str)
{
    perror(str);
    exit(1);
}
int main(int argc, char *argv[])
{
    int ret;
    int fd[2];
    pid_t pid;

    char *str ="hello pipe\n";
    char buf[1024];

    ret = pipe(fd);

    if(-1 == ret)
    {
        sys_err("pipe error!");
    }

    pid = fork();

    if(pid > 0)
    {
        close(fd[0]);
        write(fd[1],str,strlen(str));
        close(fd[1]);
    }
    else if(0 == pid)
    {
        close(fd[1]);
        ret = read(fd[0],buf,sizeof(buf));
        write(STDOUT_FILENO,buf,ret);
        close(fd[0]);
    }
    return 0;
}


read 函数的返回值
-1:出错
0: 文件末尾
>0:读到实际字节数

$make mypipe
gcc  mypipe.c -o mypipe -Wall -g
$./mypipe
$hello pipe

$


可以发现输出有点奇怪。
不是第一次看见看见了
原因:父进程先于子进程结束。

解决办法:让父进程sleep 几秒钟。

$cat mypipe.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>

void sys_err(const char *str)
{
    perror(str);
    exit(1);
}
int main(int argc, char *argv[])
{
    int ret;
    int fd[2];
    pid_t pid;

    char *str ="hello pipe\n";
    char buf[1024];

    ret = pipe(fd);

    if(-1 == ret)
    {
        sys_err("pipe error!");
    }

    pid = fork();

    if(pid > 0)
    {
        close(fd[0]);
        write(fd[1],str,strlen(str));
        sleep(1);
        close(fd[1]);
    }
    else if(0 == pid)
    {
        close(fd[1]);
        ret = read(fd[0],buf,sizeof(buf));
        write(STDOUT_FILENO,buf,ret);
        close(fd[0]);
    }
    return 0;
}
make mypipe
gcc  mypipe.c -o mypipe -Wall -g
$./pipe
bash: ./pipe: No such file or directory
$./mypipe
hello pipe
$


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值