Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4069462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:30:15+00:00 2026-05-20T16:30:15+00:00

I wrote the following code to create a kernel thread: #include<linux/init.h> #include<linux/module.h> #include<linux/kernel.h> #include<linux/kthread.h>

  • 0

I wrote the following code to create a kernel thread:

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kthread.h>
#include<linux/sched.h>

struct task_struct *task;
int data;
int ret;
int thread_function(void *data)
{
    int var;
    var = 10;
    return var;
}

static int kernel_init(void)
{
    data = 20;
    printk(KERN_INFO"--------------------------------------------");
    task = kthread_create(&thread_function,(void *)data,"pradeep");
    task = kthread_run(&thread_function,(void *)data,"pradeep");
    printk(KERN_INFO"Kernel Thread : %s\n",task->comm);
    return 0;
}

static void kernel_exit(void)
{
    ret = kthread_stop(task);
}

module_init(kernel_init);
module_exit(kernel_exit);

On giving the insmod command, I am able to create a kernel thread named “pradeep” and I can see the new thread using the
ps -ef command as follows

root      6071     2  0 10:21 ?        00:00:00 [pradeep]

and its parent is kthreadd whose PID is 2.
But I am not able to stop this thread on giving rmmod command. It is giving the following output:

ERROR: Removing 'pradeep': Device or resource busy.

Can somebody please tell me how to kill this thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:30:16+00:00Added an answer on May 20, 2026 at 4:30 pm

    You should use only one of kthread_create() or kthread_run():

    /**
     * kthread_run - create and wake a thread.
     * @threadfn: the function to run until signal_pending(current).
     * @data: data ptr for @threadfn.
     * @namefmt: printf-style name for the thread.
     *
     * Description: Convenient wrapper for kthread_create() followed by
     * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
     */
    #define kthread_run(threadfn, data, namefmt, ...)                      \
    ({                                                                     \
        struct task_struct *__k                                            \
                = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
        if (!IS_ERR(__k))                                                  \
                wake_up_process(__k);                                      \
        __k;                                                               \
    })
    

    So you’re creating two threads and leaking one of them:

    task = kthread_create(&thread_function,(void*) &data,"pradeep");
    task = kthread_run(&thread_function,(void*) &data,"pradeep");
    

    Furthermore, your thread function might be missing some details:

    /**
     * kthread_create - create a kthread.
     * @threadfn: the function to run until signal_pending(current).
     * @data: data ptr for @threadfn.
     * @namefmt: printf-style name for the thread.
     *
     * Description: This helper function creates and names a kernel
     * thread.  The thread will be stopped: use wake_up_process() to start
     * it.  See also kthread_run().
     *
     * When woken, the thread will run @threadfn() with @data as its
     * argument. @threadfn() can either call do_exit() directly if it is a
     * standalone thread for which noone will call kthread_stop(), or
     * return when 'kthread_should_stop()' is true (which means
     * kthread_stop() has been called).  The return value should be zero
     * or a negative error number; it will be passed to kthread_stop().
     *
     * Returns a task_struct or ERR_PTR(-ENOMEM).
     */
    

    I think the two choices for terminating a thread are:

    1. Call do_exit() when you’re done.
    2. Return a value when another thread calls kthread_stop().

    Hopefully after fixing these two small problems, you’ll have a functional thread creator / reaper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32
I wrote the following code to read the content of a file: #include <ifstream>
I wrote the following code for a Link list to create a Book its
I wrote the following code. It tries to create a storyboard that does the
I wrote the following code in SQLite : CREATE TABLE payments ( customerNumber REAL
I wrote the following code to create a dojo tree. store = new dojo.data.ItemFileWriteStore({url:
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following code to select text from database,but when i echo the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.