C中每分钟的计数

时间:2015-07-04 16:42:43

标签: mysql c

我已经构建了一个Geiger-Mueller计数器,其输出连接在计算机并行物理端口#3上。 我有一个循环,检查该端口上的事件。在2ms的时间范围内可能有2-4个脉冲。 在此行之后printf("Radiatie: %.4f uSv\n",(float)sievert); 我想以某种方式每隔60秒执行一次mysql插入查询......但是查询执行时间不应该干扰for循环。 欢迎任何帮助,谢谢!

#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>
#include <time.h>

#define BASEPORT 0x378 /* lp1 */
static int  var = 0;
static int C = 1;
static int CPM; /* pulse count */
static float sievert;

int main ()
{
    time_t start_t, end_t; double diff_t;

    if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
    outb(0, BASEPORT);
    time(&start_t);

    for( ; ; )
    {
        /* Read from port */
        var = inb(BASEPORT +1);
        if (var == 56)
        {
            /* 56 is the value of HIGH (pulse) */
            CPM = C++;
            printf("%d\n",CPM );
        }
        time(&end_t);
        diff_t = difftime(end_t, start_t);
        int b = (int) diff_t;
        if (b == 60)
        {
            sievert = CPM * 0.0058; /* converting Counts per Minute in uSievert */
            C = 1;
            printf("Radiatie: %.4f uSv\n",(float)sievert);
            time(&start_t);
        }
        /* end for */ 
    }
}

1 个答案:

答案 0 :(得分:1)

您应该使用多线程环境,以便能够同时计算和运行mysql查询。您可以使用fork()pipe()轻松完成此操作。

int comm[2];
pipe(comm);

if (pid) { // In parent process
    int count;
    int reading_loc = 0, current_read;
    while (current_read = read(comm[0], &count + reading_loc, sizeof(int) - reading_loc)) {
        reading_loc += current_read;
        if (reading_loc == sizeof(int)) {
            reading_loc = 0;
            // GOT FULL VALUE IN COUNT.
            // Run mysql query
        }
    }
} else { // In child process
    while (true) {
        // Do collection stuff
        int value_to_send;
        if (should_run_query) {
            int current_write, writing_loc = 0;
            while (current_write = write(comm[1], &value_to_send + writing_loc, sizeof(int) - writing_loc)) {
                if (current_write == -1) {
                    // Error in the writing process. Be sad :(
                }
                writing_loc += current_write;
            }
        }
    }
}

这是分叉后如何在父管和子管之间进行通信的基本概要。您的流程应由系统委派为并行运行。