In the post kernel thread cration -1 the basics of thread creation in the kernel was introduced. In continuation to the same, let us look at another function that will help us create threads in fewer lines than given that post.
To create and run a thread we had used the following two functions
kthread_create to create the thread
wake_up_process to run the thread.
Instead of the two functions we can achieve the same using just one function
kthread_run( "function name" , "data for the function" , "name of the thread" )
So instead of calling the two functions we can just call the kthread_run to create as well as run the thread. this useful when you want the thread to start executing immediately.
The modified init function will look as follows, the rest of the code remains the same as given in the post kthread creation -1
########################### init function ##########
int thread_init (void) {
char name[8]="thread1";
printk(KERN_INFO "in init");
thread1 = kthread_run(thread_fn,NULL,name);
return 0;
}
###########################################
The above init function is obviously much shorter than the previous one thus helping in reducing the code size.
To create and run a thread we had used the following two functions
kthread_create to create the thread
wake_up_process to run the thread.
Instead of the two functions we can achieve the same using just one function
kthread_run( "function name" , "data for the function" , "name of the thread" )
So instead of calling the two functions we can just call the kthread_run to create as well as run the thread. this useful when you want the thread to start executing immediately.
The modified init function will look as follows, the rest of the code remains the same as given in the post kthread creation -1
########################### init function ##########
int thread_init (void) {
char name[8]="thread1";
printk(KERN_INFO "in init");
thread1 = kthread_run(thread_fn,NULL,name);
return 0;
}
###########################################
The above init function is obviously much shorter than the previous one thus helping in reducing the code size.