c++ - Read() from file descriptor hangs -
Hey, this should be my last PTY-related question and I can move on to more exciting issues. (C;
Here is a set of small tasks that I have written to write and write PTI: The only problem is that they do not work! I have to run and write the initiator ("Ls -l"), 'Output' from readFromPty is still empty.
Ubuntu, QT C ++
EDITED: OK, I read except the loop All things can be confirmed. The Debugger's Local / Watter's tab shows that the 'Qtr \' output is actually Only after reading the data, but after that (reading ()) exits the characters from the output and then hangs up. What is happening, and how can I fix this? Thanks: (C:
< Pre> #include & lt; iostream & gt; # Include & lt; unistd.h & gt; # Include & lt; utmp.h & gt; #include & lt; pty .h & gt; # Include & lt; Qestring & gt; Include & lt; Quthad & gt; // You also need libutil in your .pro file to compile. Category CMkPty {Public: CMkPty (int * writeChannel, int * ReadChannel); ~ CMkPty); Int Run Input (four * command); Inuit Lit Tope (four * input); Int readFromPty (QString Output); Int m_nPid; Private: int m_nMaster, m_nSlave, m_nPosition, m_nBytes; Char * m_ch name; Zero secure_print (four * s); Four m_output; }; CMKPT :: CMKPTI (Int * MasterFD, Int * SlaveFDi) {Open Feety (and M_N Mester, and M_NS Slave, (Four *) 0, __nell, __nell); M_nPid = fork (); * MasterFD = M_NMaster; * SlaveFD = m_nSlave; If (m_nPid == 0) {login_tty (m_nSlave); Execl ("/ bin / bash", "-l", (four *) 0); Return; } And if (m_nPid> 0) {return; } And if (m_nPid <0) {std :: cout & lt; & Lt; "Failed to fork." ; Return; }} CMKPT :: ~ CMKPT () {closed (M_NM); Close (M_Neslave); } Int CMkPty :: writeToPty (char * szInput) {int nWriteTest; Type (m_nmaster, szInput, sizeof (szInput)); Write NWriteTest = (m_nMaster, "\ n", 1); If (nWriteTest & lt; 0) {std :: cout & lt; & Lt; "Write PTY Failed"; Return -1; } Return 0; } Intel CMkPty :: readFromPty {quarters} {four buffers [160]; M_nBytes = sizeof (buffer); While reading (m_nPosition =) (m_nMaster, buffer, m_nites)) gt; {buffer [m_nPosition] = 0; output = buffer;} return 0;}
edit: finally my A link to the question with the code that works for.
I'm completely familiar with posix, But after reading this page I had some information. What's more, I do not see the adjustment of your M_nBytes value if you do not expect to pass before loop Do not read as much.
Edit: From that link, maybe it will be of some help:
If the pipe is open for writing a process and the O_Nblock is clear, read ( ) Will block the calling thread unless the data is written or the pipe is closed from all the processes on which the pipe was open to write
A file (other than a pipe or fifo) When trying to read, which reads non-intercept and above There is currently no data available:
* If O_NONBLOCK is clear, read () will block calling location until some data is available
Therefore, if you must An error is not in the situation, and you ask for it to continue reading, it will be blocked until something is found in reading it.