0

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

Ctx
  • 18,090
  • 24
  • 36
  • 51
J-a-n-u-s
  • 1,469
  • 17
  • 20
  • 1
    That's hard to debug... One guess: Is it possible that you output a control sequence like XOFF (ctrl+s), an escape sequence or similar? Can you try to filter the output for printable chars only? – Ctx Feb 01 '16 at 23:29
  • Thanks for replying. The stuff I'm outputting is very simple text like `"|---Connecting\n" "|-Talking...\n"` It's output I've coded, not unknown stuff... Any other ideas? I studied this [link] (http://www.linusakesson.net/programming/tty/) and this jumped at me, but I can't find out how to clear that kernel buffer they talk about "The pseudo terminal can only keep a certain amount of data inside its kernel buffer, and when that buffer is full [...]" – J-a-n-u-s Feb 01 '16 at 23:46
  • Can you run strace on a hanging xterm process to see what it is trying to do? – Ctx Feb 02 '16 at 00:00
  • hmmmm do you know how to do that since I'm running it from system() call and just passing the pts location as a command line argument? – J-a-n-u-s Feb 02 '16 at 00:11
  • Get the pid with `ps afx` or similar and do `strace -p ` – Ctx Feb 02 '16 at 00:12

1 Answers1

0

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105