2021-03-23管道学习内容

pipe原型

NAME
       pipe, pipe2 - create pipe

SYNOPSIS
       #include <unistd.h>

       int pipe(int pipefd[2]);

       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <unistd.h>

       int pipe2(int pipefd[2], int flags);

pipe无名管道 学习内容

#include<stdio.h>
#include<unistd.h>



int main()
{

        int fd[2];

        pid_t pid;
        char buff[30];


        if(pipe(fd)<0){
                printf("create pipe error\n");
        }


        pid = fork();

        if(pid  <0){

                printf(" fork  error \n");

        }
        else if(pid > 0){
                printf("this  is  a  father\n");
                close(fd[0]);

                write(fd[1],"jianglongjie henshuai",30);
        }

        else{
                printf("this  is  a  child\n");
                close(fd[1]);


                read(fd[0],buff,30);

                printf("read successed\n");


                printf("%s\n",buff);
        }





        return 0;
}
~           

pipe部分内容学习代码


FIFO 有名管道 部分内容学习

FIFO有名管道原型

MKFIFO(1)                                  User Commands                                  MKFIFO(1)

NAME
       mkfifo - make FIFOs (named pipes)

SYNOPSIS
       mkfifo [OPTION]... NAME...

1.建立FIFO有名管道

#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>


int main()
{
        int ret=0;
        int buf[30];
        if(mkfifo("./fifo",0600)&& errno!= EEXIST)
        {
                printf("create  fifo fail\n");
                perror("why");
        }

        int fd = open("./file",O_RDONLY);

        printf("open success");


        return 0;
}
~                                                                                                     
~                                                                                                     
~                                                                                                     
~             


2.FIFO有名管道内容写入

#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>


int main()
{
        char *str ="jianglongjiehenshuai write successed";

        int fd = open("./file",O_WRONLY);

        write(fd,str,strlen(str));

        printf("write success\n");



        close(fd);
        return 0;
}
~                                                                                                     
~                                                                                                     
~           

3.FIFO有名管道内容读取

#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include <fcntl.h>


int main()
{
        char buf[50]= {0};

        if(mkfifo("./file",0600)== -1 && errno != EEXIST)
        {

                printf("mkfifo failed");
                perror("why");

        }

        int fd = open("./file",O_RDONLY);

        int nread = read(fd, buf , 50);

        printf("read  success\n");

        printf("read %d byte from  fifo :%s \n",nread, buf);

        close(fd);
        return 0;
}
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                              

效果图
效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值