20135239 Benefits Silam Description of the Linux kernel analysis process and creation of processes

Source: Internet
Author: User

"Yi Silam Original works reproduced please indicate the source" Linux kernel Analysis "MOOC course http://mooc.study.163.com/course/USTC-1000029000"

Description of the six-week process and the creation of a process
    • 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
    • The state of the Linux process seems to be different from the process state described in the operating system principle, such as the readiness state and the running state are task_running, why?
    • 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
      • When the process is in the kernel state, it is different from the user-state stack, that is, the core stack is specified in the PCB, why is there no user-state stack in the PCB? How is the user-state stack set?
      • The kernel controls the path with very few stacks, so 8KB is enough for stacks and thread_info
    • struct THREAD_STRUCT thread; Cpu-specific State of this task
    • File system and file descriptors
    • Memory management-The address space of the process
Creation of processes
    • Process creation overview and fork the source of a process
    • Review:
      • Startkernel creates the CPUidle, which is the No. 0 process. And the No. 0 process has created two threads, one is kernel_init, that is, the 1th process, the process eventually started the user state;
      • The other is Kthreadd. This is "Daosh one, Life two". Process number No. 0 is a fixed code;
      • The 1th process was modified on the basis of the replication process PCB No. 0.
    • 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
A sub-process code for Fork
`#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");      }  }

`

To create a new process in the kernel execution process
  1. Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to implement the process;
  2. Linux creates a new process by replicating the parent process, which gives us an idea of the framework that this process provides:

    • Copy a pcb--task_struct
    • err = arch_dup_task_struct(tsk, orig);
    • To assign a new kernel stack to the new process
    • ' ti = allocthreadinfo_node (tsk, node);

      Tsk->stack = Ti;

      Setupthreadstack (tsk, orig); This is just a copy, not a copy of the kernel heap '

  3. To modify the copied process data, such as PID, process chain list and so on to change it, see copy_process inside.
  4. 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 theSP and IP in the thread record in the task struct, where is it set? Copythread in copy_process
  5. *childregs = *current_pt_regs(); //复制内核堆栈
  6. childregs->ax = 0; //为什么子进程的fork返回0,这里就是原因!
  7. p->thread.sp = (unsigned long) childregs; //调度到子进程时的内核栈顶
  8. p->thread.ip = (unsigned long) ret_from_fork; //调度到子进程时的第一条指令地址
Using GDB to track the process of creating a new process
    • Update the menu kernel, and then delete test_fork.c and test.c (to reduce the effect on subsequent experiments
    • To compile the kernel, you can see the fork command
    • Start GDB debugging and set breakpoints on the main function
    • Fork in Menuos, you will find that the fork function is parked in the parent process
    • After continuing, stop at theposition of Do fork. Then n steps into the copy process, and then to theDUPtaskstruct. Press S to enter the function, you can see DST = src (that is, the struct that replicates the parent process)
    • In copythread, you can seethat the task Pg_regs (p), which is the kernel stack-specific address, is found and initialized
    • The code for 159 or 160 lines is to put the pressed code into the sub-process:

       `*children = *current_pt_regs();  childregs->ax = 0;`
    • 164 lines, is the return address is OKp->thread.ip = (unsigned long) ret_from_fork;
    • Finally, enter finish to complete the operation.
Summarize

This week is mainly the development of the textbook process chapter, through the practice to more complete, very interesting.

20135239 Benefits Silam Description of the Linux kernel analysis process and creation of processes

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.