Hi Frnds,
I am posting my first query in this forum, expecting needful reply
I am using UDP (SOCK_DGRAM) sockets for Inter Process Communication (with in the device, I want to communicate to other processes).

Socket Creation Code:
Code:
gIPCFd = socket(PF_UNIX, SOCK_DGRAM, AF_UNIX);
For this, we are creating a socket for each process to send any message to out to the other module. The code is as follows to send a UDP packet *pMsg from a process to the process *to.

SendMessage Function Code:
Code:
INT8 SendMessage(char *to, void *pMsg, UINT16 msgSize, INT16 timeOut)
{
    INT8 retValue, ipcStat = IPC_SUCCESS;
    INT32 size;
    struct sockaddr_un toAddr;
    fd_set writeFdSet;
    struct timeval waitTime;
    UINT32  save_errno = 0;
 
    /************************************************************
     * socket path is ./procname                                *
     ************************************************************/
    sprintf(toAddr.sun_path, "./%s", to);
    toAddr.sun_family = AF_UNIX;
 
    /* select with timeout if timeOut is >= 0 */
    if(timeOut >= 0)
    {
        FD_ZERO(&writeFdSet);
        FD_SET(gIPCFd, &writeFdSet);
        waitTime.tv_sec = timeOut/1000;
        waitTime.tv_usec = (timeOut % 1000) * 1000;
        retValue = select(gIPCFd+1, NULL, &writeFdSet, NULL, &waitTime);
        if (-1 == retValue)
        {
            //save_errno = errno;
            perror("select");
            //fprintf(stderr, "msgrcv failed with error: %d\n", errno);
            DEBUG_ERROR(("Kishore: The errno for select to send %s message to module %s is %u\n",
                                CSGetMessageType(pCSMsg_->header.type), toAddr.sun_path, save_errno));
        }
    }
    else
    {
        /* write anyway */
        retValue = 1;
    }
 
    if(retValue > 0)
    {
        size = sendto(gIPCFd, pMsg, msgSize,
                        FLAGS_NONE,
                        (struct sockaddr *) &toAddr, sizeof(toAddr));
        if(size < 0){
            ipcStat = IPC_FAILURE;
            DEBUG_ERROR(("sendto %s Failed:%ld", toAddr.sun_path, size));
        }
    }else{
        ipcStat = IPC_FAILURE;
        DEBUG_ERROR(("sendto %s Failed:%d", toAddr.sun_path, retValue));
    }
    return(ipcStat);
Here, when I am calling select(), it should return the number of file descriptors to be changed. When I am sending with normal load say 5 to 10 messages / second, it is working fine. (i.e., returning 1 FD, and sendto() will get called, as that happens only select() returns success and more than 1 FD.)
But, when the load is increased to 40-50 messages / second, some times select() is returning 0 File Descriptors and sendto() is not getting called. Hence, I am getting reduced success ratio from application point of view.

I read in one document that, while sending, select() is not required. But, if I call sendto() directly with out calling, select(), my application gets hanged @ immediate sendto().

Here, *timeOut is an argument to the function SendMessage() and I started with sending *timeOut as 0 for the first case. If this fails, I was manually retransmitting with *timeOut as 100ms.

Retransmission Code:
Code:
retIPC = SendMessage( pProcName, pcc, msgSize, 0);
 
 if( retIPC != IPC_SUCCESS )
 {
  DEBUG_ERROR(("Sending message :%hu(reqId:%hu) to %s through IPC failed\n", 
        pcc->header.type, pcc->header.reqId, pcc->header.to));
 
  DEBUG_TRACE(("Kisshu: Trying to resend mssg % hu to %s", pcc->header.type, pcc->header.to));
  retIPC = SendMessage( pProcName, pcc, msgSize, 100 );
  if( retIPC != IPC_SUCCESS )
  {
   DEBUG_ERROR(("Retransmitting message :%hu(reqId:%hu) to %s through IPC failed\n", 
        pcc->header.type, pcc->header.reqId, pcc->header.to));
   retVal = CC_FAILURE;
  }
  else
  {
   DEBUG_TRACE(("Retransmission worked here to send %hu to %s", pcc->header.type, pcc->header.to));
  }
 }
 else
 {
  DEBUG_TRACE(("Successfully sent the message %hu to %s in first trai, YAHOO", 
      pcc->header.type, pcc->header.to));
 }
Here, in success case, select() is returning, 1 FD (if the load is less always I am getting this result). But, when load is more some times i used to get return value of select() is 0, then my retransmission mechanism will come in to picture. In that case, select() is will wait till 100ms over or it returns successfully. I observed combined result in my case. Like, some times select() is returning 1 FD and prinitng left over time (ex. 90ms) and some times it is consuming all 100ms, still it is not getting any FDs and sendto() is not getting called which leads to reduction in the success ratio.

My Q. is, why select() is returning immediately sometimes and not able to get FDs & returning 0 sometimes. On what criterion, select() wait and why it is getting delayed sometimes?
Note: For the sockets we have created, sendbuff size & rcvbuff size is 65535.
Can any one help me regarding the same.

Thanks & Rgds,
Kishore