Search code examples
socketsunixunix-socket

Unix domain socket queue details


In UNIX domain socket (AF_UNIX Type & DGRAM) , when a reader is slow , the no. of packets queued is 'max_dgram_qlen' . Is there any way to identify bytes occupied queued (or) no. of more bytes free in the queue either on the sender or receiver side. Or atleast , the writer is blocked when the queue is full. Is there any way identify the writer block event ? Thanks in advance.


Solution

    1. You can use the ioctl to find out.

    To check a write buffer if it empty (assuming you have already put data there and want to check if they were consumed):

    ioctl(fd, SIOCOUTQ, &pending);
    

    Where fd is the socket’s file descriptor and pending the variable were the remaining size of data will be returned.

    To check a read buffer if it empty (assuming someone has already put data there and you want to check if they there is any without consuming them):

    ioctl(fd, SIOCINQ, &pending);
    /*note the difference on the second parameter, where we change the flag from SIOCOUTQ to SIOCINQ*/
    
    1. Based on the error message returned by the send() function , we can identify the buffer full event.By checking the error==ENOBUFS you can identify the buffer full.