ºìÁª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·¢±í:

    ×Ô¼ºÏȶ¥Ò»ÏÂ