# How to use kernel_threads on KLD



## victormenegusso (Aug 8, 2011)

Hello everybody, I'm finishing a degree in computer science :stud, and my final project is a module in freebsd, is the first time I'm working with KLD on freebsd and I need help.
I'm using an example of kld, and it would like to know how to use kernel_threads, how to put the thread to run on the following function kt_log_rw_arq, when KLD load;


```
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/kthread.h>

static void kt_log_rw_arq(void* data)
{
	printf("In thread\n");

}
static int load(struct module *module, int cmd, void *arg)
{
	int error=0;

	switch(cmd)
	{
		case MOD_LOAD:
			uprintf("KT_LOG Carregada\n");
			//kthread_create(kt_log_rw_arq,NULL)
			kt_log_rw_arq(NULL)
			break;

		case MOD_UNLOAD:
			uprintf("KT_LOG Descarregada\n");
			break;

		default:
			error=EOPNOTSUPP;
			break;
	}
	return (error);
}

static moduledata_t kt_log_module_data = 
{
    	"kt_log",    // nome do modulo //
    	load,       // Event Handler //
    	NULL        // Extra Data //
};

DECLARE_MODULE(kt_log, kt_log_module_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
```


----------



## victormenegusso (Aug 8, 2011)

I try use 

```
static struct proc *mainproc;
```


```
kproc_create(kt_log_rw_arq, NULL, &mainproc, 0, 0, "raymond_main_thread");
```
and works fine
but when I try use 

```
kthread_create(kt_log_rw_arq, NULL, &mainproc, 0, 0, "raymond_main_thread");
```
and not works,

what is the difference between kthread_create and the kproc_create?

thanks


----------



## SirDice (Aug 8, 2011)

I'm not a kernel hacker, but



			
				victormenegusso said:
			
		

> what is the difference between kthread_create and the kproc_create?





> In FreeBSD 8.0, the older family of kthread_*(9) functions was renamed to be the kproc_*(9) family of functions, as they were previously misnamed and actually produced kernel processes.  This new family of kthread_*(9) functions was added to produce real kernel threads.  See the kproc(9) man page for more information on the renamed calls.  Also note that the kproc_kthread_add(9) function appears in both pages as its functionality is split.


See kthread(9).


----------



## victormenegusso (Aug 8, 2011)

SirDice thanks, i will read about this.


			
				SirDice said:
			
		

> I'm not a kernel hacker, but
> 
> 
> 
> ...


----------

