【发布时间】:2021-09-11 14:08:51
【问题描述】:
static struct task_struct *control = NULL;
static long call_ioctl(struct file *filp, unsigned int iocmd, unsigned long arg)
{
switch (iocmd)
{
case SETMODE:
/* get buffer data from userspace which is either 0 or 1 */
/* 0: Manual 1: Automatic*/
switch (buffer)
{
case MANUAL:
if (control)
{
kthread_stop(control);
control = NULL;
printk(KERN_ALERT "Switching to MANUAL disabling kernel thread for Automatic\n");
}
mode = MANUAL_MODE;
printk(KERN_ALERT "IN MANUAL \n");
break;
case AUTOMATIC:
if (automatic_fan_control() != 0)
{
printk(KERN_ALERT "Failed to set fan to AUTOMATIC!!! \n");
return -1;
}
break;
default:
printk(KERN_ALERT "Entered value is incorrect\n");
return -EINVAL;
}
break;
}
}
static inline int automatic_fan_control(void)
{
control = kthread_run(sense_cpu_temperature, NULL, "Temperature Thread");
if (control)
{
printk(KERN_ALERT "Kthread Created Successfully\n");
}
else
{
printk(KERN_ALERT "Failed to create kthread. \n");
control = NULL;
return -1;
}
return 0;
}
int temperature(void *arg)
{
while (!kthread_should_stop())
{
mutex_lock(&mutex);
get_temperature();
mutex_unlock(&mutex);
/* Process the temperature value */
msleep_interruptible(polling_interval);
}
return 0;
}
这是我为驱动程序编写的上述代码:
- 当我切换到自动模式时,会创建线程来获取温度并对其进行处理,一旦完成,它将休眠 polling_interval 时间,然后在时间过去后再次处理。
- 当我切换到手动模式时,必须停止 kthread 并进入不是线程的手动模式。
- 我这里面临的问题是切换到手动模式时,它会在kthread的睡眠时间完成后响应,如果提供的睡眠时间间隔很大,切换到手动模式需要很长时间。
- kthread 切换到手动模式时,有什么方法可以从睡眠中退出。有点卡在这个新手驾驶中,任何帮助都会很棒
【问题讨论】:
-
旁白:
automatic_fan_control需要处理已经自动的风扇控制,以避免不必要地创建另一个线程。否则代码会泄漏线程。 -
@IanAbbott:谢谢,会处理这个问题,关于如何从睡眠中唤醒线程的任何输入或参考?
标签: c linux-kernel kernel linux-device-driver