質問

void print_task(void)
{
    for(;;)
    {
        taskLock();
        printf("this is task %d\n", taskIdSelf());
        taskUnlock();
        taskDelay(0);
    }
}
void print_test(void)
{
    taskSpawn("t1", 100,0,0x10000, (FUNCPTR)print_task, 0,0,0,0,0,0,0,0,0,0);
    taskSpawn("t2", 100,0,0x10000, (FUNCPTR)print_task, 0,0,0,0,0,0,0,0,0,0);
}

the above code show:

this is task this is task126738208 126672144 this is task this is task 126712667214438208

this is task this is task 1266721441 26738208 this is task 126672144 this is task

what is the right way to print a string in multitask?

役に立ちましたか?

解決

The problem lies in taskLock();

Try semaphore or mutex instead.

他のヒント

The main idea to print in multi-threaded environment is using dedicated task that printout. Normally in vxWorks there is a log task that gets the log messages from all tasks in the system and print to terminal from one task only. The main problem in vxWorks logger mechanism is that the logger task use very high priority and can change your system timing.

Therefore, you should create your own low priority task that get messages from other tasks (using message queue, shared memory protected by mutex, …). In that case there are 2 great benefits: The first one, all system printout will be printed from one single task.

The second, and most important benefit, the real-time tasks in the system should not loss time using printf() function. As you know, printf is very slow function that use system calls and for sure change the timing of your tasks according the debug information you add.

taskLock, taskLock use as a command to the kernel, it mean to leave the current running task in the CPU as READY.

As you wrote in the example code taskUnlock() function doesn't have arguments. The basic reason is to enable the kernel & interrupts to perform taskUnlock in the system.

There are many system calls that perform task unlock (and sometimes interrupts service routing do it also)

Rather than invent a home-brew solution, just use logMsg(). It is the canonical safe & sane way to print stuff. Internally, it pushes your message onto a message queue. Then a separate task pulls stuff off the queue and prints it. By using logMsg(), you gain ability to print from ISR's, don't have interleaved prints from multiple tasks printing simultaneously, and so on.

For example:

printf("this is task %d\n", taskIdSelf());

becomes

logMsg("this is task %d\n", taskIdSelf(), 0,0,0,0,0,0);

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top