Saturday, 21 July 2018

Kernel Thread

Process

An executing instance of a program is called a process. Some operating systems use the term ‘task‘ to refer to a program that is being executed.  Process is a heavy weight process. Context switch between the process is time consuming.

Threads

thread is an independent flow of control that operates within the same address space as other independent flows of control within a process.
Kernel Thread
One process can have multiple threads, with each thread executing different code concurrently.
Some of the advantages of the thread, is that since all the threads within the processes share the same address space, the communication between the threads is far easier and less time consuming as compared to processes. This approach has one disadvantage also. It leads to several concurrency issues and require the synchronization mechanisms to handle the same.

Types of Thread

There are two types of thread.
  1. User Level Thread
  2. Kernel Level Thread

User Level Thread

In this type, kernel is not aware of these threads. Everything is maintained by the user thread library. That thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. So all will be in User Space.

Kernel Level Thread

Kernel level threads are managed by the OS, therefore, thread operations are implemented in the kernel code. There is no thread management code in the application area.

kthread_create

create a kthread.
struct task_struct * kthread_create (int (* threadfn(void *data),
                           void *data, const char namefmt[], ...);
Where,
threadfn – the function to run until signal_pending(current).
data – data ptr for threadfn.
namefmt[] – printf-style name for the thread.
... – variable arguments

Start Kernel Thread

wake_up_process

This is used to Wake up a specific process.

int wake_up_process (struct task_struct * p);
Where,
p – The process to be woken up.
Attempt to wake up the nominated process and move it to the set of runnable processes.
It returns 1 if the process was woken up, 0 if it was already running.

Stop Kernel Thread

kthread_stop

It stops a thread created by kthread_create.

int kthread_stop ( struct task_struct *k);
Where,
k – thread created by kthread_create.

Other functions in Kernel Thread

kthread_should_stop

should this kthread return now?
int kthread_should_stop (void);
When someone calls kthread_stop on your kthread, it will be woken and this will return true. You should then return, and your return value will be passed through to kthread_stop.

kthread_run

This is used to create and wake a thread.
kthread_run (threadfn, data, namefmt, ...);
Where,
threadfn – the function to run until signal_pending(current).
data – data ptr for threadfn.
namefmt – printf-style name for the thread.
... – variable arguments
Example:
static struct task_struct *etx_thread
int thread_function(void *pv)
{
    int i=0;
    while(!kthread_should_stop()) {
        printk(KERN_INFO "In EmbeTronicX Thread Function %d\n", i++);
        msleep(1000);
    }
    return 0;
}

etx_thread = kthread_create(thread_function,NULL,"eTx Thread");
        if(etx_thread) {
            wake_up_process(etx_thread);
        } else {
            printk(KERN_ERR "Cannot create kthread\n");
            goto r_device;
        }

No comments:

Post a Comment