简体   繁体   English

Linux中Pseudo Terminal的读写问题

[英]Read Write issues with Pseudo Terminal in Linux

I am writing a C++ program that would interact with an external process. 我正在编写一个C ++程序,它将与外部进程进行交互。 The external process is written in C# and runs on mono. 外部过程是用C#编写的,并且在mono上运行。 Note that I cannot modify the C# code as it is not a program written by me. 请注意,我不能修改C#代码,因为它不是我编写的程序。

In this regard, I first set out by using pipes, which of course as I later realized is fully buffered and hence I faced a lot of sync issues. 在这方面,我首先使用管道进行了设置,后来我意识到它是完全缓冲的,因此我遇到了很多同步问题。 Essentially the external process had to flush its output after every write and this was not possible. 本质上,外部进程必须在每次写入后刷新其输出,而这是不可能的。

The next thing that I was about to try out was files, but however I found out that using pseudo-terminals would be more apt in my case. 我接下来要尝试的是文件,但是我发现使用伪终端会更适合我的情况。 Here is some sample code that I have written: 这是我编写的一些示例代码:

int main()
{
    int fdm, fds, rc, pid;
    bool rValue;
    /* Setup Master pty*/
    rValue = rValue && (fdm = posix_openpt(O_RDWR)) >= 0 &&
             (rc = grantpt(fdm)) == 0 && (rc = unlockpt(fdm) == 0);
    if (rValue) {
        /* Open Slave pty */
        fds = open(ptsname(fdm), O_RDWR);
        pid = fork();
        if(pid < 0)
            perror("fork failed");
        else if(pid == 0) //child
        {
            close(fdm); //close master
            struct termios slave_orig_term_settings;
            struct termios new_term_settings;
            tcgetattr(slaveTTY, &slave_orig_term_settings);
            new_term_settings = slave_orig_term_settings;
            cfmakeraw(&new_term_settings);
            tcsetattr(slaveTTY, TCSANOW, &new_term_settings);

            //redirect I/O of this process
            close(0);
            close(1);
            close(2);
            dup(slaveTTY);
            dup(slaveTTY);
            dup(slaveTTY);

            close(slaveTTY);

            setsid();
            ioctl(0, TIOCSCTTY, 1);

            //launch the external process and replace its image in this process
            execve(argv[0],...);
        }
        else
        {
            close(fds); //close slave
            //Perform some interaction
            write(something using fdm);
            //Assume fdsets declared and set somewhere here
            select(fdm +1,&fdset,NULL,NULL,NULL);
            int readBytes = read(someting using fds);
        }
    }
    return EXIT_SUCCESS;
}

Assume that the fdset and fdclr for select are being taken care of. 假设正在处理用于选择的fdset和fdclr。

The following issues are being observed in the parent process: 在父流程中观察到以下问题:

  1. Sometimes read returns with readBytes > 0 but there is nothing present in the buffer 有时读取返回readBytes> 0,但缓冲区中不存在任何内容
  2. Sometimes whatever has been written to the terminal is read back 有时会读回已写入终端的任何内容
  3. Some garbage values such as ^]]49]1R are being dumped on the terminal (this is the actual terminal ie my output window) 一些垃圾值,例如^]] 49] 1R正在转储到终端上(这是实际的终端,即我的输出窗口)

PS: When the external process is written in C/C++, this issue is not occuring. PS:使用C / C ++编写外部进程时,不会发生此问题。 Only when I run a C# program in mono. 仅当我在mono中运行C#程序时。

I think pexpect in python is a good choice if you don't have to do that in C++ , it will save you a lot of time. 我认为,如果您不必在C++这样做,则python中的pexpect是一个不错的选择,它将节省大量时间。 And also you can use python freeze tools like pyinstaller to convert your python script to standalone binary. 此外,您还可以使用pyinstaller等python冻结工具将python脚本转换为独立的二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM