Describes the kernel interface functions that some kernels provide to us for creating threads in the kernel.
In fact, in the Linux kernel, whether it is a process or a thread, it is represented by a struct task_struct struct.
function to create a kernel thread:
struct task_struct * kthread_create (int (*THREADFN) (void *data),
void *data,
const char namefmt[]);
THREADFN: The function entity that is going to run;
Data: Arguments passed to the function that will be run;
NAMEFMT: Is the name of the thread that was created;
Note: Threads created with the Kthread_create () interface are not run immediately, but instead are placed in the waiting queue. If you want the created thread to run immediately, use the wake_up_process (struct task_struct *task) function to wake up the created thread immediately.
Used to stop a kernel thread:
int kthread_stop (struct task_struct *kthread)
Return value: If this kthread thread is not awakened by Wake_up_process () after the creation of the Ktreadh_create () function, then the Kthread_stop () function is called to stop the thread, A-EINTR error code is returned.
Used to create a thread that runs immediately:
struct task_struct *kthread_run (int (*KTHREADFN) (void *data),
void *data,
const char namefmt[]);
Essentially, the Kthread_run () function is actually a macro definition:
#define KTHREAD_RUN (int (*KTHREADFN) (void *data), \
void *data, \
const char namefmt[]) \
({ \
struct Task_struct *kthread = kthread_create (kthread, data, namefmt[]); \
if (!is_err (kthread)) \
Wake_up_process (Kthread); \
Kthread; \
})
so the return value of the Kthread_run () function: The address of the successful return thread;
Failed to return error address;
A function to determine whether a thread currently in the kernel is still running:
int kthread_should_stop (void)
the Kthread_should_stop () function returns 0 if one of the current threads continues to run in the kernel;
If the current thread is somewhere in the kernel by using kthread_stop (struct task_struct *)
And is stopped, the Kthread_should_stop () function returns 1 immediately.
struct task_struct *kthread_create (int (*KTHREADFN) (void *data),
void *data,
const char namefmt[]);
struct task_struct *kthread_run (int (*KTHREADFN) (void *data),
void *data,
const char namefmt[]);
int kthread_stop (struct task_struct *kthread);
int kthread_should_stop (void);
The functions provided by the kernel for thread creation and thread stop are located in the <linux/kthread.h> header file;
This article is from the "FAI Aberdeen" blog, please make sure to keep this source http://weiguozhihui.blog.51cto.com/3060615/1582753
Threads in the Linux kernel create interfaces