Linux中的伪终端编程

    xiaoxiao2026-04-09  10

    [cpp]  view plain  copy 如何操作伪终端:   伪终端的使用是成对出现的,分为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://blog.163.com/coder_jack@126  

    转载请注明原文地址: https://ju.6miu.com/read-1308648.html
    最新回复(0)