Job 6: Description of the process and creation of the process 20135115 Xuan Wenjun

Source: Internet
Author: User

Description of the process and creation of the process

Note: Xuan Wenjun, original works reproduced please indicate the source, "Linux kernel analysis"MOOC course http://mooc.study.163.com/course/ USTC-1000029000

I. Description of the process

The three main functions of the operating system: process management, memory management and file system.

1. Process Descriptor TASK_STRUCT Data structure

(1) Process Control block PCB---task_struct

(2) The process descriptor provides the process information that the kernel needs to understand.

Task_struct contains: Process status, process open files, process priority information.

Tty_struct Console

Fs_struct File System

Files_struct Open File Descriptor

Mm_struct Memory Management

Description of signal_struct communication signal

(3) struct task_struct has a very large data structure

(4) The state of the Linux process appears to be different from the process state described in operating system principles, such as: readiness and operational status are task_running.

The process call Do_exit () terminates execution, Task_zombie (the process is terminated).

Blocking states: Task_interruptible or Task_uninterruptible (waiting).

2, process of identification PID

3. TASK_STRUCT Data Structure Analysis

(1) Operating state state:-1 unrunnable,0 Runnable,>0 stopped.

Stack of kernel stacks for the process.

IFDEF CONFIG SMP conditional compilation, multi-processor use.

Process Chain List List_head tasks

Process address space of 4G

(2) Parent-child relationship of the process

P0 had three sons P1,p2,p3;p1 had two brothers P2,p3;p3 had a son P4.

(3) CPU-related status: Thread_struct

File system: Fs_struct

Ii. creation of the process

1, process creation overview and fork a process user-state code

Fork () is a system call that the user state uses to create a child process.

The fork () system call returns one time on each parent and child process, in which the PID returns a value of 0, executes the else code, and in the parent process, the return value of the PID is the ID of the child process, and the code that executes the else if.

2, the system call again review

3. Where does a newly created child process start?

Creating a new process is done by replicating the current process.

System calls kernel handler functions sys_fork, Sys_clone, sys_vfork

4. Copy process in Do fork is the main code to create a process.

The DUP task struct replicates the entire PCB.

Copy the kernel stack data and specify the first instruction address of the new process: *childregs = *current_pt_regs ();

5. Where does the new process of creation start?

P->thread.ip = (unsigned long) ret_from_fork;

When the child process gains CPU control, its return from fork can return the Iret in the back stack to the user state.

Iii. using GDB to track the process of creating a new process

1, first delete the menu, and then clone a new menu.

2. Cover test.c with TEST_FORK.C:

CD Menu

MV Test_fork.c test.c

Make rootfs//Compilation

3. Commissioning:

Qemu-system-x86_64-kernel Bzimage-initrd/home/yl/menu/rootfs.img-s-S

Gdb/usr/src/linux-source-4.4/vmlinux

Set Arch i386:x86-64

Target remote:1234

4. Set Breakpoint: B sys_clone

b do_fork

b dup_task_struct

b copy_process

b copy_thread

b ret_from_fork

5, press C to continue execution, but will error display timeout, at this time to restart the GDB can be.

6, press N step, press S into a function to step into the execution.

The registers of the current process's kernel stack are copied into the process:

Sets the IP of the child process to be dispatched, which is the starting point of the subprocess ret_from_fork:

Summarize:

This course is about the structural analysis of processes and the creation of processes. The Linux system creates a new process that relies mainly on fork (), fork () is the system call that the user state uses to create a child process, and the fork () calls Do_fork to implement the process creation.

First copy a pcb--task_struct, then assign a new kernel stack to the new process, and modify the copied process data, such as PID, process chain list and so on. With the above steps, you can create a new process.

The starting point for the new process is ret_from_fork, which replicates the kernel stack through the *childregs = *current_pt_regs () statement.

In the course of the experiment, you will encounter gdb problem, show the connection timeout, the need to restart GDB at this time. Also, you need to be aware of path problems when debugging the kernel. If no path is given, the file is located in the current directory.

Job 6: Description of the process and creation of the process 20135115 Xuan Wenjun

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.