Creating threads in linux

Creating Linux Kernel Threads

It’s been quite some time since I touched linux kernel modules, right now I’m revisiting the concepts once more. In this post I’m writing down the bullet points for creating the kernel threads.

To create a linux kernel thread, we need to:

  • Include: linux/kthread.h
  • Include: linux/sched.h
  • Create: struct task_struct * this structure is defined in linux/sched.h .
  • Create a function that you need to execute from the thread: The signature of that function needs to be int function_name (void *data)
  • Create a kthread using : kthread_create from linux/kthread.h .
  • Move the thread to the set of runnable processes using wake_up_process .
  • Stop the thread before exiting the module using kthread_stop .

Putting everything together,

Following is the (same old 😅) example program to create multiple threads from a kernel module.

#include // included for all kernel modules #include // included for __init and __exit macros #include // included for threading related functions #include // included to create tesk_struct #include // included for the sleep/delay function in the thread // array for task_struct to hold task info static struct task_struct kth_arr[4]; // long running function to be executed inside a thread, this will run for 30 secs. int thread_function(void * idx)  unsigned int i = 0; int t_id = * (int * ) idx; // kthread_should_stop call is important. while (!kthread_should_stop())  printk(KERN_INFO "Thread %d Still running. %d secs\n", t_id, i); i++; if (i == 30) break; msleep(1000); > printk(KERN_INFO "thread %d stopped\n", t_id); return 0; > // initialize one thread at a time. int initialize_thread(struct task_struct * kth, int idx)  char th_name[20]; sprintf(th_name, "kthread_%d", idx); kth = kthread_create(thread_function, & idx, (const char * ) th_name); if (kth != NULL)  wake_up_process(kth); printk(KERN_INFO "%s is running\n", th_name); > else  printk(KERN_INFO "kthread %s could not be created\n", th_name); return -1; > return 0; > // module init function static int __init mod_init(void)  int i = 0; printk(KERN_INFO "Initializing thread module\n"); for (i = 0; i  4; i++)  // initialize one thread at a time. if (initialize_thread( & kth_arr[i], i) == -1)  return -1; > > printk(KERN_INFO "all of the threads are running\n"); return 0; > // module exit function. static void __exit mod_exit(void)  int i = 0; int ret = 0; printk(KERN_INFO "exiting thread module\n"); for (i = 0; i  4; i++)  // stop all of the threads before removing the module. ret = kthread_stop( & kth_arr[i]); if (!ret)  printk("can't stop thread %d", i); > > printk(KERN_INFO "stopped all of the threads\n"); > MODULE_LICENSE("GPL"); MODULE_AUTHOR("GIRISH JOSHI"); MODULE_DESCRIPTION("KTHREAD demo"); module_init(mod_init); module_exit(mod_exit);

and of course the makefile looks like

obj-m+=kthread_demo.o all: make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean 

Источник

How to Create Threads in Linux (With a C Example Program)

In the part I of the Linux Threads series, we discussed various aspects related to threads in Linux. In this article we will focus on how a thread is created and identified. We will also present a working C program example that will explain how to do basic threaded programming.

Linux Threads Series: part 1, part 2 (this article), part 3.

Thread Identification

  • A process ID is unique across the system where as a thread ID is unique only in context of a single process.
  • A process ID is an integer value but the thread ID is not necessarily an integer value. It could well be a structure
  • A process ID can be printed very easily while a thread ID is not easy to print.

The above points give an idea about the difference between a process ID and thread ID.

Thread ID is represented by the type ‘pthread_t’. As we already discussed that in most of the cases this type is a structure, so there has to be a function that can compare two thread IDs.

#include int pthread_equal(pthread_t tid1, pthread_t tid2);

So as you can see that the above function takes two thread IDs and returns nonzero value if both the thread IDs are equal or else it returns zero.

Another case may arise when a thread would want to know its own thread ID. For this case the following function provides the desired service.

#include pthread_t pthread_self(void);

So we see that the function ‘pthread_self()’ is used by a thread for printing its own thread ID.

Now, one would ask about the case where the above two function would be required. Suppose there is a case where a link list contains data for different threads. Every node in the list contains a thread ID and the corresponding data. Now whenever a thread tries to fetch its data from linked list, it first gets its own ID by calling ‘pthread_self()’ and then it calls the ‘pthread_equal()’ on every node to see if the node contains data for it or not.

An example of the generic case discussed above would be the one in which a master thread gets the jobs to be processed and then it pushes them into a link list. Now individual worker threads parse the linked list and extract the job assigned to them.

Thread Creation

Normally when a program starts up and becomes a process, it starts with a default thread. So we can say that every process has at least one thread of control. A process can create extra threads using the following function :

#include int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg)

