相关理论
管道是UNIX系统中最早为两个进程之间提供的一种通信机制。管道是一种单向的、先入先出的、无结构的、大小固定的通信通道。写进程在管道的一端写入数据,读进程从管道的另一端读出数据。如果两个或多个进程同时对一个进程进行读写,那么这些进程必须使用锁机制或者信号量机制对其进行同步。 管道分为无名管道和有名管道。无名管道没有名字,所以只能提供给进程家族中的父子进程间通信使用,而有名管道则用于没有家族关系的任意两个进程之间的通信。 |
无名管道通信
父进程创建两个子进程,子进程分别写内容入管道中,父进程读取管道中内容。
close():当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。只有调用close()方法时,操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()的后果是数据可能只写了一部分到磁盘,剩下的丢失了。
ssize_t read (int fd, void *buf, size_t count):read()会把参数fd所指的文件传送byte个字节到buf指针所指的内存中。若参数nbyte为0,则read()不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或无可读取的数据。
ssize_t write (int fd, const void * buf, size_t count):write()会把参数buf所指的内存写入count个字节到参数放到所指的文件内。返回值:如果顺利write()会返回实际写入的字节数。当有错误发生时则返回-1,错误代码存入errno中。
pipe(int filedes[2]):返回值:成功,返回0,否则返回-1。参数数组包含pipe使用的两个文件的描述符。fd[0]:读管道,fd[1]:写管道。必须在fork()中调用pipe(),否则子进程不会继承文件描述符。两个进程不共享祖先进程,就不能使用pipe。但是可以使用命名管道。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <wait.h>
int main()
{
pid_t pid1;
pid_t pid2;
int fd[2];
//父进程读 子进程写
int result = -1;
char buf[1024] = {};
char Sp1[] = "Child 1 is sending a message!";
char Sp2[] = "Child 2 is sending a message!";
result = pipe(fd); //成功返回0 失败返回-1
if(-1 == result){
printf("create pipe failed \n");
return -1;
}
pid1 = fork();
if( pid1 n== -1){
printf("create pid1 faild\n");
return -1;
}else if(pid1 == 0){ //子进程写
close(fd[0]);
//成功返回写入的字节数
//失败返回-1
write(fd[1], Sp1, strlen(Sp1));
}else{
pid2 = fork();//建立子进程2写
if(pid2 == -1){
printf("create pid2 faild\n");
return -1;
}else if(pid2 == 0){
close(fd[0]);
write(fd[1], Sp2, strlen(Sp2));
sleep(2);
}else{//父进程读
wait(0);
close(fd[1]);
//成功返回读出的字节数
//失败返回-1
ssize_t res = read(fd[0], buf, sizeof(buf));
printf("%ld", res);
if(res > 0){
buf[res - 1] = '\0';
}
printf("%s\n", buf);
ssize_t res1 = read(fd[0], buf, sizeof(buf));
printf("%ld", res1);
if(res > 0){
buf[res1 - 1] = '\0';
}
printf("%s\n", buf);
return 0;
}
}
return 0;
}
结果展示
有名管道通信
1、创建有名管道:
$ mknode named_pipe p //named_pipe为管道名,p表示该文件是一个FIFO文件2、
2、写文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#define LEN 50
int main()
{
FILE *wfile;
char Sp[LEN] = "Child 1 is sending a message!";
wfile = fopen("named_pipe", "w");
if(wfile == NULL){
printf("fopen error.\n");
exit(1);
}
fwrite(Sp, 1, LEN, wfile);
fclose(wfile);
return 0;
}
3、读管道文件:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#define LEN 50
//读
int main()
{
FILE *file;
char Sp[LEN];
file = fopen("named_pipe", "r");
if(file == NULL){
printf("fopen error.\n");
exit(1);
}
//size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;
//buffer:地址(可以是数组,也可以是新开辟的空间,buffer就是一个索引)
// size:读的每个数据项的字节数 单位字节 count:读的数据项个数 stream:输入流(读取的文件的指针 )
//返回真实读取的项数,若大于count则意味着产生了错误。
fread(Sp, 1, LEN, file);
printf("%s", Sp);
fclose(file);
return 0;
}
结果展示
先运行命令mknod name p创建有名管道
在终端1中运行写程序:
在终端2中运行读程序: