Linux程序間通信-命名管道
- 什麼是命名管道
- 建立命名管道
- 打開命名管道
- 向命名管道寫
- 從命名管道讀
- 示例
什麼是命名管道
匿名管道隻能用于父子關系的程序之間進行通信。命名管道是一種實際存在的FIFO檔案,稱作“管道檔案”,用于不同的程序間,命名管道程序間打開同一個FIFO檔案,進行通信。
建立命名管道
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char* pathname, mode_t mode);
參數
- pathname: FIFO檔案的路徑
- mode: 檔案的權限
傳回值
- 成功:0
- 失敗:-1,errno
打開命名管道
#include <sys/types.h>
#include <sys/stat.h>
int open(const char* pathname, int flags);
參數
- pathname: FIFO檔案的路徑
- flags:
- O_RDONLY
- O_WRONLY
- O_NONBLOCK
傳回值
- 成功:檔案描述符
- 失敗:-1,errno
注意: 命名管道隻有在兩個程序中同時打開,并且是以一端隻讀,另一端隻寫的方式打開時,才能完成打開操作,不然會發生阻塞,直到兩端同時使用打開操作為止。
向命名管道寫
#include <unistd.h>
ssize_t write(int fd, void* buf, size_t count);
參數
- fd: 檔案描述符
- buf: 緩沖區
- count: 寫入的位元組數
傳回值
- 成功:寫入的位元組數
- 失敗:-1,errno
從命名管道讀
#include <unistd.h>
ssize_t read(int fd, void* buf, size_t count);
參數
- fd: 檔案描述符
- buf: 緩沖區
- count: 讀取的位元組數上限
傳回值
- 成功:讀取的位元組數
- 失敗:-1,errno
示例
//fifowrite.c
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
const char* fifo_name = "my_fifo";
const char* fifo_name2 = "your_fifo";
char buffer[PIPE_BUF + 1];
char buffer2[PIPE_BUF + 1];
if (access(fifo_name, F_OK) == -1)
{
int res = mkfifo(fifo_name, 0774);
if (res != 0)
{
perror("Could not create fifo");
exit(-1);
}
}
if (access(fifo_name2, F_OK) == -1)
{
int res = mkfifo(fifo_name2, 0774);
if (res != 0)
{
perror("Could not create fifo2");
exit(-1);
}
}
int pipe_fd = open(fifo_name, O_WRONLY);
int pipe_fd2 = open(fifo_name2, O_RDONLY);
if (pipe_fd == -1)
{
perror("Open pipe failed.");
exit(-1);
}
if (pipe_fd2 == -1)
{
perror("Open pipe failed.");
exit(-1);
}
int bytes_read = 0;
while(1)
{
printf("$ ");fflush(stdout);
bytes_read = read(0, buffer, PIPE_BUF);
if (bytes_read > 0)
{
buffer[bytes_read] = '\0';
int bytes_write = write(pipe_fd, buffer, bytes_read);
if (bytes_write <= 0)
{
perror("Write to pipe failed.");
close(pipe_fd);
exit(-1);
}
}
else
break;
bytes_read = read(pipe_fd2, buffer2, PIPE_BUF);
if (bytes_read > 0)
{
buffer2[bytes_read] = '\0';
write(1, buffer2, bytes_read);
}
}
close(pipe_fd);
close(pipe_fd2);
return 0;
}
//fiforead.c
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
const char* fifo_name = "my_fifo";
const char* fifo_name2 = "your_fifo";
char buffer[PIPE_BUF + 1];
int pipe_fd = open(fifo_name, O_RDONLY);
int pipe_fd2 = open(fifo_name2, O_WRONLY);
if (pipe_fd == -1)
{
perror("Open pipe failed");
exit(-1);
}
if (pipe_fd2 == -1)
{
perror("Open pipe failed");
exit(-1);
}
dup2(pipe_fd, 0);
dup2(pipe_fd2, 1);
dup2(pipe_fd2, 2);
execl("/bin/sh", (char*)NULL);
close(pipe_fd);
close(pipe_fd2);
return 0;
}