管道

来源:互联网 发布:js字符串中查找字符串 编辑:程序博客网 时间:2024/04/28 18:04

进程间通信概述

 

进程通信的目的

1.        数据传输

一个进程需要将它的进程传递到另一个进程

2.        资源共享

多个进程需要共享同样资源

3.        通知事件

一个进程需要向另外一个或者一组进程发送消息,通知他们发生了某种事件。

4.       进程控制

有些进程希望完全控制另一个进程的执行,此时控制进程希望能够拦截另一个进程的所有操作,并能够及时知道它的状态改变。

Linux进程间通信(IPC)由以下几个部分发展而来:

1.       UNIX 进程间通信

2.       基于System V进程间通信

3.       POSIX进程间通信

 

Linux使用的进程间通讯方式

1.       管道(pipe)和有名管道(FIFO)

2.       信号(signal)

3.       消息队列

4.       共享内存

5.       信号量

6.       套接字(socket)

 

管道

管道是单向的,先进先出,它把一个进程的输出和另一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)在管道的头部读出数据。

管道包括有名管道和无名管道,前者用于同一系统中的任意两个进程间的通信,后者用于父子进程间的通信。

1.无名管道

pipe()函数创建管道

int pipe(int filedis[2]), 当一个管道建立时,它会创建两个文件描述符filedis[0]用于读管道,filedis[1]用于写管道。关闭管道只需用close函数关闭这两个文件描述符即可。

示例如下:

#include <unistd.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int pipe_fd[2];

if(pipe(pipe_fd)<0)

{

printf("pipe create error/n");

return -1;

}

else

printf("pipe create success/n");

close(pipe_fd[0]);

close(pipe_fd[1]);

}

 

 管道读写

通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道,父进程向管道写数据,子进程从管道读数据。

示例如下:

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int pipe_fd[2];

pid_t pid;

char buf_r[100];

int r_num;

 

memset(buf_r,0,sizeof(buf_r));

 

 

if(pipe(pipe_fd)<0 && errno == EEXIST)

{

printf("pipe create error/n");

return -1;

}

 

 

 

if((pid=fork())==0)  //子进程

{

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]);//写关闭

sleep(3);

waitpid(pid,NULL,0);

exit(0);

}

return 0;

}

注意:

第一:必须在fork()函数调用之前,创建管道,否则子进程将不会继承管道描述符。

第二:fork()创建的子进程复制父进程的进程环境,比如堆栈和数据段资源,所以在子进程中对管道的读标识符或者写标识符进行关闭或者打开操作只会影响到本子进程对管道的操作,对父进程对管道的操作没有影响。

执行结果如下:

parent wirte1 Hello!

parent wirte2 Pipe!

10 numbers read from the pipe is  Hello Pipe

2.命名管道

无名管道和命名管道的异同

命名管道和无名管道基本相同,主要不同点在于:无名管道只能有父子进程使用,而对于命名管道,无相关的任意两个进程都可以使用。

创建命名管道

#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时,非阻塞标志(O_NONBLOCK)将对以后的读写产生如下影响:

没有使用O_NONBLOCK :访问要求无法满足时进程将要阻塞。

使用O_NONBLOCK:访问要求无法满足时不阻塞,立即出错返回,errno的值为ENXIO

示例程序:

1). 新建一个命名管道,循环读取管道中数据

#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(int argc,char** argv)

{

char buf_r[100];

int  fd;

int  nread;

 

 

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); //以非阻塞方式打开管道

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); //删除文件

}

 

2).向命名管道中写入数据

#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"

 

main(int argc,char** argv)

{

int fd;

char w_buf[100];

int nwrite;

 

 

fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK);

 

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);

}

打开两个终端,一个运行写进程,一个运行读进程。

原创粉丝点击