2014-02-28 58 views
0
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); 
} 

我正在處理客戶端和服務器體系結構。服務器代碼如上所示,我創建了一個套接字來通過IP地址和端口號從客戶端接收請求。服務器正在等待來自客戶端的請求並將響應發送回客戶端。當它從客戶端接收數據時,它應該調用計時器任務(即我的代碼中的callBackTimers),爲此我還創建了計時器,以每2ms,10ms和100ms調用任務。如何連續運行該計時器任務?

我的問題:在調試模式 - 控制達到了maketimer函數調用,但它沒有自動運行(我沒有添加任何中斷點)。它正在停止maketimer3。如何使它不停止運行?

+0

您可能想顯示'makeTimer()'的代碼? – alk

+0

現在我添加了make timer code。 – user3354789

+0

所以現在也請顯示'timerHandler()'的代碼。 – alk

回答

1

正如我在你的previous question回答,這可能CreateSocket功能,即要做到:

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

你正在創建通過這個循環,每次定時器。你永遠不會刪除它們。您可以創建多少個定時器是有限制的。從man (2) timer_create

內核上預先分配的「排隊實時信號」()使用timer_create創建的每個定時器。因此,定時器的數量受限於RLIMIT_SIGPENDING資源限制(請參閱setrlimit(2))。

你是不是檢查timer_create返回代碼的狀態和我的猜測是,你用完了計時器,然後之後只是失敗。

(順便說一句,不知道什麼while(1);;是該做的,我知道你一定會感到沮喪,但是這正成爲一個移動目標的東西。)

+0

非常感謝。是否可以獨立運行此計時器或與從插座接收的數據平行運行? – user3354789

0

這是一個倉促的(但我認爲準確)切碎了你的代碼版本。由於你的while(1);CreateSocket它只是要創建3個計時器。如果你運行它,你會看到TASKS1/2/3運行。這本身不是你的問題。

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

照顧循環問題,看看你必須從那裏去。創建3個定時器。如果您只希望它們在每個套接字讀取時關閉一次,則要麼沒有間隔值,以便它們不會重複地關閉在第一次之後重置定時器(即,將值/間隔值置零)走吧。但無論如何,你都應該重用它們。

+0

非常感謝。是否可以獨立運行此計時器或與從插座接收的數據平行運行? – user3354789

+0

是的,但你需要說明你真正想做什麼。在這種情況下,獨立和平行是相互排斥的。您是否想要讀取套接字來設置定時器,或者只是讓它們設置爲關閉而不管讀何者? – Duck

+0

我需要你的幫助,並在這一點卡住非常嚴重。從客戶端收到數據(這裏是一些命令的數據)之後 - 我必須創建一個定時器來每100ms處理一次該命令。 – user3354789