Linux Kernel – start_kernel in main.c and task_struct for process management

Linux start with start_kernel function in main.c. It has lots of function, such mm_init for kernel memory allocators. Let’s start with some very basic components firstly.

task_struct is an basic and important struct for process.
It is defined in “include/linux/sched.h”

One process will be linked with others, for example, parent process and child process are linked together.

each member of task_struct is very critical and it is better to go though all of them to understand its better.

Check the definition of task_struct, you could print more information you want,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/errno.h>   /* error codes */
#include <linux/sched.h>
 
 
MODULE_LICENSE("Dual BSD/GPL");
 
/* Declaration of functions */
void device_exit(void);
int device_init(void);
 
/* Declaration of the init and exit routines */
module_init(device_init);
module_exit(device_exit);
 
int device_init(void)
{
    struct task_struct *task = current; // getting global current pointer
    printk(KERN_NOTICE "assignment: current process: %s, PID: %d", task->comm, task->pid);
    do
    {
        task = task->parent;
        printk(KERN_NOTICE "assignment: parent process: %s, PID: %d", task->comm, task->pid);
    } while (task->pid != 0);
    return 0;
}
 
void device_exit(void) {
  printk(KERN_NOTICE "assignment: exiting module");
}

Screenshot from 2016-02-12 01:39:43