【Linux API】kthread的使用

本文详细分析了Linux内核中的kthread使用,包括如何创建和启动内核线程,以及线程管理的相关API。通过实例展示了kthread_create和kthread_run的区别,并提到了kthread_stop和kthread_should_stop函数在管理线程过程中的作用。
摘要由CSDN通过智能技术生成

一,内核实例分析

先来分析一下一个实例,这个例子主要是创建一个内核线程,用来打印GPIO信息,当线程没有收到停止的指令时,一直打印。

int config_gpio_thread(void *tmp_gpio_num) //回调函数,返回值必须是int ,且参数必须是void *
{
         int gpio_num = *((int *)tmp_gpio_num);//类型强转,由void*转成int*,取值
         while(!kthread_should_stop()) //当kthread_should_stop()函数没有被调用的时候,就一直打印。
                {
                 gpio_direction_output(gpio_num, 0);
                 printk("the gpio get  a value of :%d:=========buqing.wang test \n",__gpio_get_value(gpio_num));
                 msleep(500);
                 gpio_direction_output(gpio_num, 2);     
                 printk("the gpio get  a value of :%d:=============buqing.wang test \n",__gpio_get_value(gpio_num));
                 msleep(500);
                }           
    return 0;
} 

//具体的操作部分
int handle_gpio_thead(int num)  
{
struct task_struct * gpio_thread;  //任务结构体
void* gpio_num;
gpio_num=#
gpio_thread=kthread_create(config_gpio_thread,&gpio_num,"gpio_thread");//第二个参数是回调函数的参数,必须是void*
wake_up_process(gpio_thread); //唤醒线程开始工作
msleep(5000);  //5s后,叫停线程
kthread_stop(gpio_thread);//这个函数调用后,kthread_should_stop()返回值由false变成true
    return 0;
} 

二,关于线程创建相关的API

创建Kthread主要有两种方法
第一种就是如上所述:
kthread_create(threadfn, data, namefmt, arg…),宏,返回一个task_struct *类型的数据址。创建线程任务,threadfn 线程的回调函数,data是参数,那么format是线程名字。
回调函数返回值必须是int,参数必须是void*
wake_up_process(gpio_thread),启动线程

第二种方法:

kthread_run(threadfn, data, namefmt, …) 直接创建线程并且启动

其他线程相关的API:
int kthread_stop(struct task_struct *k); //让线程结束
bool kthread_should_stop(void); //判断线程状态

参考文件:

kernel/include/linux/kthread.h

#ifndef _LINUX_KTHREAD_H
#define _LINUX_KTHREAD_H
/* Simple interface for creating and stopping kernel threads without mess. */
#include <linux/err.h>
#include <linux/sched.h>

__printf(4, 5)
struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
                       void *data,
                       int node,
                       const char namefmt[], ...);

#define kthread_create(threadfn, data, namefmt, arg...) \
    kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)


struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
                      void *data,
                      unsigned int cpu,
                      const char *namefmt);

/**
 * kthread_run - create and wake a thread.
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: Convenient wrapper for kthread_create() followed by
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 */
#define kthread_run(threadfn, data, namefmt, ...)              \
({                                     \
    struct task_struct *__k                        \
        = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
    if (!IS_ERR(__k))                          \
        wake_up_process(__k);                      \
    __k;                                   \
})

void kthread_bind(struct task_struct *k, unsigned int cpu);
int kthread_stop(struct task_struct *k);
bool kthread_should_stop(void);
bool kthread_should_park(void);
bool kthread_freezable_should_stop(bool *was_frozen);
void *kthread_data(struct task_struct *k);
void *probe_kthread_data(struct task_struct *k);
int kthread_park(struct task_struct *k);
void kthread_unpark(struct task_struct *k);
void kthread_parkme(void);

int kthreadd(void *unused);
extern struct task_struct *kthreadd_task;
extern int tsk_fork_get_node(struct task_struct *tsk);

/*
 * Simple work processor based on kthread.
 *
 * This provides easier
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值