国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務(wù)器 > Linux服務(wù)器 > 正文

Linux一個(gè)命名管道

2024-09-05 23:00:38
字體:
供稿:網(wǎng)友

   命名管道也是按照文件操作流程進(jìn)行的,可以看做特殊文件。

  寫管道進(jìn)程:打開-寫-關(guān)閉

  讀管道進(jìn)程:打開-讀-關(guān)閉

  本實(shí)驗(yàn)采用阻塞式讀寫管道,一個(gè)程序?qū)懀硪粋€(gè)讀。

   寫:

  #include<sys/types.h>

  #include<sys/stat.h>

  #include<errno.h>

  #include<fcntl.h>

  #include<stdio.h>

  #include<stdlib.h>

  #include<limits.h>

  #define MYFIFO  "/tmp/myfifo"

  #define MAX_BUFFER_SIZE     PIPE_BUF

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

  {

  char buff[MAX_BUFFER_SIZE];

  int fd;

  int nwrite;

  if(argc <= 1)

  {

  printf("usage: ./write string!/n");

  exit(1);

  }

  sscanf(argv[1], "%s", buff);

  fd = open(MYFIFO, O_WRONLY);//打開管道,寫阻塞方式

  if(fd == -1)

  {

  printf("open fifo file error!/n");

  exit(1);

  }

  if((nwrite = write(fd, buff, MAX_BUFFER_SIZE)) > 0)//寫管道

  {

  printf("write '%s' to FIFO!/n ", buff);

  }

  close(fd);//關(guān)閉

  exit(0);

  }

  讀:

  #include<sys/types.h>

  #include<sys/stat.h>

  #include<errno.h>

  #include<fcntl.h>

  #include<stdio.h>

  #include<stdlib.h>

  #include<limits.h>

  #define MYFIFO  "/tmp/myfifo"

  #define MAX_BUFFER_SIZE     PIPE_BUF

  int main()

  {

  char buff[MAX_BUFFER_SIZE];

  int fd;

  int nread;

  //判斷管道是否存在,如果不存在則創(chuàng)建

  if(access(MYFIFO, F_OK) == -1)

  {

  if((mkfifo(MYFIFO, 0666) < 0) && (errno != EEXIST))

  {

  printf("cannot creat fifo file!/n");

  exit(1);

  }

  }

  fd = open(MYFIFO, O_RDONLY);//打開管道,只讀阻塞方式

  if(fd == -1)

  {

  printf("open fifo file error!/n");

  exit(1);

  }

  while(1)

  {

  memset(buff, 0, sizeof(buff));

  if((nread = read(fd, buff, MAX_BUFFER_SIZE)) > 0)//讀管道

  {

  printf("read '%s' from FIFO/n", buff);

  }

  }

  close(fd);//關(guān)閉

  exit(0);

  }

  編譯運(yùn)行,打開兩個(gè)終端,一個(gè)寫,一個(gè)讀。

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌兰察布市| 台北县| 张家口市| 汶上县| 黑河市| 卢龙县| 喀什市| 沙湾县| 黄石市| 佛坪县| 中阳县| 漠河县| 霍州市| 临夏市| 兴仁县| 三原县| 津市市| 德化县| 巴林右旗| 鲁山县| 朝阳区| 富平县| 玉屏| 仙桃市| 阳高县| 桐柏县| 简阳市| 教育| 游戏| 思南县| 象山县| 丰顺县| 大冶市| 呼伦贝尔市| 峡江县| 丰宁| 铅山县| 黄龙县| 渭南市| 临泽县| 吴堡县|