-1
 void CheckTasks()
 {
     makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
             makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
             makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
 }

int CreateSocket()
 {
     socklen_t len;
     struct sigaction sa;
     memset(&sa, 0, sizeof(sa));



   // 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");

   }

 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("Oh dear, something went wrong with bind()! %s\n", strerror(errno));

     return 1;

 }

 else

 {

   printf("Socket an port %d \n",port);
 }


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


             sa.sa_handler = &CheckTasks;
             sigaction(SIGUSR1, &sa, NULL);


   }


   close(acceptSocket);



   return 0;
   }

I created a socket to receive the data from the client and after receiving the data from client I should call the CheckTasks function, so I am using a signal handler to call this function but why isn't it coming out of the XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port ); ?? If i do like above for my situation - will it work ?? I have to call the CheckTasks function as soon as I receive the data from the client. I am receiving the data from the client. but I am failing to call the CheckTasks function to do further processing :( Could someone guide me in this ??

1 Answers1

1

There are a couple of reasons. The first, is that nothing ever sends SIGUSR1 to your process. They are user-defined signals, so they aren't triggered by any particular action. You can explicitly send them programmatically:

#include <signal.h>

kill(pid, SIGUSR1);

where pid is the process id of the receiving process. At the receiving end, you can register a signal handler for them:

#include <signal.h>

void my_handler(int signum)
{
    if (signum == SIGUSR1)
    {
        printf("Received SIGUSR1!\n");
    }
}

signal(SIGUSR1, my_handler);

Also your intend for using signal is i don't understand. Because you can directly call the function CheckTasks when you receive data from client without using signals.

You can parallel run checkTask function by making as thread

void *CheckTasks( void *ptr );
pthread_t thread1;
char *message = "This is check Task"
pthread_create( &thread1, NULL, CheckTasks, (void*) message);

And your check task should be

void *CheckTasks( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
    //Rest of your code here
}
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • could you please give me a small example ? – user3418769 Mar 21 '14 at 09:52
  • one more question : If I want that CheckTasks function to run in parallel or background ?? how is it possible ?? could you please give me an example ?? can I have your email id ?? badly need your help ?? – user3418769 Mar 21 '14 at 09:59
  • If i make CreateSocket in separate thread and CheckTasks in the main function . Is it possible ?? – user3418769 Mar 21 '14 at 10:19