麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 服務器 > Linux服務器 > 正文

Linux命名管道的使用

2024-09-05 23:00:39
字體:
來源:轉載
供稿:網友
Linux命名管道非常適合同一機器上兩個進程之間傳遞數據,其形式也是一個文件,但是讀取與寫入時遵循FIFO的原則。在使用命名管道時有兩種方式進行讀/寫:阻塞與非阻塞。

使用命名管道很容易寫出生產者與消費者的程序。下面這個程序摘自《Linux程序設計(第3版)》

producer.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF
#define TEN_MEG (1024 * 1024 * 10)

int main()
{
int pipe_fd;
int res;
int open_mode = O_WRONLY;
int bytes_sent = 0;
char buffer[BUFFER_SIZE + 1];

if(access(FIFO_NAME,F_OK) == -1){
res = mkfifo(FIFO_NAME,0777);
if(res != 0){
fprintf(stderr,"Could not create fifo %s/n",FIFO_NAME);
exit(EXIT_FAILURE);
}
}

printf("Process %d opening FIFO O_WRONLY/n",getpid());
pipe_fd = open(FIFO_NAME,open_mode);
printf("Process %d result %d/n",getpid(),pipe_fd);

if(pipe_fd != -1){
while(bytes_sent < TEN_MEG){
res = write(pipe_fd,buffer,BUFFER_SIZE);
if(res == -1){
fprintf(stderr,"Write error on pipe/n");
exit(EXIT_FAILURE);
}
bytes_sent += res;
}
(void)close(pipe_fd);
}
else{
exit(EXIT_FAILURE);
}

printf("Process %d finished/n",getpid());
exit(EXIT_SUCCESS);
}


customer.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF

int main()
{
int pipe_fd;
int res;

int open_mode = O_RDONLY;
char buffer[BUFFER_SIZE + 1];
int bytes_read = 0;

memset(buffer,'/0',sizeof(buffer));

printf("Process %d opening FIFO O_RDONLY/n",getpid());
pipe_fd = open(FIFO_NAME,open_mode);
printf("Process %d result %d/n",getpid(),pipe_fd);

if(pipe_fd != -1){
do{
res = read(pipe_fd,buffer,BUFFER_SIZE);
bytes_read += res;
}while(res > 0);
(void)close(pipe_fd);
}
else{
exit(EXIT_FAILURE);
}

printf("Process %d finished, %d bytes read/n",getpid(),bytes_read);
exit(EXIT_SUCCESS);
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 视频在线色 | 精品欧美一区二区精品久久 | 精品国产一区二区三区在线观看 | 国产精品久久久久久238 | 最新在线中文字幕 | 久久精品日产第一区二区三区 | xxxx8 | 亚洲欧美成aⅴ人在线观看 av免费在线播放 | 国产精品视频不卡 | 久久一区三区 | 爱福利视频网 | 亚洲午夜不卡 | 可以免费看的av | 免费在线观看成人av | 久久一区三区 | 久久精品九九 | 免费久久精品 | 亚洲一区二区三区精品在线观看 | 深夜影院a | 国产一区二区三区影视 | 久久精品一级片 | 久久精品视频在线看99 | 在线视频 中文字幕 | xx53xx| 精品久久久久久久久久中出 | 成人精品 | 久久久精品视频免费 | av在线播放观看 | 国产成视频在线观看 | 国产精品视频中文字幕 | 少妇一级淫片免费放4p | 91久久精品一区二区 | 欧美性激情视频 | 欧美xxxx精品另类 | 蜜桃视频网站www | 国产va在线观看免费 | www.三区| 免费在线观看毛片视频 | 精品国产乱码一区二区三区四区 | 久久久久久久久久久国产精品 | 在线91观看 |