概念
默认情况下,在shell命令执行过程中,任何命令都有一个默认的标准输入设备,标准输出设备和标准错误输出设备,使用管道 “ | ”可以将两个命令连接起来,从而改变输入输出的方式。其命令如下:
rpm -qa | grep telnet
此命令rpm -qa命令(进程)的输出作为grep telnet的输入。
现在Linux使用的进程间通信方式包括:
(1)无名管道(pipe)和有名管道(FIFO)
(2)信号(signal)
(3)消息队列
(4)共享内存
(5)信号量
(6)套接字(socket)
管道是单向的、先进先出的,它把一个进程的输出和另一个进程的输入连接在一起。
一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据。
数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。
管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞
管道包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者可用于运行于同一系统中的任意两个进程间的通信。
无名管道
无名管道创建:intpipe(int filedis[2]);
当一个管道建立时,它会创建两个文件描述符:iledis[0] 用于读管道,filedis[1] 用于写管道。创建成功则返回值0,否则返回-1值。
1.写管道write函数
ret=write(fd[1],buf,n)
若管道已满,则被阻塞,直到管道另一端read将已进人管道的数据取走为止。
2.读管道read函数ret=read(fd[0],buf,n)
若管道为空,且写端文件描述字未关闭,则被阻塞。若管道写端已关闭,则返回0。若管道不为空,分两种情况:(设管道中实际有m个字节),如n>=m,则读m个;如果n
include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
intmain()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char* p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
/*创建管道*/
if(pipe(pipe_fd)<0)
{
printf("pipe createerror\n");
return -1;
}
/*创建子进程*/
if((pid=fork())==0)
{
printf("\n");
close(pipe_fd[1]);
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0)
{
printf( "%d numbers read from the pipe is%s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0)
{
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent write1Hello!\n");
if(write(pipe_fd[1],"Pipe",5)!=-1)
printf("parent write2Pipe!\n");
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0); /*等待子进程结束*/
exit(0);
}
return 0;
}
关于无名管道需要注意的几个问题
①管道是半双工方式,数据只能单向传输。如果要在两个进程之间相互传送数据,就要建立两条管道。
②pipe()调用必须在调用fork()以前进行,否则子进程将无法继承文件描述符。
③使用无名管道互相连接的任意进程必须位于一个相关的进程家族里。因为管道必须受到内核的限制,所以如果进程没有在管道创建者的家族里面,则该进程将无法访问管道。
有名管道
有名管道和无名管道基本相同,但也有不同点:无名管道只能由父子进程使用;但是通过有名管道,不相关的进程也能交换数据。函数声明如下:
#include <sys/types.h>
#include <sys/stat.h>
extern int mkfifo(const char * pathname, mode_tmode);
pathname:FIFO文件名
mode:文件属性(权限)
创建有名管道文件,该文件必须不存在,如果执行成功返回0,否则返回-1,失败原因存在errno中,管道是一种特殊文件,可以同时read,write来执行读写操作,当然你需要先用open函数打开管道,这与一般文件读写操作有一定区别:如果你希望打开写端,则需要另一个进程打开读端,整个程序才能正常执行如果只打开有名管道的一端,则系统将会暂时阻塞打开进程。知道某个进程打开管道另一端。因此使用有名管道时一定要两个进程分别打开读端和写端。
示例:
fifo_write.c//写端
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineFIFO_SERVER "/tmp/myfifo"
main(intargc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
/*打开管道*/
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{
printf("Please sendsomething\n");
exit(-1);
}
strcpy(w_buf,argv[1]);
/* 向管道写入数据 */
if((nwrite=write(fd,w_buf,100))==-1)
{
printf("The FIFO hasnot been read yet.Please try later\n");
}
else
printf("write %s to theFIFO\n",w_buf);
}
fifo_read.c//读端
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIFO"/tmp/myfifo"
main(intargc,char** argv)
{
char buf_r[100];
int fd;
int nread;
/* 创建管道 */
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
printf("cannot createfifoserver\n");
printf("Preparing for readingbytes...\n");
memset(buf_r,0,sizeof(buf_r));
/* 打开管道 */
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==-1)
{
perror("open");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd,buf_r,100))==-1)
{
if(errno==EAGAIN)
printf("no datayet\n");
}
printf("read %s fromFIFO\n",buf_r);
sleep(1);
}
pause(); /*暂停,等待信号*/
unlink(FIFO); //删除文件
}
注意
管道使用和文件操作的区别?
FIFO文件在使用上和普通文件有相似之处,但是也有不同之处:
1.读取fifo文件的进程只能以”RDONLY”方式打开fifo文件。
2. 写fifo文件的进程只能以”WRONLY”方式打开fifo
3.fifo文件里面的内容被读取后,就消失了。但是普通文件里面的内容读取后还存在。