Question

static void timerHandler(int sig, siginfo_t *si, void *uc)
{
  timer_t *tidp;

  tidp = si->si_value.sival_ptr;

  if (*tidp == firstTimerID)

    TASK1(Task2ms_Raster);
  else if (*tidp == secondTimerID)
    TASK2(Task10ms_Raster);
  else if (*tidp == thirdTimerID)
    TASK3(Task100ms_Raster);
}

static int makeTimer(char *name, timer_t *timerID, int expireMS, int intervalMS)
{
  //sigset_t mask;
  struct sigevent te;
  struct itimerspec its;
  struct sigaction sa;
  int sigNo = SIGRTMIN;

  /* Set up signal handler. */
  memset(&sa, 0, sizeof(sa));
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = timerHandler;
  sigemptyset(&sa.sa_mask);
  if (sigaction(sigNo, &sa, NULL) == -1)
  {
    perror("sigaction");
  }

  /* Set and enable alarm */
  te.sigev_notify = SIGEV_SIGNAL;
  te.sigev_signo = sigNo;
  te.sigev_value.sival_ptr = timerID;
  timer_create(CLOCK_REALTIME, &te, timerID);

  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = intervalMS * 1000000;
  its.it_value.tv_sec = 0;
  its.it_value.tv_nsec = expireMS * 1000000;
  timer_settime(*timerID, 0, &its, NULL);

  return 1;

}

int CreateSocket()
{

  socklen_t len = sizeof(client);
  // Socket creation for UDP

  acceptSocket = socket(AF_INET, SOCK_DGRAM, 0);

  if (acceptSocket == -1)

  {
    printf("Failure: socket creation is failed, failure code\n");

    return 1;
  }
  else
  {
    printf("Socket started!\n");

  }

  //non blocking mode
  /* rc = ioctl(acceptSocket, FIONBIO, (char *)&flag);
   if (rc < 0)
   {
   printf("\n ioctl() failed \n");
   return 0;
   }*/

  //Bind the socket
  memset(&addr, 0, sizeof(addr));

  addr.sin_family = AF_INET;

  addr.sin_port = htons(port);

  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  rc = bind(acceptSocket, (struct sockaddr*) &addr, sizeof(addr));

  if (rc == -1)
  {
    printf("Failure: listen, failure code:\n");

    return 1;
  }
  else
  {
    printf("Socket an port %d \n", port);

  }

  if (acceptSocket == -1)
  {
    printf("Fehler: accept, fehler code:\n");

    return 1;
  }
  else
  {
    while (rc != -1)
    {

      rc = recvfrom(acceptSocket, buf, 256, 0, (struct sockaddr*) &client,
          &len);
      if (rc == 0)
      {
        printf("Server has no connection..\n");
        break;
      }
      if (rc == -1)
      {
        printf("something went wrong with data %s", strerror(errno));
        break;
      }

      XcpIp_RxCallback((uint16) rc, (uint8*) buf, (uint16) port);
      makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
      makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
      makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
      while (1)
        ;;

    }
  }

  close(acceptSocket);

  return 0;

}

int main()
{
  Xcp_Initialize();
  CreateSocket();
  return 2;
}

void XcpApp_IpTransmit(uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes)
{
  if ((long) XcpPort == port)
  {
    sentbytes = sendto(acceptSocket, (char*) pBytes, (long) numBytes, 0,
        (struct sockaddr*) &client, sizeof(client));
  }
  XcpIp_TxCallback(port, (uint16) sentbytes);
}

I am working on a client and server architecture. Server code is shown above and I created a socket to recieve the request from the client via the ip address and port number. Server is waiting for a request from the client and send a response back to the client. when ever it recieves data from the client, it should call the timer task (i.e callBackTimers in my code), For that I also created timer to call the task for every 2ms, 10ms and 100ms.

My QUESTION : In debug mode - control is reaching the maketimer function call but it is not running automatically (I did not add any break point). it is halting at maketimer3. How to make it run without halting ??

No correct solution

OTHER TIPS

As I answered in your previous question, this probably has to do with your CreateSocket function, namely:

while(rc!=-1)
{
     rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);
     if(rc==0)
     {
       printf("Server has no connection..\n");
       break;
     }
     if(rc==-1)
     {
         printf("something went wrong with data %s", strerror(errno));
       break;
     }

     XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );

     makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
     makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
     makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms

     while(1)
     ;;
}

You are creating new timers every time through this loop. You never delete them. There is a limit to how many timers you can create. From man (2) timer_create:

The kernel preallocates a "queued real-time signal" for each timer created using timer_create(). Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit (see setrlimit(2)).

You aren't checking the return code status of timer_create and my guess is that you run out of timers and then are just failing after that.

(BTW, not sure what the while(1);; is suppose to do. I understand the frustration you must be feeling but this is becoming something of a moving target.)

Here is a hastily (but I think accurate) chopped up version of your code. Because of your while(1); in CreateSocket it is just going to create the 3 timers. If you run it you will see that TASKS1/2/3 run. This, in itself, is not your problem.

#define _POSIX_C_SOURCE 199309
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>


timer_t firstTimerID, secondTimerID, thirdTimerID;

void TASK1() {printf("task 1\n");}
void TASK2() {printf("task 2\n");}
void TASK3() {printf("task 3\n");}


static void timerHandler(int sig, siginfo_t *si, void *uc)
{
  timer_t *tidp;

  tidp = si->si_value.sival_ptr;

  if (*tidp == firstTimerID)
    TASK1();
  else if (*tidp == secondTimerID)
    TASK2();
  else if (*tidp == thirdTimerID)
    TASK3();
}

static int makeTimer(char *name, timer_t *timerID, int expireMS, int intervalMS)
{
  //sigset_t mask;
  struct sigevent te;
  struct itimerspec its;
  struct sigaction sa;
  int sigNo = SIGRTMIN;

  /* Set up signal handler. */
  memset(&sa, 0, sizeof(sa));
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = timerHandler;
  sigemptyset(&sa.sa_mask);
  if (sigaction(sigNo, &sa, NULL) == -1)
  {
    perror("sigaction");
  }

  /* Set and enable alarm */
  te.sigev_notify = SIGEV_SIGNAL;
  te.sigev_signo = sigNo;
  te.sigev_value.sival_ptr = timerID;
  timer_create(CLOCK_REALTIME, &te, timerID);

  its.it_interval.tv_sec = 0;
  its.it_interval.tv_nsec = intervalMS * 1000000;
  its.it_value.tv_sec = 0;
  its.it_value.tv_nsec = expireMS * 1000000;
  timer_settime(*timerID, 0, &its, NULL);

  return 1;

}

int CreateSocket()
{
    int rc = 5;

    while (rc != -1)
    {
      makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
      makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
      makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms

      while (1);;
    }

  return 0;
}

int main()
{
  CreateSocket();
  return 2;
}

Take care of the loop issue and see where you have to go from there. Create the 3 timers. If you only want them to go off once per socket read then either don't have an interval value so they don't repeatedly go off or reset the timers (i.e. zero out the value/interval values) after the first time they go off. But in either case, you should reuse them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top