Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

readme.md

This Linux kernel module, written in C, is a fine example to demonstrate several key concepts in kernel programming, such as

  • Kernel Threads,
  • Process States,
  • and Kernel Task iteration.

Below is a conceptual breakdown of the program:

1. Kernel Thread:

The program creates a kernel thread named print_running_thread to print out information about processes in a running state. The kernel thread is started at module initialization and is stopped at module exit.

print_thread = kthread_run(print_running_thread, NULL, "print_running_cpu"); kthread_stop(print_thread);

2. Process States:

The get_task_state function translates the process state value to a string representation. For instance, if a task’s state is TASK_RUNNING, it will return the string "TASK_RUNNING". If the task state is unknown, it will return a custom string showing the unknown type.

switch (state) { case TASK_RUNNING: return "TASK_RUNNING"; ... default: { sprintf(buffer, "Unknown Type:%ld", state); return buffer; } }

3. Iterating Over Tasks:

Within the kernel thread function print_running_thread, there’s a loop where the program iterates over each task in the system using the for_each_process macro. For each process, if the process state is TASK_RUNNING, it prints the process name, PID, and state.

for_each_process(task_list) { if (task_list->state == TASK_RUNNING) pr_info("Process: %s\t PID:[%d]\t State:%s\n", task_list->comm, task_list->pid, get_task_state(task_list->state)); }

4. Time Delays:

After printing the details about the running tasks, the thread sleeps for 500 milliseconds before checking again. This is achieved by using msleep(500);.

5. Module Initialization and Exit:

When the module is loaded into the kernel, the test_tasks_init function is called, initiating the kernel thread. Conversely, when the module is removed, the test_tasks_exit function is called, stopping the kernel thread.

module_init(test_tasks_init); module_exit(test_tasks_exit);

In Summary:

This program is a prime illustration of using kernel threads to periodically access and display kernel-level information (processes running) without affecting user space, showcasing the concept of process states and how to interact with and manipulate kernel-level process information efficiently.