20169217 "Linux kernel Fundamentals and analysis" eighth Week work

Source: Internet
Author: User

The operation is still divided into two parts, the first part of the experiment.

Experimental requirements:

1. Read and understand the TASK_STRUCT data structure.
2. Analyze the kernel processing sys_clone of the fork function to understand how to create a new process and how to create and modify TASK_STRUCT data structures.
3. Use GDB trace to analyze a fork system call the kernel handler function Sys_clone to verify the understanding of creating a new process for the Linux system.

Experimental content:

1. Understand the TASK_STRUCT data structure.

operating system kernel inside the operating system so-called three major functions, is the process management, memory management and file system. But the core of this is process management, in Linux we have a data structure task_struct. To manage the process, the kernel describes this thing of the process, and we call it the process descriptor. It provides the information that the process needs to know.

The process state Change graph is:

Let's take a look at the process changes,the process state in the operating system has the state of readiness, operation, and blocking, and the actual process state of the kernel management is different. Let's see why not the same, you see we're inLinuxtransition diagram of the process state, we useForkto create a new process, the actual state is ready, but not running. When the scheduler chooses to the newly created process, it switches to the running state, which is alsotask_running. Both states are the same, that is, when the process istask_runningState of the time, it is operational, there is no running it, and this depends on whether it has to getCPUcontrol of the power. Which means that this process has noCPUon the actual execution. If theCPUThe actual execution is the running state, if it is dispatched, waiting is the ready state. So this is a bit different from the operating system principle. A running process, we call theDo_exit, that is, the termination, will enterTask_zombie, which is the terminating state of the process, the system processes the process out. This is the ready state and the running state. We look at a running process that goes into a blocking state when it waits for a particular event or resource.

2. Analyze the kernel processing sys_clone of the fork function to understand how to create a new process and how to create and modify TASK_STRUCT data structures.

Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to implement the process, in/LINUX-3.18.6/KERNEL/FORK.C, the code is as follows:

1623LongDo_fork (unsignedLongClone_flags,1624UnsignedLongStack_start,1625UnsignedLongStack_size,1626          int__user *Parent_tidptr,1627          int__user *child_tidptr)1628{1629    structTask_struct *p;1630    inttrace =0;1631    Longnr;16321633    /*1634 * Determine whether and which event to report to Ptracer. When1635 * Called from Kernel_thread or clone_untraced are explicitly1636 * requested, no event is reported; Otherwise, report if the event1637 * for the type of forking is enabled.1638*/1639    if(! (Clone_flags &clone_untraced)) {1640        if(Clone_flags &clone_vfork)1641trace =ptrace_event_vfork;1642        Else if((Clone_flags & csignal)! =SIGCHLD)1643trace =Ptrace_event_clone;1644        Else1645trace =ptrace_event_fork;16461647        if(Likely (!ptrace_event_enabled (current, trace)))1648trace =0;1649    }16501651p =copy_process (Clone_flags, Stack_start, Stack_size,1652child_tidptr, NULL, trace);1653    /*1654 * Do this prior waking up the new thread-the thread pointer1655 * might get invalid after that point, If the thread exits quickly.1656*/1657    if(!Is_err (P)) {1658        structcompletion vfork;1659        structPID *pid;16601661trace_sched_process_fork (current, p);16621663PID =get_task_pid (P, pidtype_pid);1664NR =PID_VNR (PID);16651666        if(Clone_flags &Clone_parent_settid)1667Put_user (NR, parent_tidptr);16681669        if(Clone_flags &clone_vfork) {1670P->vfork_done = &vfork;1671Init_completion (&vfork);1672get_task_struct (p);1673        }16741675Wake_up_new_task (p);16761677        /*forking complete and the started to run, tell Ptracer*/1678        if(Unlikely (trace))1679Ptrace_event_pid (Trace, PID);16801681        if(Clone_flags &clone_vfork) {1682            if(!wait_for_vfork_done (P, &vfork))1683ptrace_event_pid (Ptrace_event_vfork_done, PID);1684        }16851686put_pid (PID);1687}Else {1688NR =Ptr_err (p);1689    }1690    returnnr;1691}

First define a TASK_STURCT structure pointer, and then use the Copy_process function to copy a process, so the real copy process in the Copy_process function, the function of the main execution content is:

struct task_struct *p; retval =



retval = Copy_thread (Clone_flags, Stack_start, Stack_size, p);

In the Copy_thread function of linux-3.18.6/arch/x86/kernel/process_32.c

Long ) Childregs;  Plong) ret_from_fork;   Childregs0;     // Fork Child process returned as 0

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 = (unsigned long) ret_from_fork; Sets the IP of the child process to the first address of ret_from_fork, and the child process starts from ret_from_fork

Third, using the GDB trace Analysis a fork system call kernel processing function Sys_clone, verify your understanding of the Linux system to create a new process

RM menu-rfgit clone https://github.com/mengning/menu.git-kernel linux-3.18 . 6/arch/x86/boot/bzimage-initrd rootfs.img-s-sgdbfile linux-3.18. 6/vmlinux    symbol table for load debugging target remote:1234

To set breakpoints:

b sys_cloneb do_forkb DUP_TASK_STRUCTB COPY_PROCESSB copy_threadb ret_from_fork

20169217 "Linux kernel Fundamentals and analysis" eighth Week work

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.