[comp.unix.programmer] Question about SIGIO

chan@icsid2.icsi (Pamela Chan) (06/02/91)

Not long ago someone suggested that in order to catch the SIGIO signal
when doing non-blocking IO, one has to do a F_SETOWN to
specify the process group to receive the signal.

So here is the code I came up with:

THE RECEIVING SIDE
------------------
    fcntl(sock, F_SETOWN, getpid());
    fcntl(sock, F_SETFL, FASYNC|FNDELAY);
    for(;;){
	if (ret = read(sock, buf, 64)){      // read from socket
	    if (ret != -1){
		ret = write(1, buf, ret);    // output to stdout
	    }else{
		sigpause(0);                 // block wait for SIGIO
	    }
	}else{
	    quit();
        }
    }


THE SENDING SIDE
----------------
    fcntl(sock, F_SETOWN, new_pid);
    fcntl(sock, F_SETFL, FNDELAY|FASYNC);
    for(;;){
	if (ret1 = read(fd, buf, 1024)){  // reading from datafile
	    ret = write(sock, buf, ret1); // output to socket
	    if (ret<ret1) {
		ret1 -= ret;
		sigpause(0);              // block wait for SIGIO
	    }
	}
    }

The problem is that only the receiving side can detect the SIGIO
signal, but not the sending side.  I'm wondering if there is another
trick other than F_SETOWN that I need to do in order to detect the
signal. One interesting point is if I kill the connection at the
receiving end, the blocked sender will get a SIGIO and a SIGPIPE at
the same time.

--------------------------------------------------------------------
Pamela Chan               International Computer Science Institute
E-mail: chan@icsi         Berkeley, California