Re: How to to kill a c program at the exit of my gui?

From: Melissa Schrumpf (reverse-comDOTyahooATm_schrumpf_at_bogus.address)
Date: 05/13/04


Date: Thu, 13 May 2004 08:30:40 -0400

Hao Xu wrote:

> I think that at the exit of the TclTk gui, the $fd breaks, and there
> is EOF of stdin, isn't it? So I want the recv program to know this
> signal, and use the corresponding signal handler to kill itself.

> My question is:
> Is this feasible? If the answer is yes, then how to do this?

Yes, this is feasible, but checking for a broken file descriptor is something
the running program must do, not tcl.

You have three options that I can think of, off the top of my head. On unix,
you can:

    fd_set rset;
    struct timeval t;
    FD_ZERO(&rset);
    FD_SET(STDIN_FILENO, &rset);
    t.tv_sec = 0;
    t.tv_usec = 1;
    while(1) {
        if ((select(STDIN_FILENO+1), &rset, NULL, NULL, &t)>0) {break;}
        // ...
    }

On Windows, you can:

   while(!_kbhit()) {
      if (ferror(stdout) || ferror(stdin) ||
         feof(stdout) || feof(stdin)) {break;}
      // ...
   }

On either platform, you could wait until you expect no more user input (your
normal I/O is complete), then spawn a thread, and have that thread do a
blocking read (getc()) on stdin, then check for ferror or feof on stdin when
getc completes.

-- 
MKS


Relevant Pages

  • Re: HLA Lib
    ... take a look at the conversion routines. ... utilize your own read buffering for file. ... beyond the EOF. ... file data on the stdin, you can handle EOF that way. ...
    (alt.lang.asm)
  • Re: HLA Lib
    ... I think you should also provide some routine to read stdin even ... The fileio package does not do any buffering, ... stdin input deserves same kind of attention as file I/O. ... EOF is a good example. ...
    (alt.lang.asm)
  • Re: fgetc() past EOF
    ... it stops reading the BrainFuck program and starts interpreting it; ... The problem is that stdin seems to lock after fgetcgets ... > an EOF from it, ... > get an EOF, and keep on trying to read from it, the interpreter hangs. ...
    (comp.lang.c)
  • Re: Is there any way to force line-buffering in a pipeline?
    ... By default unbuffer does not read from stdin. ... it is awkward to use because it exits when ... it sees EOF on input, so you have to resort to things like: ... the final grep to have finished reading the data before you close ...
    (comp.unix.shell)
  • Re: Is there any way to force line-buffering in a pipeline?
    ... > By default unbuffer does not read from stdin. ... it is awkward to use because it exits when ... > it sees EOF on input, so you have to resort to things like: ... > the final grep to have finished reading the data before you close ...
    (comp.unix.shell)