国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 南城县| 扎赉特旗| 许昌县| 龙岩市| 瑞安市| 涟水县| 安康市| 贞丰县| 来宾市| 扎鲁特旗| 闵行区| 蓝田县| 苍山县| 平远县| 本溪| 罗城| 滦南县| 苏尼特右旗| 达日县| 乐至县| 增城市| 方正县| 壤塘县| 延寿县| 博湖县| 九龙坡区| 高安市| 石泉县| 施秉县| 德昌县| 银川市| 津南区| 大安市| 含山县| 渭南市| 巩义市| 阿克陶县| 筠连县| 宜春市| 武陟县| 安图县|