Linux 内核线程

发布于 2024-10-22 21:10:22 字数 2180 浏览 2 评论 0原文

所以我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-10-29 21:10:22
 for (i = train.stop ;i != Five_back; i = (i - 1) % 10)
 {
    int count = robots_count(train.waiting[i]);

当 i == 1 时,在 2 次迭代后,您将有 Five_back 负值,并且有效 robots_count(train.waiting[-1);

 for (i = train.stop ;i != five_back; i = (i - 1) % 10)
 {
    int count = robots_count(train.waiting[i]);

When i == 1, you have five_back negative and effectively robots_count(train.waiting[-1); after 2 iterations.

鸢与 2024-10-29 21:10:22

一旦您解决了 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 consider schedule_timeout() or msleep. Take a look in Linux Device Drivers for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文