码迷,mamicode.com
首页 > 其他好文 > 详细

2fifo有名管道

时间:2014-09-21 01:31:29      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   文件   2014   div   



1fifo有名管道

创建一个有名管道,fifo

bubuko.com,布布扣

2.fifo依赖的头文件

#include <sys/types.h>

#include <sys/stat.h>

函数声明

int mkfifo(const char *pathname, mode_tmode);

bubuko.com,布布扣

bubuko.com,布布扣

3.fifo的写端应用

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <string.h>

 

void sys_err(char *str, int exitno)

{

   perror(str);

   exit(exitno);

}

 

int main(int argc, char *argv[])

{

   int fd;

   char buf[1024] = "hello toto\n";

   if (argc < 2) {

       printf("./a.out fifoname\n");

       exit(1);

    }

 

   //fd = open(argv[1], O_RDONLY);

   fd = open(argv[1], O_WRONLY);

   if (fd < 0)

       sys_err("open", 1);

 

   write(fd, buf, strlen(buf));

   close(fd);

 

   return 0;

}

fifo的读端应用

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <string.h>

void sys_err(char *str, int exitno)

{

   perror(str);

   exit(exitno);

}

 

int main(int argc, char *argv[])

{

   int fd, len;

   char buf[1024];

   if (argc < 2) {

       printf("./a.out fifoname\n");

       exit(1);

    }

 

   fd = open(argv[1], O_RDONLY);

   if (fd < 0)

       sys_err("open", 1);

 

    len = read(fd, buf, sizeof(buf));

   write(STDOUT_FILENO, buf, len);

 

   close(fd);

 

   return 0;

}

注意,这里要有可供写的fifo文件,并且在运行的时候传入file文件。

 

 

 

 

 

 

 

2fifo有名管道

标签:style   blog   http   io   os   ar   文件   2014   div   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/39437833

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!