SIGIO only mean readable or writable, how channel event, avoid writable

yang chen shanchuan04 at gmail.com
Mon Nov 13 16:10:03 UTC 2017


I write a c source file, and test in my machine. At the beginning, the fd
is writable when the fd is open but the process doesn't receive the SIGIO.
so i'm confused a lot of paper or books say that the process will receive
SIGIO when the fd is writable or readable. but in fact it doesn't. so any
idea?

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<fcntl.h>
#include <sys/ioctl.h>
#include<signal.h>
#include<string.h>


void ngx_signal_handler(int signo, siginfo_t *siginfo, void *ucontext){
    printf("%d\n", signo);
}

int main()
{
    int sv[2];
    if(socketpair(PF_LOCAL,SOCK_STREAM,0,sv) < 0)
    {
        perror("socketpair");
        return 0;
    }
    struct sigaction sa;

    memset(&sa, 0, sizeof(struct sigaction));

    sa.sa_sigaction = ngx_signal_handler;
    sa.sa_flags = SA_SIGINFO;

    sigaction(SIGIO, &sa, NULL);

    pid_t id = fork();
    if(id == 0)
    {

        close(sv[0]);

        const char* msg = "i'm child\n";
        char buf[1024];
        while(1)
        {
            /*write(sv[1],msg,strlen(msg));*/
            sleep(1);

            /*ssize_t _s = read(sv[1],buf,sizeof(buf)-1);*/
            /*if(_s > 0)*/
            /*{*/
            /*    buf[_s] = '\0';*/
            /*    printf(" %s\n",buf);*/
            /*}*/
        }
    }
    else   //father
    {
        close(sv[1]);
        const char* msg = "i'm father\n";
        char buf[1024];
        int on = 1;
        if (ioctl(sv[0], FIOASYNC, &on) == -1) {
            return 1;
        }

        if (fcntl(sv[0], F_SETOWN, getpid()) == -1) {
            return 1;
        }
        while(1){

            /*ssize_t _s = read(sv[0],buf,sizeof(buf)-1);*/
            /*if(_s > 0)*/
            /*{*/
            /*    buf[_s] = '\0';*/
            /*    printf(" %s\n",buf);*/
            /*    sleep(1);*/
            /*}*/
            /*write(sv[0],msg,strlen(msg));*/
        }
    }
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20171114/13b82f0e/attachment.html>


More information about the nginx mailing list