在后台运行任务?

时间:2014-03-02 09:01:27

标签: c linux multithreading timer scheduled-tasks

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 );
          }


     }

       close(acceptSocket);   
       return 0;

     }    

void checkTasks()
{
    int i;
for( i =1; i<= 100 ;i++)
{
    if(++task1_counter ==2 )
    {
        task1_counter = 0;
        makeTimer("First Timer", &firstTimerID, 2, 2);   // creating the timer for 2ms
    }
    else if(++task2_counter == 10)
    {
        task2_counter = 0;
        makeTimer("Second Timer", &secondTimerID, 10, 10);    //creating the timer for 10ms


    }
    else if(++task3_counter == 100)
    {
        task3_counter = 0;
        makeTimer("Third Timer", &thirdTimerID, 100, 100);  //creating the timer for 100ms
    }
    else
        printf("error\n");

}

}


 }


int main()
{


     CreateSocket();            //initializing all the foreground tasks


     while(1)
     {

                 checkTasks();
     }
}

CreateSocket:此API正在不断地从客户端接收数据。我在CreateSocket函数中的while循环中调用了recvfrom api(在上面的代码中没有显示)。我在main函数的while循环中调用checkTasks。它正在检查计数器并调用makeTimer()函数调用:此函数定义用于创建计时器并通过信号每2ms,10ms和100ms调用任务。

我的问题:一旦在main()中调用一个init函数:它是否会从客户端接收数据,并且在后台checkTasks将继续运行?是不是?

0 个答案:

没有答案