简体   繁体   English

Linux内核模块编程中的_do_fork()问题

[英]Problem of _do_fork() in Linux kernel module programming

I am new to Linux programming, and I am stuck in trouble when programming a loadable kernel module for the Operating System course. 我是Linux编程的新手,为操作系统课程编程可加载的内核模块时遇到麻烦。 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. 在该函数内,应使用kernel / fork.c中定义的_do_fork()例程创建一个子进程。 The parameter list of _do_fork is as below: _do_fork的参数列表如下:

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"? 我的问题是如何填写参数“ sttack_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()? 谁能给我一个有关如何使用_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. 初始化program2.ko时,创建一个内核线程并运行my_fork函数。 (10 points) Within my_fork, fork a process to execute the test program. (10分)在my_fork中,派生一个进程来执行测试程序。 (10 points) (10点)

Hints: 1) Use “_do_fork” to fork a new process. 提示:1)使用“ _do_fork”派生一个新进程。 (/kernel/fork.c) (/kernel/fork.c)

Thanks a lot!!! 非常感谢!!!

If you search for uses of _do_fork , you will find, for instance, kernel_thread in kernel/fork.c : 如果搜索_do_fork用途, _do_fork ,您将在kernel/fork.c找到kernel_thread

/*
 * 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 ! 这是如何使用_do_fork创建内核线程的一个很好的例子! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM