进程间通信(Inter-Process Communication,IPC)是用于处理一个多进程系统中各个进程之间的协调。所谓进程间通信,就是指多个进程间相互通信、交换信息的方法。常见的进程间通信方法包括以下几种:
- 管道和命名管道
- 信号
- 消息队列
- 共享内存
- 信号量
- 套接字
其中,管道和命名管道、信号、消息队列、共享内存和信号量只适用于本地进程间通信,套接字用于远程通信,因而一般用于网络编程,这里不做介绍。
1、管道(pipe)
在Linux系统中,管道是一种特殊的文件,它的主要用途是实现进程间通信。
特点:
1. 管道是单向的、先进先出的;
2. 当一个管道建立后,将获得两个文件描述符,分别用于对管道的读取和写入,一个进程在管道的尾部写入数据,另一个进程从管道的头部读出数据;
3. 数据被一个进程读出后,将被从管道中删除,其他读进程不能再读到这些数据;
4. 只能用于父进程和子进程之间的通信。
管道的创建和关闭
#include <unistd.h>
int pipe(int fd[2]);
此函数用于创建一个管道。参数fd[2]是一个两元整型数组,用于存放调用该函数所创建的两个文件描述符。fd[0]存放读取端文件描述符,fd[1]存放写入端文件描述符。调用成功,返回值0;调用失败,返回值-1;关闭管道只是将两个文件描述符关闭即可,可以使用普通的close函数逐个关闭。
注意:创建管道之后,还不能实现通过管道在两个进程间通信。因为此时管道的读入端和写入端的文件描述符属于同一个进程。因此通常在pipe函数调用完成后,调用fork函数派生一个子进程,需要时调用exec函数族的函数使子进程执行所需程序。然后根据数据传输方向关闭父进程和子进程的一个文件描述符。必须在系统调用fork()之前调用pipe(),否则子进程将不会继承文件描述符。
实例:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
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 create error\n");
return -1;
}
/*创建子进程*/
if((pid=fork())==0)
{
printf("\n");
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]);/*父子进程都有两个文件描述符,将不需要使用的文件描述符关闭*/
if(write(pipe_fd[1]," Pipe",5)!=-1)
printf("parent write2 Pipe!\n");
if(write(pipe_fd[1],"\n",3) != -1)
printf("OK\n");
close(pipe_fd[1]);
exit(0);
}
else if(pid>0)
{
if(write(pipe_fd[1],"Hello",5)!=-1)
printf("parent write1 Hello!\n");
close(pipe_fd[1]);/*父子进程都有两个文件描述符,将不需要使用的文件描述符关闭*/
sleep(5); /*让子进程先运行,这样父进程才能读到信息*/
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]);
waitpid(pid,NULL,0); /*等待子进程结束*/
exit(0);
}
return 0;
}
2、命名管道
命名管道(FIFO)又称为先入先出队列,是一种特殊的管道,存在于文件系统中。命名管道一旦创建,就可以像普通文件一样进行读写操作。
特点:
1. 命名管道可以用于任何两个进程间的通信,并不限制这两个进程同源;
2. 数据被一个进程读出后,将被从管道中删除,其他读进程不能再读到这些数据;
3. 命名管道作为一种特殊的文件存在于文件系统中,而不像管道一样存在于内核中。当进程对命名管道使用完毕,命名管道任然存在于文件系统中,除非对其进行删除操作,否则该命名管道不会消失;
4. 命名管道只能用于单向数据传输,如果要用命名管道实现两个进程间数据的相互交换,需要使用两个命名管道。
管道的创建和关闭
要创建一个命名管道,需要使用如下系统调用:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
参数pathname是一个字符串指针,用于存放命名管道的文件名。参数mode用于指定所创建的文件的权限,其取值及含义与创建普通文件的creat函数中的mode相似。调用成功,返回值0;调用失败,返回值-1。
注意:
1. 一旦创建了一个FIFO,就可以用open()打开它,一般的文件访问函数(read和write)进行读写,使用系统调用close关闭一个命名管道。若要删除一个命名管道,使用系统调用unlink;
2. 使用命名管道时,需要在两个将进行通信的进程中分别打开命名管道,因此当一个进程读打开(或写打开)一个命名管道而没有其他进程写打开(或读打开)此命名管道,该进程就会进入阻塞状态,直到另一个进程写打开(或读打开)此命名管道;
3. 打开FIFO,非阻塞标志(O_NONBLOCK)对以后的读写产生如下影响:
没有使用O_NONBLOCK,访问要求无法满足时进程阻塞,如果试图读取空的FIFO,将导致进程阻塞;若使用O_NONBLOCK,访问时无法满足要求时阻塞。
实例:
pipe_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"
int main(int argc,char** argv)
{
char buf_r[100];
int fd;
int nread;
/* 创建管道 检查文件是否存在,不存在则创建,否则返回错误,errno为EEXIST*/
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
printf("cannot create fifoserver\n");
printf("Preparing for reading bytes...\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 data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause(); /*暂停,等待信号*/
unlink(FIFO); //删除文件
}
pipe_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>
#define FIFO_SERVER "/tmp/myfifo"
int main(int argc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
/*打开管道*/
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{
printf("Please send something\n");
exit(-1);
}
strcpy(w_buf,argv[1]);
/* 向管道写入数据 */
if((nwrite=write(fd,w_buf,100))==-1)
{
if(errno==EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
printf("write %s to the FIFO\n",w_buf);
}