2
votes

I am new to Linux programming, and I am stuck in trouble when programming a loadable kernel module for the Operating System course. In this task, I need to create a kernel thread when initializing the module, and then run a function. Within the function, a child process should be created by using _do_fork() routine defined in kernel/fork.c. The parameter list of _do_fork is as below:

long _do_fork(unsigned long clone_flags,
      unsigned long stack_start,
      unsigned long stack_size,
      int __user *parent_tidptr,
      int __user *child_tidptr,
      unsigned long tls)

My question is how to fill out the parameter "stack_start"? I know it specifies the location of stack which is used by the child process. Any one can give me an example of how to use _do_fork()?

Here attaches part of my code:

static struct task_struct *task;

int my_fork(void *argc){

    long pid = 0;

    /* fork a process using do_fork */
    pid = _do_fork(SIGCHLD, ???, 0, NULL, NULL, 0);

    printk("This is child process with pid %ld\n", pid);

    return 0;
} 

static int __init program2_init(void){

    printk("[program2] : Module_init\n");
    /* create a kernel thread to run my_fork */
    task = kthread_create(&my_fork, NULL, "GoodThread");    
    // wake up the thread
    if (!IS_ERR(task)) {
        printk("kthread starts\n");
        wake_up_process(task);
    }
    return 0;
}

And requirement in this assignment is attached:

When program2.ko being initialized, create a kernel thread and run my_fork function. (10 points) Within my_fork, fork a process to execute the test program. (10 points)

Hints: 1) Use “_do_fork” to fork a new process. (/kernel/fork.c)

Thanks a lot!!!

1
Why do you need a kernel thread? Try to avoid a lot of code in kernel land and prefer user-land helper processes. Also, explain what is your kernel module useful for (as a rule of thumb, you probably should first want to avoid coding kernel modules; if you really need one, make it as simple as possible). So edit your question to improve it and give a lot more motivation and context - Basile Starynkevitch
Actually I'm doing my Operating System assignment, so it is required to create a kernel thread :-) - PPPatrick
Then you should look into the source code of existing stuff starting kernel threads. Very probably they don't use _do_fork. And you still need to explain what is your kernel module intended to do - Basile Starynkevitch
Well, I'll show what the professor want in this module: When program2.ko being initialized, create a kernel thread and run my_fork function. (10 points) Within my_fork, fork a process to execute the test program. (10 points) Hints: 1) Use “_do_fork” to fork a new process. (/kernel/fork.c) - PPPatrick

1 Answers

2
votes

If you search for uses of _do_fork, you will find, for instance, kernel_thread in kernel/fork.c:

/*
 * Create a kernel thread.
 */
pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
{
    return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
        (unsigned long)arg, NULL, NULL, 0);
}

which is a very good example of how to create a kernel thread using _do_fork! ;)