linux中的伪终端编程,Linux中的伪终端编程

如何操作伪终端:

伪终端的使用是成对出现的,分为master 和 slaver

主设备:/dev/ptmx (i850上的主设备名)

从设备:动态生成:/dev/pts/0.......x

功能:写入主设备的信息,可以从从设备上读出;写入从设备的信息,可以从主设备读出。用以实现对串口的多路复用。

以下是测试代码

Ubuntu 下的编译方法:

gcc -Wall ptyhost.c -o ptyhost -util /lib/libutil-2.9.so

# include

# include

# include

# include

# include

# include

# include /* for openpty and forkpty */

#include /* for login_tty */

#include

# include /* for openpty and forkpty */

#include /* for login_tty */

int main(int argc, char *argv[])

{

int rtnVal=0;

int mpty, spty, c=0, dev=0;

char *pName=NULL;

char ptyBuff[100] = {'/0'};

char sptyname[20] = {'/0'};

rtnVal = openpty(&mpty, &spty, sptyname, NULL, NULL);/*该函数遍历系统中的伪终端匹配对,如果能找到一组未使用的,则返回1,否则返回-1,成功返回时mpty会带出匹配对中主设备的文件描述符,spty会带出从设备的文件描述符,第三个实参如果不空的话,它会带出从设备的路径名!后边两个参数是在设置终端属性,一般是不要的,据说伪终端对属性设置是忽略的*/

// Check if Pseudo-Term pair was created

if(rtnVal != -1)

{

pName = ptsname(mpty);//get slave device name, the arg is the master device

printf("Name of slave side is fd = %d/n", pName, spty);

strcpy(sptyname, pName);

printf("my sptyname is %s/n",sptyname);

//test write to mpty and read from spty*************

char temp[50] = {"hell/nworld ! i have write to mpty!"};

char temp2[100] = {'/0'};

c = write(mpty,temp,5);

if(c <=0)

printf("ERROR : can not write to mpty/n");

sleep(3);

printf("write %d charactors to mpty success/n",c);

sleep(3);

printf("try to read from spty/n");

sleep(3);

c = read(spty,temp2,5);

if(c <=0)

printf("ERROR : can not read from mpty/n");

printf("read from spty %d charactors success/n",c);

printf("/n>>>>> %s <<<<

//**************************************************

// Go into loop and read what is sent to Slave side of pair

while(1)

{

c = read(mpty, ptyBuff, 100);

if(c > 0)

{

printf("###-/n", c);

printf("buff:__|%s",ptyBuff);

}

}

}

else

{

printf("PseudoTerm, creation failed.../n");

}

return rtnVal;

} http://blog.163.com/coder_jack@126

伪终端的操作:

以下是源码,来自/fsl_myandroid_r6/external/qemu/vl.c line 2545

r7里面都已经没有了!真快呀!哈哈

/* Once Solaris has openpty(), this is going to be removed. */

int openpty(int *amaster, int *aslave, char *name,

struct termios *termp, struct winsize *winp)

{

const char *slave;

int mfd = -1, sfd = -1;

*amaster = *aslave = -1;

mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);

if (mfd < 0)

goto err;

if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)

goto err;

if ((slave = ptsname(mfd)) == NULL)

goto err;

if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)

goto err;

if (ioctl(sfd, I_PUSH, "ptem") == -1 ||

(termp != NULL && tcgetattr(sfd, termp) < 0))

goto err;

if (amaster)

*amaster = mfd;

if (aslave)

*aslave = sfd;

if (winp)

ioctl(sfd, TIOCSWINSZ, winp);

return 0;

err:

if (sfd != -1)

close(sfd);

close(mfd);

return -1;

} http://blog.163.com/coder_jack@126

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值