管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常说的管道多是指无名管道,无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别。
有名管道叫named pipe或者FIFO(先进先出),可以用函数mkfifo()创建。
从本质上说,管道也是一种文件,但它又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题,具体表现为:
1)限制管道的大小。
实际上,管道是一个固定大小的缓冲区。在Linux中,该缓冲区的大小为1页,即4K字节,使得它的大小不象文件那样不加检验地增长。使 用单个固定缓冲区也会带来问题,比如在写管道时可能变满,当这种情况发生时,随后对管道的write()调用将默认地被阻塞,等待某些数据被读取,以便腾
出足够的空间供write()调用写。
· 2)读取进程也可能工作得比写进程快。
当所有当前进程数据已被读取时,管道变空。当这种情况发生时,一个随后的read()调用将默认地被阻塞,等待某些数据被写入,这解决了read()调用返回文件结束的问题。
3)从管道读数据是一次性操作,数据一旦被读,它就从管道中被抛弃,释放空间以便写更多的数据。
在 Linux 中,管道的实现并没有使用专门的数据结构,而是借助了文件系统的file结构和VFS的索引节点inode。通过将两个 file 结构指向同一个临时的 VFS 索引节点,而这个 VFS 索引节点又指向一个物理页面而实现的。
创建一个管道:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
int res = mkfifo("/tmp/my_fifo", 0777);
if (res == 0)
{
printf("FIFO created/n");
}
exit(EXIT_SUCCESS);
}
编译这个程序:
gcc –o fifo1.c fifo
运行这个程序:
$ ./fifo1
用ls命令查看所创建的管道
$ ls -lF /tmp/my_fifo
prwxr-xr-x 1 root root 0 05-08 20:10 /tmp/my_fifo|
注意:ls命令的输出结果中的第一个字符为p,表示这是一个管道。最后的|符号是由ls命令的-F选项添加的,它也表示是这是一个管道。虽然,我们所设置的文件创建模式为“0777”,但它被用户掩码(umask)设置(022)给改变了,这与普通文件创建是一样的,所以文件的最终模式为755。
打开FIFO一个主要的限制是,程序不能是O_RDWR模式打开FIFO文件进行读写操作,这样做的后果未明确定义。这个限制是有道理的,因为我们使用FIFO只是为了单身传递数据,所以没有必要使用O_RDWR模式。如果一个管道以读/写方式打开FIFO,进程就会从这个管道读回它自己的输出。如果确实需要在程序之间双向传递数据,最好使用一对FIFO,一个方向使用一个。
当一个Linux进程被阻塞时,它并不消耗CPU资源,这种进程的同步方式对CPU而言是非常有效率的。
一、实验:使用FIFO实现进程间通信
两个独立的程序:
1.
2.
生产者程序fifo2.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/Linux/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 = 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 < TEN_MEG)
{
res = write(pipe_fd, buffer, BUFFER_SIZE);
if (res == -1)
{
fprintf(stderr, "Write error on pipe/n");
exit(EXIT_FAILURE);
}
bytes += res;
}
close(pipe_fd);
}
else
{
exit(EXIT_FAILURE);
}
printf("Process %d finish/n", getpid());
exit(EXIT_SUCCESS);
}
//消费者程序fifo3.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FIFO_NAME "/tmp/Linux/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 = 0;
memset(buffer, '/0', sizeof(buffer));
printf("Process %d opeining 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 += res;
}while(res > 0);
close(pipe_fd);
}
else
{
exit(EXIT_FAILURE);
}
printf("Process %d finished, %d bytes read/n", getpid(), bytes);
exit(EXIT_SUCCESS);
}
编译这两个程序:
gcc –o fifo2 fifo2.c
gcc –o fifo3 fifo3.c
运行这两个程序:
[root@localhost chaper12]# ./fifo2 &
[2] 23121
Process 23121 opening FIFO O_WRONLY
[root@localhost chaper12]# time ./fifo3à读数据
Process 24155 opeining FIFO O_RDONLY
Process 23121 result 3
Process 24155 result 3
Process 23121 finish
Process 24155 finished, 10485760 bytes read
[2]-
real
user 0m0.000s
sys
以上两个程序均是使用阻塞模式FIFO。Linux会安排好这两个进程之间的调试,使它们在可以运行的时候运行,在不能运行的时候阻塞。因此,写进程将在管道满时阻塞,读进程将在管道空时阻塞。
虚拟机上,time命令显示,读进程只运行了0.2秒的时间,却读取了10M字节的数据。这说明管道在程序之间传递数据是非常有效的。
二、实验:使用FIFO的客户/服务器应用程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SERVER_FIFO_NAME "/tmp/Linux/chaper12/server_fifo"
#define CLIENT_FIFO_NAME "/tmp/Linux/chaper12/client_%d_fifo"
#define BUFFER_SIZE PIPE_BUF
#define MESSAGE_SIZE 20
#define NAME_SIZE 256
typedef struct message
{
pid_t client_pid;
char data[MESSAGE_SIZE + 1];
}message;
#include "client.h"
int main()
{
int server_fifo_fd;
int client_fifo_fd;
int res;
char client_fifo_name[NAME_SIZE];
message msg;
char *p;
if (mkfifo(SERVER_FIFO_NAME, 0777) == -1)
{
fprintf(stderr, "Sorry, create server fifo failure!/n");
exit(EXIT_FAILURE);
}
server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);
if (server_fifo_fd == -1)
{
fprintf(stderr, "Sorry, server fifo open failure!/n");
exit(EXIT_FAILURE);
}
sleep(5);
while (res = read(server_fifo_fd, &msg, sizeof(msg)) > 0)
{
p = msg.data;
while (*p)
{
*p = toupper(*p);
++p;
}
sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid);
client_fifo_fd = open(client_fifo_name, O_WRONLY);
if (client_fifo_fd == -1)
{
fprintf(stderr, "Sorry, client fifo open failure!/n");
exit(EXIT_FAILURE);
}
write(client_fifo_fd, &msg, sizeof(msg));
close(client_fifo_fd);
}
close(server_fifo_fd);
unlink(SERVER_FIFO_NAME);
exit(EXIT_SUCCESS);
}
#include "client.h"
int main()
{
int server_fifo_fd;
int client_fifo_fd;
int res;
char client_fifo_name[NAME_SIZE];
message msg;
msg.client_pid = getpid();
sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid);
if (mkfifo(client_fifo_name, 0777) == -1)
{
fprintf(stderr, "Sorry, create client fifo failure!/n");
exit(EXIT_FAILURE);
}
server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
if (server_fifo_fd == -1)
{
fprintf(stderr, "Sorry, open server fifo failure!/n");
exit(EXIT_FAILURE);
}
sprintf(msg.data, "Hello from %d", msg.client_pid);
printf("%d sent %s ", msg.client_pid, msg.data);
write(server_fifo_fd, &msg, sizeof(msg));
client_fifo_fd = open(client_fifo_name, O_RDONLY);
if (client_fifo_fd == -1)
{
fprintf(stderr, "Sorry, client fifo open failure!/n");
exit(EXIT_FAILURE);
}
res = read(client_fifo_fd, &msg, sizeof(msg));
if (res > 0)
{
printf("received:%s/n", msg.data);
}
close(client_fifo_fd);
close(server_fifo_fd);
unlink(client_fifo_name);
exit(EXIT_SUCCESS);
}
gcc –o server server.c
gcc –o client client.c
测试这个程序,我们需要一个服务器进程和多个客户进程。为了让多个客户进程在同一时间启动,我们使用了shell命令:
[root@localhost chaper12]# ./server &
[26] 5171
[root@localhost chaper12]# for i in 1 2 3 4 5; do ./client & done
[27] 5172
[28] 5173
[29] 5174
[30] 5175
[31] 5176
[root@localhost chaper12]# 5172 sent Hello from 5172 received:HELLO FROM 5172
5173 sent Hello from 5173 received:HELLO FROM 5173
5174 sent Hello from 5174 received:HELLO FROM 5174
5175 sent Hello from 5175 received:HELLO FROM 5175
5176 sent Hello from 5176 received:HELLO FROM 5176