红联Linux门户
Linux帮助

命名管道问题

发布时间:2010-12-28 15:35:03来源:红联作者:JasonLee4341
有三个进程,一个是系统进程(s),对应于编程文件sys.c,还有两个进程(a)和(b),分别对应于编程文件a.c和b.c
进程(s)建立4个命名管道,分别是patos(a到s的管道),pbtos(b到s的管道),pstoa(s到a的管道),pstob(s到b的管道)。
我的设计目标是进程(a)向管道(patos)写数据,从管道(pstoa)读数据;进程(b)向管道(pbtos)写数据,从管道(pstob)读数据;
进程(s)把从管道(patos)读到的数据直接写到管道(pstob)里去,也就是把从进程(a)发送来的数据直接发送给进程(b);反之,进程(s)
把从管道(pbtos)读到的数据直接写到管道(pstoa)里去,也就是把从进程(b)发送来的数据直接发送给进程(a)。其中,管道的读写都以
非阻塞的方式打开,达到非阻塞通信的目的。代码如下,但是我执行的结果是进程(a)、进程(b)互相发送几包数据后,好像就都阻塞了,请各位
大虾给看看是怎么回事。
------------
sys.c
------------
#include
#include
#include
#include
#include
#include
#include
#include "comm.h"

static int fdatos;
static int fdbtos;
static int fdstoa;
static int fdstob;

static void doa(void);
static void dob(void);

int main(void)
{
fd_set readfds;
struct timeval timeout;
int ret;
int maxfd;

if((mkfifo("patos",0666)<0) && (errno!=EEXIST)) {
printf("can't create patos.\n");
}
if((mkfifo("pbtos",0666)<0) && (errno!=EEXIST)) {
printf("can't create pbtos.\n");
}

if((mkfifo("pstoa",0666)<0) && (errno!=EEXIST)) {
printf("can't create pstoa.\n");
}
if((mkfifo("pstob",0666)<0) && (errno!=EEXIST)) {
printf("can't create pstob.\n");
}

//a to sys, b to sys
//fdatos = open("/jason/sys/patos", O_RDONLY|O_NONBLOCK);
fdatos = open("/jason/sys/patos", O_RDONLY);
if(fdatos < 0) {
perror("open patos fail.\n");
}

//fdbtos = open("/jason/sys/pbtos", O_RDONLY|O_NONBLOCK);
fdbtos = open("/jason/sys/pbtos", O_RDONLY);
if(fdbtos < 0) {
perror("open pbtos fail.\n");
}

//sys to a, sys to b
//fdstoa = open("/jason/sys/pstoa", O_WRONLY|O_NONBLOCK);
fdstoa = open("/jason/sys/pstoa", O_WRONLY);
if(fdstoa < 0) {
perror("open pstoa fail.\n");
}

//fdstob = open("/jason/sys/pstob", O_WRONLY|O_NONBLOCK);
fdstob = open("/jason/sys/pstob", O_WRONLY);
if(fdstob < 0) {
perror("open pstob fail.\n");
}

fcntl(fdatos, F_SETFL, fcntl(fdatos, F_GETFL) | O_NONBLOCK);
fcntl(fdbtos, F_SETFL, fcntl(fdbtos, F_GETFL) | O_NONBLOCK);
fcntl(fdstoa, F_SETFL, fcntl(fdstoa, F_GETFL) | O_NONBLOCK);
fcntl(fdstob, F_SETFL, fcntl(fdstob, F_GETFL) | O_NONBLOCK);

maxfd = fdatos > fdbtos ? fdatos : fdbtos;

FD_ZERO(&readfds);
FD_SET(fdatos, &readfds);
FD_SET(fdbtos, &readfds);

timeout.tv_sec=0;
timeout.tv_usec=4000;

while(1) {
//ret = select(maxfd+1, &readfds, NULL, NULL, &timeout);
ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
if(ret == -1) {
perror("select fail.\n");
} else if(ret > 0) {
if(FD_ISSET(fdatos, &readfds)) {
doa();
}
if(FD_ISSET(fdbtos, &readfds)) {
dob();
}
}
}

close(fdatos);
close(fdbtos);
close(fdstoa);
close(fdstob);
}

