有名管道

创建有名管道

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ 
        
    int ret = mkfifo("myfifo", 0664);//创建有名管道,管道名为myfifo可以改名,0664为8进制权限
    if(ret < 0)
    {
        perror("mkfifo");
        exit(-1);
    }

    return 0;
} 

向管道传输数据

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{ 
    if(argc < 2)//从命令行接收的命令小于2个
    {
        printf("Usage: %s <fifo_name>\n", argv[0]);
        exit(-1);
    }

    int fd = open(argv[1], O_WRONLY);//open打开文件 返回文件描述符,详细见后面
    if(fd < 0)
    {
        perror("open");
        exit(-1);
    }
    
    printf("open ok!\n");

    char buf[64] = {0};
    while(1)
    {
        fgets(buf, 64, stdin);//stdin标准输入(键盘),输入到buf
        write(fd, buf, 64);//将buf的数据写入fd
    }

    return 0;
} 

从管道接收数据

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{ 
    if(argc < 2)//从命令行接收的命令小于2个
    {
        printf("Usage: %s <fifo_name>\n", argv[0]);
        exit(-1);
    }

    int fd = open(argv[1], O_RDONLY);//open打开文件 返回文件描述符,详细见后面
    if(fd < 0)
    {
        perror("open");
        exit(-1);
    }
    
    printf("open ok!\n");

    char buf[64] = {0};
    while(1)
    {
        read(fd, buf, 64);//将fd的内容给buf
        printf("%s", buf);
    }

    return 0;
} 

alt

alt

全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务