Using separate window child process for stdin/stdout

Using separate window child process for stdin/stdout

Post by Russ Vreelan » Thu, 01 Jun 2000 04:00:00



I have an application which needs to spawn a separate window (xterm,
shelltool, whatever, I don't care) and use that window to pass data in and
out of the parent process. The parent's window is too busy with other status
and info msgs. The parent can poll the stdin pipe with a non-blocking
read().

I know how to set up the pipes and redirect stdin and stdout from a child
process, but I'm stuck on how to do the separate window without getting too
deep into all the terminal IO stuff. Surely there is a straightforward way
to do this?
( I don't need to handle anything other than dumb text entry ).

Any help would be most appreciated.

rev3

 
 
 

Using separate window child process for stdin/stdout

Post by Andrew Gabri » Fri, 02 Jun 2000 04:00:00




Quote:> I have an application which needs to spawn a separate window (xterm,
> shelltool, whatever, I don't care) and use that window to pass data in and
> out of the parent process. The parent's window is too busy with other status
> and info msgs. The parent can poll the stdin pipe with a non-blocking
> read().

> I know how to set up the pipes and redirect stdin and stdout from a child
> process, but I'm stuck on how to do the separate window without getting too
> deep into all the terminal IO stuff. Surely there is a straightforward way
> to do this?

You need to create a pseudo tty master and slave pair. Your program
then talks to the slave side, and you fork/exec an xterm, handing it
the master side and using the -S option to tell it which fd the pty
master is.

I posted an article on doing this a month ago...

     http://www.deja.com/getdoc.xp?AN=618136725&fmt=text

--
Andrew Gabriel

 
 
 

Using separate window child process for stdin/stdout

Post by Chuck Dillo » Fri, 02 Jun 2000 04:00:00



> I have an application which needs to spawn a separate window (xterm,
> shelltool, whatever, I don't care) and use that window to pass data in and
> out of the parent process. The parent's window is too busy with other status
> and info msgs. The parent can poll the stdin pipe with a non-blocking
> read().

If I understand you correctly you have two ways to go that I can
think of.

Option 1. Use a terminal emulator (e.g. xterm) by:
        a) Write a buddy program to work with your parent which passes information
           to/from your server using IPC (e.g. a fifo).
        b) Have your parent parent fork/exec a terminal emulator with the to
           run the buddy program.  For example,
                xterm -e buddy /tmp/myfifo
The buddy interacts with the screen/user via the emulator and reports to the parent
via the fifo.

Option 2. Roll your own emulator using ptys.  In this case your buddy
program creates it's own window and does it's own terminal emulation
via pesudo terminals.  You can shorten development time on this approach
by leveraging the functions in libexpect.  See http://expect.nist.gov/.

HTH,

ced

--
Chuck Dillon
Senior Software Engineer
Genetics Computer Group, a subsidiary of Oxford Molecular

 
 
 

Using separate window child process for stdin/stdout

Post by Russ Vreelan » Fri, 02 Jun 2000 04:00:00


Thanks, this approach works very well. I've copied 2 small test programs at
the bottom if anyone's interested. (solaris 2.6)

-rev3



>> I have an application which needs to spawn a separate window (xterm,
>> shelltool, whatever, I don't care) and use that window to pass data in
and
>> out of the parent process. The parent's window is too busy with other
status
>> and info msgs. The parent can poll the stdin pipe with a non-blocking
>> read().

>If I understand you correctly you have two ways to go that I can
>think of.

>Option 1. Use a terminal emulator (e.g. xterm) by:
> a) Write a buddy program to work with your parent which passes information
>    to/from your server using IPC (e.g. a fifo).
> b) Have your parent parent fork/exec a terminal emulator with the to
>    run the buddy program.  For example,
> xterm -e buddy /tmp/myfifo
>The buddy interacts with the screen/user via the emulator and reports to
the parent
>via the fifo.

>Option 2. Roll your own emulator using ptys.  In this case your buddy
>program creates it's own window and does it's own terminal emulation
>via pesudo terminals.  You can shorten development time on this approach
>by leveraging the functions in libexpect.  See http://expect.nist.gov/.

>HTH,

>ced

>--
>Chuck Dillon
>Senior Software Engineer
>Genetics Computer Group, a subsidiary of Oxford Molecular

do a 'mkfifo /tmp/fifo' before running.
=====================Program
1==================================================

define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* main routine to test all this */
int main(int argc, char **argv) {

  pid_t Child_id;
  int rc;
  int n;
  int i;
  int fifoInput;
  int bufsize = 200;
  char buf[200];

  Child_id = fork();

  if(Child_id == -1) {
    fprintf(stderr,"Fork failed\n");
    exit(0);
  }

  if (Child_id) {    /* Parent */

    if ((fifoInput = open("/tmp/fifo",(O_RDWR | O_NONBLOCK))) < 0) {
      fprintf(stderr,"Fifo input open failed \n");
      exit(0);
    }
    while (1) {
      if ( (n = read(fifoInput, buf, bufsize)) > 0) {
        buf[n] = '\0';
        printf("Saw this: ");
        printf("%s\n",buf);
      }
      sleep(1);
    }
  } else {   /* child */

    rc = execl("/tools/bin/xterm" ,"xterm", "-bg", "white", "-fg", "black",
"-title", "Terminal Window", "-e", "/path/uart_buddy", "/tmp/fifo", (char
*)0);
    fprintf(stderr,"child: execl failed: %d\n",rc);
    exit(0);
  }

Quote:}

======================Program 2========================================
/* uart_buddy */
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

  int fifoOutput;
  char buf[200];
  int n;
  int bufsize = 200;

  fprintf(stderr,"Using fifo at %s\n",argv[1]);

  if ((fifoOutput = open (argv[1],O_WRONLY)) < 0) {
    fprintf(stderr,"Fifo output open failed \n");
    exit(0);
  }

  while (1) {
    putchar('>');
    gets(buf);
    if ( (n = write(fifoOutput, buf, bufsize)) < 0) {
      fprintf(stderr,"Couldn't write to /tmp/fifo\n");
      exit(0);
    }
  }

- Show quoted text -

Quote:}