在 contiki 中等待事件时执行转移到第二个进程

Execution is transferred to second process while waiting for an event in contiki

我在Contiki 中创建了两个进程。在第一个过程中,我想在一定时间后继续调用一个函数,为此我设置了一个事件计时器。但是在等待定时器到期的事件时,执行控制转移到第二个进程。

代码:

PROCESS(hello_world_process, "Hello world process");
PROCESS(hello_world_process2, "Hello world process2");
AUTOSTART_PROCESSES(&hello_world_process,&hello_world_process2);
#define TIMEOUT              (CLOCK_SECOND / 4)
static struct etimer timer;

void setFlag(void)
{
    int i;
    i = random_rand();
    if (i>0 && i<32767)
    {
        printf("Setting flag\n");
        flag =1;
    } 

}
PROCESS_THREAD(hello_world_process, ev, data)
{
    PROCESS_BEGIN();
    printf("1st process started\n");
    do 
    {
        etimer_set(&timer, TIMEOUT);
        PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
        setFlag();
    } while(!flag);
    printf("1st process completed\n");
    PROCESS_END();
}
PROCESS_THREAD(hello_world_process2, ev, data)
{
    printf("2nd process started\n");
    PROCESS_BEGIN();
    printf("2nd process completed\n");
    PROCESS_END();
}

输出:

Contiki-list-1532-g2ca33d4 started with IPV6, RPL
Rime started with address 1.2.3.4.5.6.7.8
MAC nullmac RDC nullrdc NETWORK sicslowpan
Tentative link-local IPv6 address fe80:0000:0000:0000:0302:0304:0506:0708
1st process started
2nd process started
2nd process completed
Setting flag
1st process completed

我希望只有在第一个进程完全执行后才执行第二个进程,即在等待事件时,不应将控制权转移到第二个进程。

试试这个:

AUTOSTART_PROCESSES(&hello_world_process);

// ...

PROCESS_THREAD(hello_world_process, ev, data)
{
    PROCESS_BEGIN();
    printf("1st process started\n");
    do 
    {
        etimer_set(&timer, TIMEOUT);
        PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
        setFlag();
    } while(!flag);
    printf("1st process completed\n");
    process_start(&hello_world_process2, NULL); // start the second process
    PROCESS_END();
} 

参见https://github.com/contiki-os/contiki/wiki/Processes

此外,不要在PROCESS_BEGIN()之前放置任何可执行代码!您不会收到警告,但结果可能不是您所期望的,因为每次恢复进程时都会执行该代码。