Pan Heng Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
TASK_STRUCT structure:
struct task_struct {
volatile long state; process status
void *stack; Stack
pid_t pid; Process identifiers
unsigned int rt_priority; real-time priority
unsigned int policy; scheduling policy
struct files_struct *files; system Open File
...
}
Kernel handler function Sys_clone:
System calls enable the creation of processes through Do_fork:
return do_fork (clone_flags, newsp, 0, Parent_tidptr, child_tidptr);
The Linux system creates a child process by copying the information of the parent process, and in the Do_fork function, the copy_process function is actually replicated:
p = copy_process (Clone_flags, Stack_start, Stack_size,child_tidptr, NULL, Trace);
p = dup_task_struct (current); Creating a kernel stack
retval = Security_task_create (clone_flags);
retval = Sched_fork (Clone_flags, p); and scheduling-related settings, the CPU will dispatch this task
retval = Copy_thread (Clone_flags, Stack_start, Stack_size, p); Copies the contents of the parent process stack to the child process's stack. In this, the statement P->thread.ip = (unsigned long) in the Copy_thread function determines the first instruction address of the new process.
Create a stack function dup_task_struct:
Tsk = alloc_task_struct_node (node); Open up memory space
TI = Alloc_thread_info_node (tsk, node), Ti points to Thread_info's first address, and is the first address of two successive pages that the system assigns to the new process.
Err = Arch_dup_task_struct (tsk, orig); Copy the task_struct information of the parent process into the new task_struct
Tsk->stack = ti;task corresponding stack
Setup_thread_stack (tsk, orig); Initialize the thread info structure
Set_task_stack_end_magic (tsk); End of stack address setting data for stack end indicator
Where the new process begins to execute:
In the previous analysis, the Copy_thread () function in Copy_process is the function that determines the execution of a child process after it returns from the system call. Ret_from_fork determines the first instruction address of the new process. P->thread.ip = (unsignedlong) ret_from_fork; Sets the IP of the child process to the first address of ret_from_fork, and the child process from the Ret_from_ Fork to start execution
How the execution starting point is consistent with the kernel stack 1. Before Ret_from_fork, that is, *childregs = *current_pt_regs () in the Copy_thread () function, which assigns the regs parameter of the parent process to the child process's kernel stack, 2. The type of *childregs is Pt_regs, which holds the parameter 3 of the Save all in the stack. So it can be executed smoothly in the subsequent restore all.
Experiment: Analyzing the process of creating a new process for the Linux kernel
Summarize:
1.Linux creates a new process by copying the parent process, which is implemented by calling Do_fork.
2.Linux dynamically assigns a task_struct structure to each newly created process.
3. In order to organize all the processes in the kernel, Linux provides several organizational ways, in which the hash table and the bidirectional loop list method are for all processes in the system (including kernel threads), while the run queue and wait queue are organized by the process in the same state.
The 4.fork () function is called once, but returns two times.
The process of analyzing the Linux kernel to create a new process in week six