Linux中的伪终端编程

来源:互联网 发布:数据分析师条件 编辑:程序博客网 时间:2024/04/29 19:04

[cpp] view plain copy
  1. 如何操作伪终端:  
  2. 伪终端的使用是成对出现的,分为master 和 slaver  
  3. 主设备:/dev/ptmx (i850上的主设备名)  
  4. 从设备:动态生成:/dev/pts/0.......x  
  5. 功能:写入主设备的信息,可以从从设备上读出;写入从设备的信息,可以从主设备读出。用以实现对串口的多路复用。  
  6. 以下是测试代码  
  7. Ubuntu 下的编译方法:  
  8. gcc -Wall ptyhost.c -o ptyhost -util /lib/libutil-2.9.so  
  9. # include <stdio.h>  
  10. # include <stdlib.h>  
  11. # include <string.h>  
  12. # include <unistd.h>  
  13. # include <sys/types.h>  
  14. # include <linux/limits.h>  
  15. # include <pty.h> /* for openpty and forkpty */  
  16. #include <utmp.h> /* for login_tty */  
  17. #include <time.h>  
  18. # include <pty.h> /* for openpty and forkpty */  
  19. #include <utmp.h> /* for login_tty */  
  20. int main(int argc, char *argv[])  
  21. {  
  22.         int rtnVal=0;  
  23.         int mpty, spty, c=0, dev=0;  
  24.         char *pName=NULL;  
  25.         char ptyBuff[100] = {'/0'};  
  26.         char sptyname[20] = {'/0'};  
  27.         rtnVal = openpty(&mpty, &spty, sptyname, NULL, NULL);/*该函数遍历系统中的伪终端匹配对,如果能找到一组未使用的,则返回1,否则返回-1,成功返回时mpty会带出匹配对中主设备的文件描述符,spty会带出从设备的文件描述符,第三个实参如果不空的话,它会带出从设备的路径名!后边两个参数是在设置终端属性,一般是不要的,据说伪终端对属性设置是忽略的*/  
  28.         // Check if Pseudo-Term pair was created  
  29.         if(rtnVal != -1)  
  30.         {  
  31.                 pName = ptsname(mpty);//get slave device name, the arg is the master device  
  32.                 printf("Name of slave side is <%s>    fd = %d/n", pName, spty);  
  33.                  
  34.                 strcpy(sptyname, pName);  
  35.                 printf("my sptyname is %s/n",sptyname);  
  36. //test write to mpty and read from spty*************  
  37. char temp[50] = {"hell/nworld ! i have write to mpty!"};  
  38. char temp2[100] = {'/0'};  
  39. c = write(mpty,temp,5);  
  40. if(c <=0)  
  41.         printf("ERROR : can not write to mpty/n");  
  42. sleep(3);  
  43. printf("write %d charactors to mpty success/n",c);  
  44. sleep(3);  
  45. printf("try to read from spty/n");  
  46. sleep(3);  
  47. c = read(spty,temp2,5);  
  48. if(c <=0)  
  49.         printf("ERROR : can not read from mpty/n");  
  50. printf("read from spty  %d charactors success/n",c);  
  51. printf("/n>>>>>  %s  <<<<</n/n___________________/n",temp2);  
  52. //**************************************************  
  53.   
  54.                 // Go into loop and read what is sent to Slave side of pair  
  55.                 while(1)  
  56.                 {  
  57.                         c = read(mpty, ptyBuff, 100);  
  58.                         if(c > 0)  
  59.                         {  
  60.                                 printf("###-<%d>/n", c);  
  61.                                 printf("buff:__|%s",ptyBuff);  
  62.                         }  
  63.                 }  
  64.         }  
  65.          
  66.         else  
  67.         {  
  68.                 printf("PseudoTerm, creation failed.../n");  
  69.         }  
  70.         return rtnVal;  
  71. } http://blog.163.com/coder_jack@126  
  72.   
  73. 伪终端的操作:  
  74. 以下是源码,来自/fsl_myandroid_r6/external/qemu/vl.c   line 2545  
  75. r7里面都已经没有了!真快呀!哈哈  
  76. /* Once Solaris has openpty(), this is going to be removed. */  
  77. int openpty(int *amaster, int *aslave, char *name,  
  78.             struct termios *termp, struct winsize *winp)  
  79. {  
  80.         const char *slave;  
  81.         int mfd = -1, sfd = -1;  
  82.         *amaster = *aslave = -1;  
  83.         mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);  
  84.         if (mfd < 0)  
  85.                 goto err;  
  86.         if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)  
  87.                 goto err;  
  88.         if ((slave = ptsname(mfd)) == NULL)  
  89.                 goto err;  
  90.         if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)  
  91.                 goto err;  
  92.         if (ioctl(sfd, I_PUSH, "ptem") == -1 ||  
  93.             (termp != NULL && tcgetattr(sfd, termp) < 0))  
  94.                 goto err;  
  95.         if (amaster)  
  96.                 *amaster = mfd;  
  97.         if (aslave)  
  98.                 *aslave = sfd;  
  99.         if (winp)  
  100.                 ioctl(sfd, TIOCSWINSZ, winp);  
  101.         return 0;  
  102. err:  
  103.         if (sfd != -1)  
  104.                 close(sfd);  
  105.         close(mfd);  
  106.         return -1;  
  107. } http://blog.163.com/coder_jack@126  

0 0