gpt4 book ai didi

linux-kernel - POSIX:如何在线程之间执行上下文切换?

转载 作者:行者123 更新时间:2023-12-02 03:21:13 25 4
gpt4 key购买 nike

我想使用 Xilkernel 实现线程之间的上下文切换,但是没有原始的 POSIX 兼容允许停止然后恢复线程的执行。

有没有人可以帮助我?

最佳答案

我使用此 C 代码为 FPGA 进行上下文切换。如果您觉得它很有用并且想要获得更多周围的代码,请询问我。

/*
* threadswitch - change thread
*
* The thread stack-pointer is supplied as a parameter.
* The old thread's stack-pointer value is saved to the array
* thread_info_array, and a new thread is selected from the array.
* The stack pointer of the new thread is returned.
*/
unsigned int * threadswitch( unsigned int * old_sp )
{
unsigned int * new_sp;

number_of_thread_switches += 1; /* Increase thread-switch counter. */

/* Print line 1 of an informational message. */
printf( "\nPerforming thread-switch number %d. The system has been running for %d ticks.\n",
number_of_thread_switches,
get_internal_globaltime() );

/* Save the stack pointer of the old thread. */
thread_info_array[ currently_running_thread ].thread_sp = old_sp;

/* Print part 1 of a message saying which threads are involved this time. */
printf( "Switching from thread-ID %d ",
thread_info_array[ currently_running_thread ].thread_id );

/* Perform the scheduling decision (round-robin). */
currently_running_thread += 1;
if( currently_running_thread >= current_thread_count )
{
currently_running_thread = 0;
}

/* Print part 2 of the informational message. */
printf( "to thread-ID %d.\n",
thread_info_array[ currently_running_thread ].thread_id );

/* Get the stack pointer of the new thread. */
new_sp = thread_info_array[ currently_running_thread ].thread_sp;

/* Return. */
return( new_sp );
}

关于linux-kernel - POSIX:如何在线程之间执行上下文切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com