2012-07-25 19 views
2

的功能我用下面的C代码(Linux操作系统Ubuntu),以每5分钟采样代理服务器,并得到了买入价和卖出价值:ç-language停靠在指定的时间

int main(int argc, char *argv[]) 
{ 
struct stock myStock; 
struct stock *myStock_ptr; 
struct timeval t; 
time_t timeNow; 


strcpy(myStock.exchange,"MI"); 
strcpy(myStock.market,"EQCON"); 
strcpy(myStock.t3Id,"1"); 
strcpy(myStock.subscLabel,""); 
strcpy(myStock.status,"0"); 
strcpy(myStock.ask,""); 
strcpy(myStock.bid,""); 

buildSubLabel(&myStock); 

while (1) { 
    t.tv_sec = 1; 
    t.tv_usec = 0; 

    select(0, NULL, NULL, NULL, &t); 
    time(&timeNow); 

    sample(&myStock); 

    printf("DataLink on %s\n",myStock.subscLabel); 
    printf("Time Now: --- %s",ctime(&timeNow)); 
    printf("DataLink Status---- %s\n",myStock.status); 
    printf("Ask --- %s\n",myStock.ask); 
    printf("Bid --- %s\n",myStock.bid); 
    printf("###################\n"); 

} 

return 0; 
} 

我”什么m不能做的是在特定时间安排样本功能。 我想打电话样本函数在 9.01首次 9.05第二时间 9.10第三时间 9.15 ...... 9.20 ...... ,以此类推,直到17.30 后17.30这个过程应该终止。

问候 马西莫

+0

我不确定你的最终目标,但更容易做的事情是运行cron作业。 Cron只是一个安排程序在特定时间运行的工具。您可以设置它在一天中的特定时段每五分钟运行一次程序。请参阅https://help.ubuntu.com/community/CronHowto。 – bchurchill 2012-07-25 16:57:36

回答

2

您应该使用一个线程来调用你想要的功能的具体时间之后。
做这样的事情:

#include <pthread.h> 
#include <unistd.h> // for sleep() and usleep() 

void *thread(void *arg) { // arguments not used in this case 
    sleep(9); // wait 9 seconds 
    usleep(10000) // wait 10000 microseconds (1000000s are 1 second) 
    // thread has sleeped for 9.01 seconds 
    function(); // call your function 
    // add more here 
    return NULL; 
} 

int main() { 
    pthread_t pt; 
    pthread_create(&pt, NULL, thread, NULL); 
    // thread is running and will call function() after 9.01 seconds 
} 

你可以(通过检查你的程序运行的时间)代码的线程功能的另一种方式:

void *thread(void *arg) { 
    while ((clock()/(double)CLOCKS_PER_SEC) < 9.01) // check the running time while it's less than 9.01 seconds 
     ; 
    function(); 
    // etc... 
    return NULL; 
} 

记住:你必须pthread库链接!如果你使用gcc,这将是-lpthread

有关并行线程的详细信息(POSIX线程),你可以看看这个网站:
https://computing.llnl.gov/tutorials/pthreads/
并且在时钟功能:
http://www.cplusplus.com/reference/clibrary/ctime/clock/

+0

非常感谢,玉米芯是我想知道的最后一次机会,如果使用某些Linux系统调用可能实现Excel/VBA onTime函数。 – msalese 2012-07-25 17:07:46

+0

已更新我的回答:线程函数的变体与运行时间。 – qwertz 2012-07-25 17:27:30

0

当你这样做的处理(后即printf东西)您需要计算延迟,因为处理需要时间。您可以在17:30或更晚时间结束循环。

如果你不减少延迟,那么你不会在一天中的正确时间得到样本。

+0

所以我应该启动程序并计算时间(&timeNow)和上午9点01分之间在秒(或毫秒)中的差异。这样我可以更新t.tv_sec(或t.tv_usec)等等 – msalese 2012-07-25 17:12:48