进程间通信的目的:
Ø 数据传输:一个进程需要将它的数据发送给另一个进程
Ø 资源共享:多个进程之间共享同样的资源
Ø 通知事件:一个进程休要向另一个或另一组进程发送消息,通知它门发生某种 事件
Ø 进程控制:一个进程完全控制另一个进程的执行(如debug进程),此时控制进程希望能够拦截另一个进程的所有操作,并能够及时通知它的状态改变。
发展:
Linux进程间通信(IPC)由以下几部分发展而来:
u UNIX进程间通信
u 基于System V进程间通信
u POSIX进程间通信
POSIX(理解)
POSIX(Portable Operating System Interface)表示可移植操作系统接口。电器和电子工程师协会(Institute of Electrical and Electronics Engineers,IEEE)最初开发POSIX标准,是为了提高UNIX环境下应用程序的可移植性。然而,POSIX并不局限于UNIX,许多其他的操作系统,如DEC OpenVMS和Microsoft Windows,都支持POSIX标准。
分类(必须全部记住)
现在linux使用的进程间通信方式包括:
1. 管道(pipe)和有名管道(FIFO)
2. 信号(signal)
3. 消息队列
4. 共享内存
5. 信号量
6. 套接字(socket)
管道通信
管道是单向的,先进先出的,它是把一个进程的输出和另一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据。
数据被一个进程读出后,将被从管道中删除,其他读进程将不能再读到这些数据。管道提供了简单的流程控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞。
管道创建
管道包括无名管道和有名管道两种,前者用于父进程与子进程间通信,后者用于运行于同一系统中的任意两个进程间通信。
无名管道由pipe()函数创建:
int pipe (int filedis[2]);
当一个管道建立时,它会创建两个文件描述符:filedis[0]用于读管道,filedis[1]用于写管道。
管道关闭
将这两个文件描述符关闭即可。可用普通的close(fclose)函数逐个关闭。
管道读写
管道用于不同进程间通信。通常先创建一个管道,在通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道。
注意事项:
必须在系统调用fork()前调用pipe()。否则子进程将不会继承文件描述符。
例题:pipe_rw.c
#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");
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 write1 Hello!\n");
if(write(pipe_fd[1]," Pipe",5)!=-1)
printf("parent write2 Pipe!\n");
close(pipe_fd[1]);
waitpid(pid,NULL,0); /*等待子进程结束*/
exit(0);
}
return 0;
}
命名管道:(FIFO)
创建:
#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char *pathname , mode_t mode)
Ø Pathname :FIFO文件名
Ø Mode :属性 一旦创建了一个FIFO,就可用open打开它,一般的文件访问函数(close,read,write等)都可用于FIFO.
例题: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>
#define FIFO_SERVER "/tmp/myfifo"
int main(int argc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
/*创建有名管道*/
if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL|O_RDWR)<0)&&(errno!=EEXIST))
printf("cannot create fifoserver\n");
/*打开管道*/
fd=open(FIFO_SERVER,O_RDWR|O_NONBLOCK,0);
if(fd==-1)
{
perror("open");
exit(1);
}
/*入参检测*/
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);
close(fd); //关闭管道
return 0;
}
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"
int main(int argc,char** argv)
{
char buf_r[100];
int fd;
int nread;
/* 打开管道 */
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);
}
//后面三句话是不会被运行到的,但不会影响程序运行的效果当程序在上面的死循环中执行时收到信号后会马上结束运行而没有执行后面的三句话。这些会在后面的信号处理中讲到,现在不理解没有关系,这个问题留给大家学习了信号处理之后来解决。
close(fd); //关闭管道
pause(); /*暂停,等待信号*/
unlink(FIFO); //删除文件
}