问题
I have been working on launching several threads from my program from which I'd like each of them to output to their respective terminal windows (that I have popping up in a tiled pattern).
They are not shells, just terminal windows for output.
I'm coding on ubuntu in C. I have it working, but for some reason, after a few minutes it's as though the xterm display buffers fill up (independent of one another) and they stop displaying new text. The old text remains, the threads keep doing their thing in the background, but just no new output.
I've tried everything I can find and remain baffled.
I can't seem to find a possible:
- buffer I need to flush or clear
- a way to clear the screen (those VT escape codes do nothing even after I've redirected input or tried sending them as output to xterm)
- a way to reposition the cursor at the top left of the terminal window
- a way to reset the file descriptor that I have pointed to the appropriate /dev/pts
Here is how it stands now (also please feel free to point out anything redundant or wrong in what I'm doing; N.B. portability isn't very important in this situation). I have omitted error checking. I wish I didn't have to use system() but this is the only way I could get it working (posix_spawn wasn't working either). This code is run in each thread (the tiling effect is not shown, but it's managed through shared memory and some additional parameters on the xterm commandline):
char buf_xt[256]; // Used along with sprinf() and write() to output to xterm
char *pSptyName;
int xterm_fd1; // File descriptor for xterm
xterm_fd1 = posix_openpt(O_RDWR | O_NONBLOCK | O_NOCTTY);
pSptyName = ptsname(xterm_fd1);
sprintf(buf_xt, "xterm -S%s/%d &", pSptyName, xterm_fd1);
system(buf_xt);
I output to xterm like this:
sprintf(buf_xt, “Writing to xterm.\n”);
write(xterm_fd1, buf_xt, strlen(buf_xt));
I'm hopeful that maybe newterm() from curses could do the trick (open to other ideas too), but I can't find any useful literature or examples anywhere, so I'm hopeful someone here can provide some real info other than RTFM (I have already but a concrete implementation would help fill in the gaping holes in the documentation).
As a sidenote, when I close up the program, I don't have a way to close the xterm instances individually. I have to make another system()
call to killall xterm
, so any ideas on this would also be greatly appreciated.
Thank you
回答1:
In the ncurses sources, the ditto
program (test/ditto.c) can be compiled to make several xterms, e.g., running it as
ditto first second
to create two xterms with those titles (in addition to the screen where you start).
That uses newterm
. To do this you would have to configure ncurses with the --with-pthread
option. That does not solve the problem of closing the xterms.
来源:https://stackoverflow.com/questions/35142200/multiple-xterm-pseudoterminals-used-as-output-only-from-multiple-threads