2019-2020-1 20199320 "Linux kernel principle and Analysis" in the seventh week of work

A description of the process of

  1. OS operating system kernel to achieve three major management functions: process management, and file system management kernel, the core of which is a function of process management.

  2. In principle the operating system, by the process control block PCB we described process, usually a data structure struct task_struct described process, the structure is roughly divided into the following sections:

    • Process State (State)

    • Process scheduling information (Scheduling Information)

    • Various identifiers (Identifiers)

    • For information on interprocess communication (IPC: Inter_Process Communication)

    • Time and timer information (Times and Timers)

    • Process link information (Links)

    • File system information (File System)

    • Information virtual memory (Virtual Memory)

    • Management information page (page)

    • Symmetric Multi-Processor (SMP) information

    • And a processor associated environment (context) information (Processor Specific Context)

      Following analysis of the process in which states:

      Linux kernel process state management of the conversion as shown below:

Second, the experimental content: analysis of the Linux kernel process create a new process

  1. MenuOS the fork in order to increase the specific commands, and achieve the effect shots as shown below:

  2. Open another terminal, for gdb trace, set breakpoints for sys_clone, do_fork, dup_task_struct, copy_process, copy_thread ret_from_fork and, to achieve the effect as shown:

    • After running the first stop in sys_clone at:

    • Then do_fork, followed copy_process:

    • Enter copy_thread:

    • In copy_thread, we can see the value of p:

    • But back then copy_process view, will be prompted with a value optimized out, this is because the Linux kernel is open gcc -O2 option if you want to turn off optimization cause, can refer to: here

    • ret_from_fork be invoked in the previous analysis, tracking can not continue to syscall_exit after. To debug a system call in the machine, then when you enter the system call, the system has been in a suspended state. If you want to track debugging system_call, etc. you can be used kgdb

The new process is carried out where to start

Speaking before the copy_process in copy_thread () function, it was decided to perform this function after the child returns from the system call.

int copy_thread(unsigned long clone_flags, unsigned long sp,
      unsigned long arg, struct task_struct *p)
      {
          ...
          
          *childregs = *current_pt_regs();
          childregs->ax = 0;
          if (sp)
              childregs->sp = sp;
          
          p->thread.ip = (unsigned long) ret_from_fork;
          
          ...
}

Sub Process Execution ret_from_fork

ENTRY(ret_from_fork)
          CFI_STARTPROC
          pushl_cfi %eax
          call schedule_tail
          GET_THREAD_INFO(%ebp)
          popl_cfi %eax
          pushl_cfi $0x0202       # Reset kernel eflags
          popfl_cfi
          jmp syscall_exit
          CFI_ENDPROC
END(ret_from_fork)

How to perform a starting point and the kernel stack to ensure consistent?

  • Before ret_from_fork, that is copy_thread () function childregs = current_pt_regs (); This sentence will regs parameters assigned to the parent process child process kernel stack,

  • * Childregs of type pt_regs, the parameters stored inside pushed onto the stack in the SAVE ALL

  • Therefore, in RESTORE ALL after the smoothly implemented.

Third, the summary

  • Linux creates a new process by copying the parent process, done by calling do_fork

  • Linux task_struct structure for each process assigned a newly created dynamically for.

  • In order to organize all processes in the kernel up, Linux offers several organization with a hash table, and two-way circular linked list is for all processes in the system (including kernel threads), and run queue and wait queue is put in the same process state organized

  • fork () function is called once but returns twice.

Guess you like

Origin www.cnblogs.com/liangxu111/p/11789611.html