Linux 内核线程
所以我正在开发一个 Linux 内核模块,它需要在由 kthread 启动的单独线程中进行无限等待循环。以下是kthread运行的函数,train.queue_mutex是在kthread之前初始化的互斥体(通过mutex_init(&train.queue_mutex))
我一生都无法弄清楚它为什么锁定内核。我的想法是,在 mdelay 和 Schedule() 每次迭代之间,其他进程应该获得 CPU 时间。
int train_thread(void *param)
{
while (!train.is_deactivating)
{
mutex_lock_interruptible(&train.queue_mutex);
while (train.num_waiting > 0)
{
int five_foward = (train.stop + 5) % 10;
int five_back = (train.stop - 5) % 10;
int i = train.stop;
int max_count = 0;
int max_count_index = 0;
for (;i != five_foward; i = (i + 1) % 10)
{
int count = robots_count(train.waiting[i]);
if (count > max_count)
{
max_count_index = i;
max_count = count;
}
}
for (i = train.stop ;i != five_back; i = (i - 1) % 10)
{
int count = robots_count(train.waiting[i]);
if (count > max_count)
{
max_count_index = i;
max_count = count;
}
}
// Should have max_count_index set to index of stop with the most bots
printk("Most bots %d at stop %d\n", max_count, max_count_index);
mutex_unlock(&train.queue_mutex);
schedule();
mutex_lock_interruptible(&train.queue_mutex);
}
mutex_unlock(&train.queue_mutex);
mdelay(10);
}
train.is_active = 0;
return 0;
}
So I'm working on a linux Kernel module which requires an infinite waiting loop in a separate thread initiated by kthread. The following is the function run by kthread, and train.queue_mutex is a mutex initialized before kthread (via mutex_init(&train.queue_mutex))
I cannot for the life of me figure out why it locks the kernel. My thinking is that between the mdelay and the schedule() every iteration, other processes should get CPU time.
int train_thread(void *param)
{
while (!train.is_deactivating)
{
mutex_lock_interruptible(&train.queue_mutex);
while (train.num_waiting > 0)
{
int five_foward = (train.stop + 5) % 10;
int five_back = (train.stop - 5) % 10;
int i = train.stop;
int max_count = 0;
int max_count_index = 0;
for (;i != five_foward; i = (i + 1) % 10)
{
int count = robots_count(train.waiting[i]);
if (count > max_count)
{
max_count_index = i;
max_count = count;
}
}
for (i = train.stop ;i != five_back; i = (i - 1) % 10)
{
int count = robots_count(train.waiting[i]);
if (count > max_count)
{
max_count_index = i;
max_count = count;
}
}
// Should have max_count_index set to index of stop with the most bots
printk("Most bots %d at stop %d\n", max_count, max_count_index);
mutex_unlock(&train.queue_mutex);
schedule();
mutex_lock_interruptible(&train.queue_mutex);
}
mutex_unlock(&train.queue_mutex);
mdelay(10);
}
train.is_active = 0;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 i == 1 时,在 2 次迭代后,您将有 Five_back 负值,并且有效
robots_count(train.waiting[-1);
。When i == 1, you have five_back negative and effectively
robots_count(train.waiting[-1);
after 2 iterations.一旦您解决了 bestsss 指出的逻辑问题,您仍然会遇到
mdelay()
是忙等待的问题,它不会释放 CPU。您应该考虑schedule_timeout()
或msleep
。有关更多详细信息,请参阅 Linux 设备驱动程序。Once you've fixed the logic problem pointed out by bestsss you still have the problem that
mdelay()
is a busy-wait, which does not release the CPU. You should considerschedule_timeout()
ormsleep
. Take a look in Linux Device Drivers for more details.