Linux命名管道的使用_Linux教程
Linux命名管道非常適合同一機(jī)器上兩個(gè)進(jìn)程之間傳遞數(shù)據(jù),其形式也是一個(gè)文件,但是讀取與寫入時(shí)遵循FIFO的原則。在使用命名管道時(shí)有兩種方式進(jìn)行讀\寫:阻塞與非阻塞。
使用命名管道很容易寫出生產(chǎn)者與消費(fèi)者的程序。下面這個(gè)程序摘自《Linux程序設(shè)計(jì)(第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);
}
使用命名管道很容易寫出生產(chǎn)者與消費(fèi)者的程序。下面這個(gè)程序摘自《Linux程序設(shè)計(jì)(第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);
}
相關(guān)Linux教程:
- Linux系統(tǒng)下TOP命令使用與分析詳解
- 安裝Linux我們需要改變20件事情
- 使用Linux系統(tǒng)架設(shè)VSFTP服務(wù)器
- Linux系統(tǒng)上架設(shè)POP3服務(wù)器
- Linux中“Networking Disabled”的解決方法(解決Ubuntu等系統(tǒng)無法上網(wǎng))
- ubuntu系統(tǒng)清理磁盤教程
- linux下搭建pxe自動化安裝環(huán)境
- BIOS不支持導(dǎo)致Linux內(nèi)核耗電增加
- Debian GNU/Linux系統(tǒng)卡片
- Linux操作系統(tǒng)開機(jī)自行啟動項(xiàng)目詳細(xì)解析
- Linux菜鳥入門級命令大全
- Linux操作系統(tǒng)中讀取目錄文件信息的過程
Linux教程Rss訂閱服務(wù)器教程搜索
Linux教程推薦
- Oracle定時(shí)器(Job)各時(shí)間段的寫法
- Linux如何實(shí)時(shí)添加Bash History
- centos配置安裝Squid 3.0反向代理的教程
- Linux知識99問
- Linux系統(tǒng)如何設(shè)置程序開機(jī)自動運(yùn)行
- 為Linux MySQL數(shù)據(jù)庫設(shè)置磁盤限額
- Linux系統(tǒng)下防垃圾郵件基本功能
- Linux下的內(nèi)存管理方式
- Linux中“Networking Disabled”的解決方法(解決Ubuntu等系統(tǒng)無法上網(wǎng))
- 防范網(wǎng)絡(luò)攻擊確保Linux服務(wù)器安全
- 相關(guān)鏈接:
- 教程說明:
Linux教程-Linux命名管道的使用。