static void doa(void)
{
MSG_T msg;
int rnum, wnum;

rnum = read(fdatos, &msg, sizeof(MSG_T));
if(rnum > 0) {
wnum = write(fdstob, &msg, sizeof(MSG_T));
if(wnum != sizeof(MSG_T)) {
perror("doa write msg fail.\n");
}
}
}

static void dob(void)
{
MSG_T msg;
int rnum, wnum;

rnum = read(fdbtos, &msg, sizeof(MSG_T));
if(rnum > 0) {
wnum = write(fdstoa, &msg, sizeof(MSG_T));
if(wnum != sizeof(MSG_T)) {
perror("dob write msg fail.\n");
}
}
}

------------
a.c
------------
#include
#include
#include
#include
#include
#include
#include
#include "comm.h"

int main(void)
{
int rfd, wfd;
int rnum, wnum;
MSG_T msg1, msg2;
fd_set readfds;
struct timeval timeout;
int ret;

//wfd = open("patos", O_WRONLY|O_NONBLOCK);
wfd = open("patos", O_WRONLY);
if(wfd < 0) {
perror("a open patos fail.\n");
}

//rfd = open("pstoa", O_RDONLY|O_NONBLOCK);
rfd = open("pstoa", O_RDONLY);
if(rfd < 0) {
perror("a open pstoa fail.\n");
}

fcntl(rfd, F_SETFL, fcntl(rfd, F_GETFL) | O_NONBLOCK);
fcntl(wfd, F_SETFL, fcntl(wfd, F_GETFL) | O_NONBLOCK);

msg1.desid = 'A';
msg1.data = 0xAA;

FD_ZERO(&readfds);
FD_SET(rfd, &readfds);

timeout.tv_sec=0;
timeout.tv_usec=4000;

while(1) {
wnum = write(wfd, &msg1, sizeof(MSG_T));
if(wnum < 0) {
perror("a write msg fail.\n");
}

ret = select(rfd+1, &readfds, NULL, NULL, NULL);

if(ret == -1) {
perror("select fail.\n");
} else if(ret > 0) {
if(FD_ISSET(rfd, &readfds)) {
rnum = read(rfd, &msg2, sizeof(MSG_T));
if(rnum < 0) {
perror("a read msg fail.\n");
} else if(rnum > 0) {
printf("a msg:desid=%c, data=0x%X.\n", msg2.desid, msg2.data);
}
}
}
}
}

------------
b.c
------------
#include
#include
#include
#include
#include
#include
#include
#include "comm.h"

int main(void)
{
int rfd, wfd;
int rnum, wnum;
MSG_T msg1, msg2;
fd_set readfds;
struct timeval timeout;
int ret;

//wfd = open("pbtos", O_WRONLY|O_NONBLOCK);
wfd = open("pbtos", O_WRONLY);
if(wfd < 0) {
perror("a open pbtos fail.\n");
}

//rfd = open("pstob", O_RDONLY|O_NONBLOCK);
rfd = open("pstob", O_RDONLY);
if(rfd < 0) {
perror("a open pstob fail.\n");
}

fcntl(rfd, F_SETFL, fcntl(rfd, F_GETFL) | O_NONBLOCK);
fcntl(wfd, F_SETFL, fcntl(wfd, F_GETFL) | O_NONBLOCK);

msg1.desid = 'B';
msg1.data = 0xBB;

FD_ZERO(&readfds);
FD_SET(rfd, &readfds);

timeout.tv_sec=0;
timeout.tv_usec=4000;

while(1) {
wnum = write(wfd, &msg1, sizeof(MSG_T));
if(wnum < 0) {
perror("a write msg fail.\n");
}

ret = select(rfd+1, &readfds, NULL, NULL, NULL);

if(ret == -1) {
perror("select fail.\n");
} else if(ret > 0) {
if(FD_ISSET(rfd, &readfds)) {
rnum = read(rfd, &msg2, sizeof(MSG_T));
if(rnum < 0) {
perror("b read msg fail.\n");
} else if(rnum > 0) {
printf("b msg:desid=%c, data=0x%X.\n", msg2.desid, msg2.data);
}
}
}
}
}
文章评论

共有 3 条评论

  1. skyoceanlover 于 2011-01-02 23:09:13发表:

    楼主需要找个牛人问。

  2. naruto01 于 2010-12-28 16:47:57发表:

    红联貌似不是太适合讨论编程

  3. JasonLee4341 于 2010-12-28 16:08:50发表:

    自己先顶一下