The above function requires four arguments, lets first discuss a bit on them :

  • The first argument is a pthread_t type address. Once the function is called successfully, the variable whose address is passed as first argument will hold the thread ID of the newly created thread.
  • The second argument may contain certain attributes which we want the new thread to contain. It could be priority etc.
  • The third argument is a function pointer. This is something to keep in mind that each thread starts with a function and that functions address is passed here as the third argument so that the kernel knows which function to start the thread from.
  • As the function (whose address is passed in the third argument above) may accept some arguments also so we can pass these arguments in form of a pointer to a void type. Now, why a void type was chosen? This was because if a function accepts more than one argument then this pointer could be a pointer to a structure that may contain these arguments.

A Practical Thread Example

Following is the example code where we tried to use all the three functions discussed above.

#include #include #include #include #include pthread_t tid[2]; void* doSomeThing(void *arg) < unsigned long i = 0; pthread_t if(pthread_equal(id,tid[0])) < printf("\n First thread processing\n"); >else < printf("\n Second thread processing\n"); >for(i=0; i <(0xFFFFFFFF);i++); return NULL; >int main(void) < int i = 0; int err; while(i < 2) < err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); if (err != 0) printf("\ncan't create thread :[%s]", strerror(err)); else printf("\n Thread created successfully\n"); i++; >sleep(5); return 0; >

So what this code does is :

  • It uses the pthread_create() function to create two threads
  • The starting function for both the threads is kept same.
  • Inside the function ‘doSomeThing()’, the thread uses pthread_self() and pthread_equal() functions to identify whether the executing thread is the first one or the second one as created.
  • Also, Inside the same function ‘doSomeThing()’ a for loop is run so as to simulate some time consuming work.

Now, when the above code is run, following was the output :

$ ./threads Thread created successfully First thread processing Thread created successfully Second thread processing

As seen in the output, first thread is created and it starts processing, then the second thread is created and then it starts processing. Well one point to be noted here is that the order of execution of threads is not always fixed. It depends on the OS scheduling algorithm.

Note: The whole explanation in this article is done on Posix threads. As can be comprehended from the type, the pthread_t type stands for POSIX threads. If an application wants to test whether POSIX threads are supported or not, then the application can use the macro _POSIX_THREADS for compile time test. To compile a code containing calls to posix APIs, please use the compile option ‘-pthread’.

If you enjoyed this article, you might also like..

Comments on this entry are closed.

Hello, Great article but missing a few things. I noticed you are return NULL from your worker/background thread. Should be pthread_exit(NULL) NULL since you are not returning any arguments. This should clean up a few things before the thread exits. Also you have a sleep() function at the end of main. I guess if the thread had a long task that takes more than 5 seconds then main would finish and as the main thread created the worker thread that would get killed as well, which could result it UB and cause you program to crash. You should just pthread_join(thread_id) for main to wait for the worker thread to finish. Or you can use pthread_exit(NULL) at the end of main. So that the main thread would finish without killing the worker thread. The worker thread would finish without any problem. However, I would recommend pthread_join() so you can exit your program in a controlled way. Hope this helps,

@steve
Hi Steve, thanks for you valuable comments. The two points that you brought into notice will be covered in the next part of the ongoing series of Linux threads. I intentionally did not focus on ‘pthread_exit()’ and the ‘sleep()’ function in this part as this part was intended to just give a small overview of how threads come alive. The subsequent parts will peel of the layers and will focus on deeper aspects including ‘pthread_exit()’ and why sleep() should not be used etc..

Also for clarity, it should be added to link against library -lpthread for the example given to build correctly.

Another Example that might help #include
#include
#include void * func(void * param) printf(“Inside Thread function\n”);
char ** var = (char **)param;
printf(“The passed argument is \”%s\”\n”, *var);
int * i;
i=(int *)malloc(4);
*i=100;
pthread_exit((void*)i);
> int main(int argc, char * argv[]) < //Argument to be passed as command line argument
int * retval;
retval = (int *)malloc(4);
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,func,&argv[1]);
pthread_join(tid, (void **)&retval);
printf(“retval is %d\n”, *retval);
>

Hi, you mentioned that thread ID can be a structure. And you also mentioned that thread IDs can be compared. In C, can structures be compared? Isn’t it a syntax error if you try to compare two structures? Thank you,
Dominic Pritham

hello sir,
i have a doubt in my program
HOW TO KILL REMAINING THREADS WHEN WE COME OUT OF ANY SINGLE THREAD ?
I searched for this but no result! i think u have great knowledge in this area could u help me to solve this

Hello,
Great write-up.
I was a bit confused because I believe: void *(*start_rtn)(void) in: int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg) may be incorrect because the definition for doSomeThing wouldn’t match up. void* doSomeThing(void *arg) In fact in pthread.h the definition is: extern int pthread_create (pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3)); Austin

When I try to run above code in DEVC++ compiler it produces following errors…
1)- [Linker error] undefined reference to `_imp__pthread_self’
2)- [Linker error] undefined reference to `_imp__pthread_equal’
3)- [Linker error] undefined reference to `_imp__pthread_create’
4)- ld returned 1 exit status

Источник

Читайте также:  Linux автоматическое подключение принтеров
Оцените статью
Adblock
detector