ctimerwatchdogmsgrcv

Implementing a WatchDog timer


I need to implement a timer that checks for conditions every 35 seconds. My program is using IPC schemes to communicate information back and forth between client and server processes. The problem is that I am running msgrcv() function in a loop, which pauses the loop until it finds a message, which is no good because I need the timer to always be checking if a client has stopped sending messages. (if it only checks when it receives a message, this will be useless...)

The problem may seem unclear, but the basics of what I need is a way to implement a Watchdog timer that will check a condition every 35 seconds.

I currently have this code:

time_t start = time(NULL);

//Enter main processing loop
while(running)
{
    size_t size = sizeof(StatusMessage) - sizeof(long);

    if(msgrcv(messageID, &statusMessage, size, 0, 0) != -1)
    {
        printf("(SERVER) Message Data (ID #%ld)  =  %d  :  %s\n", statusMessage.machineID, statusMessage.status_code, statusMessage.status);

        masterList->msgQueueID = messageID;
        int numDCs = ++masterList->numberOfDCs;

        masterList->dc[numDCs].dcProcessID = (pid_t) statusMessage.machineID;
        masterList->dc[numDCs].lastTimeHeardFrom = 1000;

        printf("%d\n", masterList->numberOfDCs);
    }

    printf("%.2f\n", (double) (time(NULL) - start));
}

The only problem is as I stated before, the code to check how much time has passed, won't be reached if there is no message to come in, as the msgrcv function will hold the process.

I hope I am making sense, and that someone will be able to assist me in my problem.


Solution

  • You may want to try the msgctl(msqid, IPC_STAT, struct msqid_ds *msgqdsbuf); If the call is successful, then the current number of messages can be found using msgdsbuf->msg_qnum. The caller needed read permissions, which I think you may have in here.