2014-02-25 6 views
0
#define million 1000000L 

timer_t firstTimerID, secondTimerID, thirdTimerID; 
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster; 

struct sockaddr_in addr, client; 
int acceptSocket; 
char buf[256]; 
long rc, sentbytes; 
int port = 18033; 


void TASK1(Task2ms_Raster) 
{ 

    struct timespec start, stop; 
    uint32 startTime, stopTime; 

     if((startTime = clock_gettime(CLOCK_REALTIME, &start)) == -1) { 
      perror("clock gettime"); 

     } 

     startTime =start.tv_sec + 0.0000001 * start.tv_nsec; 
      // printf("start time is %lf", StartTime); 


     // return EXIT_SUCCESS; 

    /* Trigger DAQ for the 2ms XCP raster. */ 
    if(XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_2msRstr()) 
    { 
     ++numDaqOverload2ms; 
    } 

    /* Update those variables which are modified every 2ms. */ 
counter32 += slope32; 

    /* Trigger STIM for the 2ms XCP raster. */ 
    if(enableBypass2ms) 
    { 
     if(XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_2msRstr()) 
     { 
    ++numMissingDto2ms; 
     } 
     } 
    if((stopTime = clock_gettime(CLOCK_REALTIME, &stop)) == -1) { 
      perror("clock gettime"); 

     } 
    stopTime = stop.tv_sec + 0.0000001 * stop.tv_nsec; 
    //printf("stop time is %ld", stopTime); 

      duration2ms = (uint32)(stopTime- startTime); 
      // printf("time difference is= %ld\n", duration2ms); 



} 



void TASK3(Task100ms_Raster) 
{ 
    struct timespec start, stop; 
    uint32 startTime, stopTime; 



      if((startTime = clock_gettime(CLOCK_REALTIME, &start)) == -1) 
      { 
       perror("clock gettime"); 

      } 
      startTime =start.tv_sec + 0.0000001 * start.tv_nsec; 
      // printf("start time is %lf", startTime); 

    /* Trigger DAQ for the 100ms XCP raster. */ 
    if(XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_100msRstr()) 
    { 
     ++numDaqOverload100ms; 
    } 

    /* Update those variables which are modified every 100ms. */ 
    counter8 += slope8; 


    /* Trigger STIM for the 100ms XCP raster. */ 
    if(enableBypass100ms) 
    { 
     if(XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_100msRstr()) 
     { 
      ++numMissingDto100ms; 
     } 
    } 



    if((stopTime = clock_gettime(CLOCK_REALTIME, &stop)) == -1) { 
       perror("clock gettime"); 

      } 

    stopTime = stop.tv_sec + 0.0000001 * stop.tv_nsec; 
    // printf("stop time is %lf", stopTime); 

    Xcp_CmdProcessor(); 

    duration100ms = (stop.tv_sec - start.tv_sec) 
        + (double)(stop.tv_nsec - start.tv_nsec) 
        /(double)million; 
      // printf("time difference is= %ld\n", duration100ms); 
} 

/*The handler checks that the value stored in sival_ptr matches a given timerID 
variable. The sival_ptr is the same as the one we set in makeTimer(), 
though here it lives in a different structure. 
Obviously, it got copied from there to here on the way to this signal handler. 
The point is that the timerID is what is used to determine which timer just went off 
and determine what to do next */ 


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

/* 
The function takes a pointer to a timer_t variable that will be filled with the 
timer ID created by timer_create(). This pointer is also saved in the sival_ptr 
variable right before calling timer_create(). In this function notice that we 
always use the SIGRTMIN signal, so expiration of any timer causes this signal to 
be raised. The signal handler I've written for that signal is timerHandler. 
*/ 

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

    /* Set up signal handler. */ 
    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"); 

     } 

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


     // create a timer 

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


    } 

     close(acceptSocket); 



     return 0; 

    } 



int main() 
{ 

    Xcp_Initialize(); 
    CreateSocket(); 
    return 0; 
} 




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

저는 클라이언트 및 서버 아키텍처에서 작업하고 있습니다. 서버 코드는 위 그림과 같이 클라이언트로부터 IP 주소와 포트 번호를 통해 요청을 받기위한 소켓을 만들었습니다. 서버가 클라이언트로부터의 요청을 기다리고 클라이언트에게 응답을 보냅니다. 또한 2ms, 10ms 및 100ms마다 te 작업을 호출하는 타이머를 만들었습니다. 타이머 작업을 위해 별도의 스레드를 만들지 않았습니다. 방금 처리 할 처리기를 만들었습니다 (신호 처리기). 클라이언트가 서버에 요청을 보내면 recvfrom api에서 데이터를 수신하고 나중에 maketimer (2ms)를 호출하고 2ms 작업에서 나오지 않습니다.함수를 병렬로 실행하는 방법은 무엇입니까?

클라이언트는 서버에 데이터를 보내는 데 사용되는 도구 (INCA)입니다. 나는 리눅스 운영 체제에서 일하고있다.

누군가가 위의 프로그램에서 무엇이 잘못되었는지 말해 주시겠습니까 ??

답변

0

recvfrom()이 차단 기능이기 때문에 while (rc! = - 1) 루프에 대해 별도의 스레드를 만들어야합니다. 새로운 데이터가 올 때까지 스레드를 비활성 상태로 만듭니다.

+0

나에게 예를 들어 주시겠습니까 ?? – user3345539

+0

[pthreads] (http://en.wikipedia.org/wiki/Pthreads)를 통해 별도의 스레드에서 작업을 실행할 수 있지만 필요하지는 않습니다. 논 블로킹 I/O를 사용하는 것이 좋습니다. – Jason

관련 문제