进程间通信——FIFO(多个客户进程,一个服务进程)

时间:2023-03-09 00:51:20
进程间通信——FIFO(多个客户进程,一个服务进程)

FIFO简介

FIFO就是Unix的一种复合POSIX标准的进程间通信机制。他又称为命名管道,跟管道的不同点是,每个FIFO都有一个路径名与之关联。

FIFO虽然有路径名,但是他这中文件是在内核态(管道也是在内核态),跟文件系统没有关系。

单个服务器进程,多个客户端进程与服务器进通信。客户端进程想服务器进程发送请求(客户端进程通过write写FIFO),服务端处理(通过read读客户进程的请求)之后返回相应内容(通过write写入专用FIFO)。

单个服务器进程多个客户端进程通信框架

进程间通信——FIFO(多个客户进程,一个服务进程)

客户进程和服务器进程都知道专用FIFO的路径名,关键是怎样创建客户和服务器进程专用的FIFO。因为,一个系统的进程id是确定的并且互斥,所以我们可以通过进程id去寻找FIFO,客户进程和服务进程之间的FIFO用进程id来创建,在客户和服务器进程之间协商一个格式(比如“fifo.pid”格式)。

多个客户进程向服务器进程请求服务

本示例代码,实现的是将客户进程请求打开一个文件,具体就是客户进程传文件名,服务器进程将文件内容返回。

客户端代码:

 /*
* client.c
*
* Created on: Nov 2, 2016
* Author: sujunjun
*/ #include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<limits.h>
#include<fcntl.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h> #define FIFO_SERVER "fifo.server"
#define SIZE PIPE_BUF
#define FILE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH) int main(){ pid_t pid=getpid();
char buf[SIZE];
char client_fifo_name[SIZE];//from server
snprintf(client_fifo_name,sizeof(client_fifo_name),"fifo.%ld",(long)pid);
if(mkfifo(client_fifo_name,FILE_MODE)< && errno!=EEXIST){
printf("mk error \n");
exit();
}
snprintf(buf,sizeof(buf),"%ld ",(long)pid);
int len=strlen(buf);
char *pathname=buf+len;
fgets(pathname,SIZE-len,stdin);
len =strlen(buf);/f include \n int writefd=open(FIFO_SERVER,O_WRONLY);
write(writefd,buf,len-);//len-1==not include \n int readfd=open(client_fifo_name,O_RDONLY);
int r;
while((r=read(readfd,buf,SIZE))>){
write(STDOUT_FILENO,buf,r);
}
close(readfd);
unlink(client_fifo_name);//delete this temp file
return ;
}

服务器代码:

 /*
* server.c
*
* Created on: Nov 2, 2016
*/ #include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdlib.h>
#include<fcntl.h>
#include<limits.h>
#include<string.h> #define FIFO_SERVER "fifo.server"//all known #define FILE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH) #define SIZE PIPE_BUF
int main(){
if(mkfifo(FIFO_SERVER,FILE_MODE)< && errno!=EEXIST){
printf("mk error \n");
exit();
} int readfd=open(FIFO_SERVER,O_RDONLY);
int writefd=open(FIFO_SERVER,O_WRONLY); if(readfd==-||writefd==-){
printf("open error \n");
exit();
} int r;char buf[SIZE];char *pathname;pid_t pid;char client_fifo_name[SIZE];
while((r=read(readfd,buf,SIZE))>){
buf[r]='\0';
if((pathname=strchr(buf,' '))==NULL){
printf("not include pid\n");
continue;
}
*pathname++='\0';//*pid_s=0 pid_s++
pid=atol(buf);
snprintf(client_fifo_name,sizeof(client_fifo_name),"fifo.%ld",(long)pid);
if((writefd=open(client_fifo_name,O_WRONLY))<){
printf("error\n");
continue;
}
int fd;
if((fd=open(pathname,O_RDONLY))<){
printf("open this file error\n");
write(writefd,"open this file error\n",sizeof("open this file error\n"));
close(writefd);
}else {
while((r=read(fd,buf,SIZE))>){
write(writefd,buf,r);
}
close(fd);
}
close(writefd);
}
return ;
}

运行示例:

编译两个文件

gcc client.c -o client

gcc server.c -o server

./server

./client

./client

./client

多开几个客户进程,请求打开不同的文件,就会看到显示的文件内容。

总结

本例子设计的是一个迭代服务器,如果要设计一个并发服务器,还需要一些多线程编程的知识。

还是要对FIFO一些特性要熟悉,比如open一个FIFO的时候,阻塞和非阻塞open之间的区别。还有FIFO的一次请求字节数必须小于等于PIPE_BUF(在limits.h中),这样能保证一次读请求是原子操作。如果不是原子的,那么两次请求的字节内容将无法区分。因为服务器进程是一直在循环读请求字节(服务器进程里有一个大循环,read用readline代替就更好理解了)。

snprintf()函数还是比较方便,java中提供的一些字符串与数字之间转换的便利操作,其实在C语言中也有,只不过被我们忽视了。snprintf函数就为我们提供了一个很好的数字转换为字符串的函数。例如:snprintf(buf,buf的长度,"process id is %ld",(long)getpid());最后buf中存放的是"process id is 1234"(进程具体ID).

另外,迭代服务器存在一些弊端,就是在处理服务型攻击的时候比较麻烦,这个不如并发服务器好处理。

服务型攻击,就拿咱们的这个代码来说,如果咱们的客户进程只是请求服务,却从不打开自己进程与服务器专用FIFO来读服务回传的内容,这个时候,服务器可能就处于停顿状态。这就叫服务型攻击(DoS型攻击)。