Possessing + original works reproduced please specify the source + "Linux kernel analysis" MOOC course +http://mooc.study.163.com/course/[email protected]
Description of the process ulk the big picture for task_struct
Sketchy, see the Pre-code overview
Preamble Process Control block pcb--task_struct
To manage the process, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand.
struct task_struct数据结构很庞大
The state of the Linux process seems to be different from the process state described in the operating system principle, such as the readiness state and the running state are task_running, why?
A66: Depends on whether you gain control of the CPU
Process-coded PID
Unique labeling for each process
Scheduling-related
Key keywords are:
Runqueue
Priority level
Seize
Scheduling information
struct Task_struct {...int ON_RQ; int Prio, Static_prio, Normal_prio; unsigned int rt_priority; const struct Sched_class *sched_class; struct sched_entity se; struct sched_rt_entity RT;...struct sched_dl_entity dl;...unsigned int policy; int nr_cpus_allowed; cpumask_t cpus_allowed;#ifdef CONFIG_PREEMPT_RCUint rcu_read_lock_nesting; Union rcu_special Rcu_read_unlock_special; struct List_head rcu_node_entry;#endif/* #ifdef CONFIG_PREEMPT_RCU * /#ifdef CONFIG_TREE_PREEMPT_RCUstruct Rcu_node *rcu_blocked_node;#endif/* #ifdef CONFIG_TREE_PREEMPT_RCU * /#ifdef CONFIG_TASKS_RCUunsigned long RCU_TASKS_NVCSW; BOOL Rcu_tasks_holdout; struct List_head rcu_tasks_holdout_list; int rcu_tasks_idle_cpu;#endif/* #ifdef CONFIG_TASKS_RCU * /#if defined (config_schedstats) | | defined (CONFIG_TASK_DELAY_ACCT)struct Sched_info sched_info;#endif...}
All process linked list struct list_head tasks;
The implementation method of the kernel's bidirectional cyclic link list-A more abbreviated two-way circular linked list
Standalone operation
Parent-child Relationship
A process created by a program has a parent-child relationship, and it is often necessary to refer to such a parent-child relationship when programming. There are several fields in the process descriptor that are used to represent such relationships
/* * Pointers to(original) parent process, youngest child, younger sibling, * older sibling, respectively. (P->father can replaced with* p->real_parent->pid) */struct task_struct __rcu *real_parent; /*RealParent process */struct task_struct __rcu *parent; /* Recipient ofSIGCHLD, WAIT4 () reports */* * children/sibling Forms the List of myNatural Children */struct list_head children; /*List of myChildren */struct list_head sibling; /* Linkageinch myParent ' s childrenList*/struct task_struct *group_leader; /* Threadgroup leader */
Mm_struct
This is not the focus of this chapter, but later chapters
The address space of the Linux process is an interesting topic.
CPU-related
Related to our menuos, note the SP/IP pointer.
structthread_struct {/* Cached TLS descriptors: */ structDesc_struct Tls_array[gdt_entry_tls_entries];unsigned LongSP0;unsigned LongSp#ifdef config_x86_32 unsigned LongSysenter_cs;#else unsigned LongUSERSP;/ * Copy from PDA * / unsigned ShortEsunsigned Shortdsunsigned ShortFsindex;unsigned ShortGsindex;#endif#ifdef config_x86_32 unsigned Longip#endif...}
At this point, about 400 lines of task_struct are introduced roughly to this
Memory Area
? Linux allocates a 8KB-sized memory area for each process to hold two different data structures for the process: thread_info and process kernel stacks
? When the process is in the kernel state, it is different from the user-state stack, that is, the core stack is specified in the PCB, why is there no user-state stack in the PCB? How is the user-state stack set?
? The kernel controls the path with very few stacks, so 8KB is enough for stacks and thread_info
? struct thread_struct thread;? Cpu-specific State of this task
? File system and file descriptors
Memory management-The address space of the process
Not into the MMU.
Two. Review of process creation
Start_kernel->
Kernel_init->
Kthreadd
Fork the code of a child process
#include <stdio.h>#include <stdlib.h>#include <unistd.h>intMainintargcChar* argv[]) {intpid/ * Fork Another process * /PID = fork ();//Core call if(PID <0) {/ * ERROR occurred * / fprintf(stderr,"Fork failed!");Exit(-1); }Else if(PID = =0) {/ * Child process * / printf("This is a child process!\n"); }Else{/ * Parent Process * / printf("This is Parent process!\n");/ * Parent'll wait for the complete*/Wait (NULL);printf("Child complete!\n"); }}
System Call Review
More than just calling a fork
A mate of int 0x80 and Iret
What is the source of the fork process? Let's look at the following
Create a new process in the kernel execution process (sys_clone–>do_fork)
Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to implement the process;
Linux creates a new process by replicating the parent process, which gives us an idea of the framework that this process provides:
Copy a pcb--task_struct
err = arch_dup_task_struct(tsk, orig);
To assign a new kernel stack to the new process
= alloc_thread_info_node(tsk, node);tsk->stack=//这里只是复制thread_info,而非复制内核堆栈
To modify the copied process data, such as PID, process chain list and so on to change it, see copy_process inside.
Look at the fork () from the user's code, the function returns two times, that is, each time it is returned in a parent-child process, the parent process returns from the system call is easier to understand, and the child process returns from the system call, where does it start executing in the process of system invocation? This involves the kernel stack data state of the child process and the consistency of the SP and IP in the thread record in Task_struct, where is this set? Copy_thread in Copy_process
Modification of process data
*=*//复制内核堆栈childregs->=0//为什么子进程的fork返回0,这里就是原因!p->thread.=//调度到子进程时的内核栈顶p->thread.=//调度到子进程时的第一条指令地址
Code Scenario Analysis
Fork.c
Do_fork–>copy_process–>dup_task_struct
Dup_task_struct
{ //slab管理 task_stuct... //keme_pages stack }
Copy_thread
Do_fork–>copy_process–>dup_task_struct
Child process Initialization (many) –>copy_thread
copy_thread{sp指针内核堆栈数据拷贝(重点展开)}
Figures
childregs->ax=0;
Here you see the position of the return value (PID) Assignment
Child process Start Location
The essential reason is ret_from_fork
Expand the Pt_regs content first, which is also the contents of the INT directive and the Save_all pressed to the stack
struct pt_regs { long ebx; long ecx; long edx; long esi; long edi; long ebp; long eax; int xds; int xes; int xfs; int xgs; long orig_eax; long eip; int xcs; long eflags; long esp; int xss;};
In system calls, Ax is the call number.
Expand Ret_from_fork
ENTRY(ret_from_fork) movi a4, schedule_tail callx4 a4 movi a4, do_syscall_trace_leave mov a6, a1 callx4 a4 j common_exception_returnENDPROC(ret_from_fork)...
Expand Syscall_exit
syscall_exit: LOCKDEP_SYS_EXIT DISABLE_INTERRUPTS(CLBR_ANY) # make sure we don‘t miss an interrupt # setting need_resched or sigpending # between sampling and the iret TRACE_IRQS_OFF movl TI_flags(%ebp%ecx $_TIF_ALLWORK_MASK%ecx# current->work jne syscall_exit_workrestore_all: TRACE_IRQS_IRET
The future process is actually the content of the last lesson.
Does the system call occur before the child process returns to the user station?
Personally think that will.
The reason for this is illustrated in the following figure (Ulk).
Three. Hands-on Labs
- RM Menu
- git clone
- MV Test_fork.c test.c
- Make Rootfs
Now, enjoy the work see the "fork"
The information to debug
- Sys_clone
- Do_fork
- Dup_task_struct
- Copy_process
- Copy_thread
- Ret_from_fork (*)
The result of debugging
- copy_process alloc_thread_info arc_dup_task_struct setup_thread_stack- copy_thread childregs //ptype current_pt_regs()//前后堆栈变化 ip- ret_from_fork //step by step 不跟踪schedule,continue- syscall_exit (直到这个位置)
Todo
Add hands
Other
GDB debugger, can the stack be modified in real time?
Thank:
Picture path problem
http://www.v2ex.com/t/57063
Picture path: c:/users/vanny/downloads/markdowneditor/local hard
Conditional compilation in comments is interpreted incorrectly
How do I enter the three quotation marks of a comment?
The client does not support syntax nesting? Like a note with a bold
There is a limited fallback function, only one step
And the call of the editor, the find.
How to let # do not show escape
Second Level directory
Green version and so on need to improve
Analysis of Linux Process management