Thread: SIGIO How-to

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    SIGIO How-to

    Does anyone know of any good references on using SIGIO. I have searched around but have been able to find very little on the subject. I have taken a look at the fcntl man page which gives a bit of information on it, but am still unsure how it works.

    Thanks for all of your help.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    What do you want to know about sigio? It is sent to a program when io has become possible. You may want to setup a signal handler using signal(), sigaction(), etc.
    fcntl() man page
    F_SETSIG
    Sets the signal sent when input or output becomes possible. A value of zero means to send the default SIGIO signal. Any other value (including SIGIO) is the signal to send instead, and in this case additional info is available to the signal handler if installed with SA_SIGINFO.
    so something like this should do
    ret = fcntl(fd, SETSIG, sig);
    Last edited by chrismiceli; 08-11-2006 at 12:50 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Okay, thanks. How do I find out for what file descriptor the signal is related to and what event has happened. I am looking around for a complete example but have been unable to find one.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I don't think that there is a way to tell what file descriptor is available for io, but you might want to look into the select function. This function comes in handy when working with sockets, so files should be the same. Signals are kind of like interrups and can't really carry data with them (like which file is ready), that is if I recall correctly.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    After looking around the net I came across a presentation, which had a bit of information on it and what appears to be a code example. But I can not make heads nor tails of it.
    Snippet 1:
    Code:
    int sigio_add_fd(int fd) { 
    static const int signum=SIGRTMIN+1; 
    static pid_t mypid=0; 
    if (!mypid) mypid=getpid(); 
    fcntl(fd,F_SETOWN,mypid); 
    fcntl(fd,F_SETSIG,signum); 
    fcntl(fd,F_SETFL,fcntl(fd,F_GETFL)|O_NONBLOCK|O_ASYNC); 
    } 
    int sigio_rm_fd(struct sigio* s,int fd) { 
    fcntl(fd,F_SETFL,fcntl(fd,F_GETFL)&(~O_ASYNC)); 
    }
    Snippet 2:
    Code:
    for (;;) { 
    timeout.tv_sec=0; 
    timeout.tv_nsec=10000; 
    switch (r=sigtimedwait(&s.ss,&info,&timeout)) { 
    case -1: if (errno!=EAGAIN) error("sigtimedwait"); 
    case SIGIO: puts("SIGIO queue overflow!"); return 1; 
    } 
    if (r==signum) handle_io(info.si_fd,info.si_band); 
    }
    I get the first part, about declaring interest, but have very little idea what is going on in the second bit. I believe that the call to sigtimedwait is getting it to wait for a signal.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    What are you trying to do and I can tell you if that code is necessary or not.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed