20135201 Li Chenxi Description and creation of the sixth week process of Linux kernel analysis

Source: Internet
Author: User

Li Chenxi Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. Description of the process

The three main management functions of the operating system:

    • Process management (most important)
    • Memory management
    • File system

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.

Process Control block PCB task_struct:

    • Process status
    • Files opened by the process
    • Process-Priority information

Task_struct abstraction of the overall data structure:

tty:控制台fs:文件系统files:文件描述符mm:内存管理signal:信号描述

process state: different from operating system (ready, running, blocked), such as the ready state and running state in Linux are task_running

task_running Specific is ready or implementation, depends on the system's current resource allocation;

Task_zombie also known as the zombie process

two. Creation of processes

--Review of process origins:

道生一(start_kernel....cpu_idle)一生二(kernel_init和kthreadd)二生三(即前面0、1和2三个进程)三生万物(1号进程是所有用户态进程的祖先,2号进程是所有内核线程的祖先)

Process No. 0, is the code to write dead, 1th process replication No. 0 Process PCB, then modify, and then load the executable program.

1.fork Code

1.#include<stdio.h>2.#include<stdlib.h>3.#include<unistd.h>4.int main (int argc,char * argv[])5.{6.int pid;7./* Fork another process */8.pid = fork ();9.if (PID <0)10.{11./* ERROR occurred */12.fprintf (stderr,"Fork failed!");13.exit (-1);14.}15.Elseif (PID = =0)PID = = 0 and the following else will be executed (one is the PID ==0 in the parent process, one in the child process, that is, the PID is not equal to 0)16.{17. /* Child process */18.printf ( "This is Child process!\n"); 19.} 20.else 21.{ 22./* parent process */23.printf ( Span class= "hljs-string" > "This is the Parent process!\n"); 24./* parent would wait for the child to Complete*/25.wait (null);  "child complete!\n");  27.}28.} 


/span>

2. Create a new process in the kernel execution process

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 copying the parent process:

do_fork主要是复制了父进程的task_struct,然后修改必要的信息,从而得到子进程的task_struct。

An imaginary framework for understanding this process:

Copy a pcb--task_struct

err = arch_dup_task_struct(tsk, orig);

To assign a new kernel stack to the new process

 ti = Alloc_thread_info_node (tsk, node) ;tsk->stack = Ti;setup_thread_stack ( tsk, orig) //This is just a copy of Thread_info, not a copy of the kernel stack       



3. What line of code does a newly created child process (after it obtains the CPU) be executed from?
    • In comparison with previously written my_kernel, kernel can specify where the new process will start (that is, specify the line of code via the EIP register). There are similar mechanisms in fork.
    • 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

      1.*childregs = *current_pt_regs(); //复制内核堆栈,并不是全部,只是regs结构体(内核堆栈栈底的程序)2.childregs->ax = 0; //为什么子进程的fork返回0,这里就是原因!3. 4.p->thread.sp = (unsigned long) childregs; //调度到子进程时的内核栈顶5.p->thread.ip = (unsigned long) ret_from_fork; //调度到子进程时的第一条指令地址,也就是说返回的就是子进程的空间了

three. Experiment: analyzing the process of creating a new process for the Linux kernel

1. start Menuos

cd LinuxKernel   rm menu -rfgit clone https://github.com/mengning/menu.gitcd menumv test_fork.c test.cmake rootfs
 

2.gdb Debug Fork Command

qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -s -Sgdbfile linux-3.18.6/vmlinuxtarget remote:1234



3. Set Breakpoints:

b sys_cloneb do_forkb dup_task_structb copy_processb copy_threadb ret_from_fork



4.继续执行之后,停在了do_fork的位置。




四.总结
Understanding of the "Linux system creates a new process":
P->THREAD.SP = (
P->thread.ip = (//The first instruction address when dispatched to a child process

The IP of the child process is set to the first address of the ret_ form fork, so the child process is executed from ret_ From_ fork.
The regs parameter of the parent process is assigned to the child process's kernel stack, and the *childregs type is Pt_regs, which holds the arguments to the stack in save all.








20135201 Li Chenxi Description and creation of the sixth week process of Linux kernel analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.