概述
//有限状态机 finite-state machine
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define TTY1 "/dev/tty11"
#define TTY2 "/dev/tty12"
#define BUFSIZE 1024
enum
{
STATE_R=1,
STATE_W,
STATE_Ex,
STATE_T //finil state
};
struct fsa_st
{
int state;
int sfd,dfd;
char buf[BUFSIZE];
int len,pos;
char *errstr;
};
static void fsa_driver(struct fsa_st *fsa)
{
int ret;
switch(fsa->state)
{
case STATE_R:
fsa->len = read(fsa->sfd,fsa->buf,BUFSIZE);
if(fsa->len == 0)
fsa->state = STATE_T;
else if(fsa->len < 0)
{
if(errno == EAGAIN)
fsa->state = STATE_R;
else
{
fsa->state = STATE_Ex;
fsa->errstr = "read()";
}
}
else
{
fsa->pos = 0;
fsa->state = STATE_W;
}
break;
case STATE_W:
ret = write(fsa->dfd,fsa->buf+fsa->pos,fsa->len);
if(ret < 0)
{
if(errno == EAGAIN)
fsa->state = STATE_W;
else
{
fsa->errstr = "write()";
fsa->state = STATE_Ex;
}
}
else
{
fsa->pos += ret;
fsa->len -= ret;
if(fsa->len == 0)
fsa->state = STATE_R;
else
fsa->state = STATE_W;
}
break;
case STATE_Ex:
perror(fsa->errstr);
fsa->state = STATE_T;
break;
case STATE_T:/*do nothing*/
break;
default:
abort();
break;
}
}
static void relay(int fd1,int fd2)
{
int fd1_save,fd2_save;
struct fsa_st fsa12,fsa21;
fd1_save = fcntl(fd1,F_GETFL);
fcntl(fd1,F_SETFL,fd1_save|O_NONBLOCK);
fd2_save = fcntl(fd2,F_GETFL);
fcntl(fd2,F_SETFL,fd2_save|O_NONBLOCK);
fsa12.state = STATE_R;
fsa12.sfd = fd1;
fsa12.dfd = fd2;
fsa21.state = STATE_R;
fsa21.sfd = fd2;
fsa21.dfd = fd1;
while(fsa12.state != STATE_T || fsa21.state != STATE_T) //cann't is finil state
{
fsa_driver(&fsa12);
fsa_driver(&fsa21);
}
// recover
fcntl(fd1,F_SETFL,fd1_save);
fcntl(fd2,F_SETFL,fd2_save);
}
int main()
{
int fd1,fd2;
fd1 = open(TTY1,O_RDWR);
if(fd1 < 0)
{
perror("open()");
exit(1);
}
write(fd1,"TTY1n",5);
fd2 = open(TTY2,O_RDWR|O_NONBLOCK);
if(fd2 < 0)
{
perror("open()");
exit(1);
}
write(fd2,"TTY2n",5);
relay(fd1,fd2);
close(fd1);
close(fd2);
exit(0);
}
最后
以上就是喜悦飞鸟为你收集整理的有限状态机—— FSM(finite-state machine)的全部内容,希望文章能够帮你解决有限状态机—— FSM(finite-state machine)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复