Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146560
  • 博文数量: 165
  • 博客积分: 7070
  • 博客等级: 少将
  • 技术积分: 1730
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 21:20
文章分类

全部博文(165)

文章存档

2010年(165)

我的朋友

分类: LINUX

2010-09-15 18:31:52

如何操作伪终端:
伪终端的使用是成对出现的,分为master 和 slaver
主设备:/dev/ptmx (i850上的主设备名)
从设备:动态生成:/dev/pts/0.......x
功能:写入主设备的信息,可以从从设备上读出;写入从设备的信息,可以从主设备读出。用以实现对串口的多路复用。
以下是测试代码
Ubuntu 下的编译方法:
gcc -Wall ptyhost.c -o ptyhost -util /lib/libutil-2.9.so
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <sys/types.h>
# include <linux/limits.h>
# include <pty.h> /* for openpty and forkpty */
#include <utmp.h> /* for login_tty */
#include <time.h>
# include <pty.h> /* for openpty and forkpty */
#include <utmp.h> /* 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 <%s> 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 <<<<<\n\n___________________\n",temp2);
//**************************************************


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

                while(1)
                {
                        c = read(mpty, ptyBuff, 100);
                        if(c > 0)
                        {
                                printf("###-<%d>\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:


阅读(1979) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~