读书人

关于LINUX有名管道的多路复用有关问题

发布时间: 2012-10-25 10:58:58 作者: rapoo

关于LINUX有名管道的多路复用问题
我的意思是说,用有名管道来实现,命名管道文件只有1个,所有的进程用读写模式打开。其中1个进程相当于服务器的角色,从管道中读取后,将一定的数据回送给发送方,比如可以原封不动的将收到的报文回送。其他进程分别对这个有名管道写数据,写完了后就读回应。假如S是这个类似服务器角色的进程,S读到C1进程写进管道的内容后,应该向管道中回写,C1读这个管道,应该收到内容。同理C2、C3也这么做,这样就有个问题,C1/C2/C3收到的报文必须是正确的发送给自己的,而不能读到其他的。这样怎么实现?
目前这个S端的程序是这样的,没有用到SELECT
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define FIFO "/tmp/myfifo"

main(int argc,char** argv)
{
char buf_r[100];
int fd;
int nread;
pid_t pid;

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

pid = fork();
if (pid == 0)
{
//child,execl a process
execl("./t2","t2","hello",NULL,(char *)0);
}
else //** parent,keep reading
{
//fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
fd=open(FIFO,O_RDWR|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");
sleep(1);
continue;
}
printf("read %s from FIFO\n",buf_r);
if (nread > 0)
{
printf("now send back\n");
nread = write(fd,buf_r,100);
printf("send %d bytes back\n",nread);
}
sleep(1);
}
pause();
unlink(FIFO);
}
}

C1这么写:
#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,0);
fd=open(FIFO_SERVER,O_RDWR,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);
sleep(1);
memset(w_buf,0x0,sizeof(w_buf));
nwrite = read(fd,w_buf,100);
printf("read back,%s\n",w_buf);
}
这个程序可以正确的运行,因为只有1对读写进程。如果fork2次,用execl执行2次C,则可以在C端看到发送和接收是混乱的
解决思路是用SELECT进行多路转接吗?如何实现呢?试图写了2个版本,只能实现1对进程的正确读写,另一个进程却阻塞了,貌似并不是SELECT在正确工作
分不多,真的只有这么多了

[解决办法]
一个FIFO是做梦。



APUE上标准实现是:

1,服务端创建一个众所周知的监听FIFO。
2,客户端启动后创建各自的FIFO,FIFO的路径以pid命名,之后向服务端的FIFO写入自己的PID。
3,服务端检测到监听FIFO可读,就读取其中的PID,并打开这些PID对应的FIFO加入到检测集。
4,客户端可以向自己的FIFO读写数据,服务端可以检测到每个客户端的FIFO的数据读写。

读书人网 >UNIXLINUX

热点推荐