Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the kernel use task_struct?

I am a student doing research involving Linux processes, and I need to learn more about them to proceed with my experiment. In reading a couple books and things online, I've come across task_struct, which I'm not sure I'm understanding fully, and would like confirmation/correction onto my existing thought.

From what I think I've understood, task_struct is the C structure that acts as the process descriptor, holding everything the kernel might need to know about a processes. At the end of the process kernel stack lives another struct, thread_info, which has a pointer to the processes task_struct.

Another question: how does one access the task_struct of a process? Is there a calculation to find the location of it's thread_info? Is there a macro/function within the kernel?

like image 395
Ramsey Alsheikh Avatar asked Jun 10 '19 18:06

Ramsey Alsheikh


People also ask

What is task_struct in Linux kernel?

From what I think I've understood, task_struct is the C structure that acts as the process descriptor, holding everything the kernel might need to know about a processes. At the end of the process kernel stack lives another struct, thread_info , which has a pointer to the processes task_struct .

Where is task_struct stored?

From the perspective of Virtual memory system, task_struct is allocated by the Slab allocator, so that it's located in the kernel space.

What is Thread_info Why is it stored at the end of kernel stack?

struct thread_info is stored at the bottom of stack if stack grows down and up if stack grows up. Let, Kernel Stack is 8KB of size. Also, it should have struct thread_info in it. This gives the remaining size of 8192-52 = 8140 Bytes.

How do you create a kernel process?

A kernel process is created by a kernel-mode routine by calling the creatp kernel service. The creatp kernel service allocates and initializes a process block for the process and sets the new process state to idle.


1 Answers

Yes, the task_struct structure contains all the information about a process. You can obtain a pointer to the structure that describes the current process using the current macro as follows:

struct task_struct *p = current;

If you want to get the structure that describes a process given a pid, you can use the find_task_by_vpid function as follows:

read_lock(&tasklist_lock);
p = find_task_by_vpid(pid);
if (p) get_task_struct(p);
read_unlock(&tasklist_lock);
if (p == NULL) {
    // Task not found.
}

// Later, once you're finished with the task, execute:
put_task_struct(p);

Finally, if you want to iterate over all processes, you can use for_each_process as follows:

read_lock(&tasklist_lock);
for_each_process(p) {
    // p is a pointer to a task_struct instance.
}
read_unlock(&tasklist_lock);

If you want to an exclusive access to the task list to be able to make changes to one or more fields in the structure, write_lock_irqsave must be used instead of read_lock.

like image 100
Hadi Brais Avatar answered Sep 25 '22 04:09

Hadi Brais