2014-02-25 68 views
0
#include <features.h> 
#include <time.h> 
#include <sys/time.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <signal.h> 
#include <unistd.h> 
#include <errno.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 

#include <netinet/in.h> 
#include <arpa/inet.h> 






#define million 1000000L 

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

struct sockaddr_in addr, client; 
int acceptSocket; 
char buf[1024]; 
char a[128]; 
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.00000001 * 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.00000001 * stop.tv_nsec; 
    //printf("stop time is %ld", stopTime); 

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



} 

void TASK2(Task10ms_Raster) 
{ 
    struct timespec start, stop; 

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

      } 

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


    /* Trigger DAQ for the 10ms XCP raster. */ 
    if(XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_10msRstr()) 
    { 
     ++numDaqOverload10ms; 
    } 

    /* Update those variables which are modified every 10ms. */ 
    counter16 += slope16; 

    /* Trigger STIM for the 10ms XCP raster. */ 
    if(enableBypass10ms) 
    { 
     if(XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_10msRstr()) 
     { 
      ++numMissingDto10ms; 
     } 
    } 

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

      } 
    Xcp_CmdProcessor(); 

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


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


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.00000001 * 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.00000001 * stop.tv_nsec; 
    // printf("stop time is %lf", stopTime); 


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



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) 
{ 
    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, sizeof(buf), 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 
      } 


    } 

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

我正在處理客戶端和服務器體系結構。我的代碼工作正常,服務器正在等待來自客戶端的請求,當客戶端向服務器發送請求時,我在調試模式下收到以下消息。 沒有來源可用於「__kernel_vsyscall()at 0xb779e424」沒有來源可用於「__kernel_vsyscall()at 0xb779e424」?

我正在使用Linux操作系統。

+0

你重寫了一些局部變量(主要的),摧毀了堆棧,這會讓你回到正確的操作系統。仔細查看任何你有的數組。 –

回答

1

可能是你的代碼中某處存在內存損壞(可能是緩衝區溢出)。

當你使用3級優化進行編譯時,編譯器輸出的代碼是這樣的:緩衝區溢出寫入重要的東西(可能會損壞堆棧?),而且碰巧編譯器在沒有運行時編譯器產生的非優化代碼優化標誌是不同的,所以溢出運行其他的東西,並不會導致這種特定的症狀。這個錯誤可能還在那裏,它可能以其他方式表現出來,甚至根本不會 - 直到你改變了一些與非相關的東西,然後再次咬你。

__kernel_vsyscall()事情就是一個glibc函數,只要你進行系統調用,它就是在內部調用的。那裏沒有什麼重要的。

請在valgrind下運行你的程序。它很可能會找到你的內存溢出。

+0

我使用兩個全局聲明的內存作爲char buf [1024]; char a [128];月食中valgrind的選項在哪裏?我試着編譯優化級別爲2,然後也是相同的錯誤。 – user3345539

+0

@ user3345539好的,你可以使用'char buf [1024]; char a [128];'你的代碼中的變量,所以我可以嘗試找出或發佈一些客戶端/服務器的代碼。 –

+0

buf用於保存來自客戶端的數據。記憶a在我的目標中被用作ecu記憶。 – user3345539

相關問題