天天看點

2fifo有名管道



1fifo有名管道

建立一個有名管道,fifo

2fifo有名管道

2.fifo依賴的頭檔案

#include <sys/types.h>

#include <sys/stat.h>

函數聲明

int mkfifo(const char *pathname, mode_tmode);

2fifo有名管道
2fifo有名管道

3.fifo的寫端應用

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <fcntl.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的讀端應用

   int fd, len;

   char buf[1024];

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

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

   write(stdout_fileno, buf, len);

注意,這裡要有可供寫的fifo檔案,并且在運作的時候傳入file檔案。