"Linux Kernel Analysis" section sixth analyzes the process of creating a new process for the Linux kernel

Source: Internet
Author: User
Tags session id

Fan + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

First, the experimental process

1. Delete the original menu, and clone the new menu, with TEST_FORK.C overlay test.c

2. Make rootfs after the new kernel boot, test fork function

3. Use the-s-s frozen core to prepare for commissioning

4. Set Breakpoints

5. Follow the breakpoint to get the result

1, the following is the execution of instructions

CD Linuxkernel

    • RM MENU-RF
    • git clone https://github.com/mengning/menu.git
    • CD Menu
    • MV Test_fork.c test.c
    • Make Rootfs

2. gdb above the fork command

Close the Qemu window, and in the command line, enter:

Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S

Start Menuos again, and pause for GDB debugging.

Start debugging by entering the following command in turn:

Gdb

File Linux-3.18.6/vmlinux

Target remote:1234

Then set the following breakpoint:

b sys_clone

b do_fork

b dup_task_struct

b copy_process

b copy_thread

b ret_from_fork

After continuing, stop at the do_fork location. Then n steps into Copy_process, Dup_task_struct. Press S to enter the function, you can see dst =src (that is, the struct that replicates the parent process)

Second, the experimental summary

1. Fork system call

The fork call creates a new process. A new process, or a child process, is a copy of the calling process or the parent process.

1. If the fork executes successfully, it returns the PID of the child process to the parent process and returns 0 to the child process. This is all together. Even if you only call the fork once, he will return two times.

The new process created by 2.Fork is the same copy as the parent process (except PID and Ppid), including true and valid UID and GID, process mix session ID, environment, resource limit, open file, and shared memory segments.

There is a little difference between the parent and child processes. The child process does not inherit the file lock, or the pending signal, created by the parent process from the supermarket settings of the parent process (using the alarm call). The key concept to understand is that the new process that fork creates is an exact copy of the parent process.

3. In general, it is indeterminate whether the parent process executes first or the child process after the fork. This depends on the scheduling algorithm used by the kernel.

This week's video learning content is as follows:

One, the operating system kernel has three major functions: process management, memory management, file system

Ii. description of the process: when it comes to the description of the process, first analyze the process descriptor: task_struct

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 data structure is huge

Process-coded PID

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

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

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

Iii. Creation of processes

Overview of process creation and user-state code for a process fork

Iret corresponds to the int 0x80 directive, one is the pop-up register value, and one is the value of the press-in register.

If the system call is analogous to fork (), then it is equivalent to a system call that creates a child process, and then the child process returns and then runs in the kernel state, while returning to the parent process and still running in the user state

Fork the code of a child process

#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char * argv[]){int pid;/* fork another process */pid = fork();if (pid < 0) { /* error occurred */fprintf(stderr,"Fork Failed!");exit(-1);} else if (pid == 0) {/* child process */printf("This is Child Process!\n");} else {  /* parent process  */printf("This is Parent Process!\n");/* parent will wait for the child to complete*/wait(NULL);printf("Child Complete!\n");}}
Create a framework for a new process

Fork,vfork,clone can create new processes, all of which are implemented by calling Do_fork.

Copy_process Modify the copied PCB to fit the characteristics of the sub-process, that is, the initialization of the child process

Allocate a new kernel stack (for child process data)

Part of the kernel stack is also copied from the parent process

Linux creates a new process by replicating the parent process, which gives us an idea of how this process provides an imaginary framework: copying a pcb--taskstruct

1 err = arch_dup_task_struct(tsk, orig);

To assign a new kernel stack to the new process

123 ti = alloc_thread_info_node(tsk, node);tsk->stack = ti;setup_thread_stack(tsk, orig); //这里只是复制thread_info,而非复制内核堆栈

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 taskstruct, where is it set ?

copythread in copy_process

"Linux Kernel Analysis" section sixth analyzes the process of creating a new process for the Linux kernel

Related Article

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.