20135327 Guo Hao--linux Kernel Analysis Description of the sixth week process and creation of the process

Source: Internet
Author: User

Description of the process and the creation of a process, a description of the process

The three main functions of the operating system:

    • Process Management
    • Memory management
    • File system

Process Descriptor TASK_STRUCT Data structure

    • Task _ Struct: In order to manage processes, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand.

    • Status of the process: status of the Linux process (ready, run, blocked)

    • Process-marked PID: Used to mark the process

Process descriptor TASK_STRUCT Data structure (important part):

1 structtask_struct {2 volatile LongState/*running state of the process -1 unrunnable, 0 runnable, >0 stopped*/3 void*stack;/*the kernel stack for the process is specified*/4 atomic_t usage;5UnsignedintFlags/*identifiers for each process*/6 7 intON_RQ;/*Run Queue*/8 9pid_t pid;/*Process Identifiers*/Ten  OneStruck List_head task;/*Process Chain List*/ A  - /*Parent-Child processes*/ - structTask_struct __rcu *real_parent;/*Real Parent Process*/ the structTask_struct __rcu *parent; -  - structList_head children;/*List of my children*/

Ii. creation of the process

The start _ Kernel code in Rest _ init creates two kernel threads, kernel _ init and kthreadd.

Kernel _ init initiates the user-state process init, which is the ancestor of all user-state processes.

Kthreadd is the ancestor of all kernel threads.

Process Number No. 0 is the ancestor of all threads. (hand-written during process number No. 0)

Fork the code of a child process:

1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4 intMainintargcChar*argv[])5 {6     intpid;7     /*Fork Another process*/8PID =fork ();9     if(PID <0) Ten     {  One         /*error occurred*/ Afprintf (stderr,"Fork failed!"); -Exit (-1); -     }  the     Else if(PID = =0)  -     { -         /*Child Process*/ -printf"This is the child process!\n"); +     }  -     Else  +     {  A         /*Parent Process*/ atprintf"This is the Parent process!\n"); -         /*parent would wait for the*/ - Wait (NULL); -printf"Child complete!\n"); -     } -}

In particular, the code for the else if and if two modules will be executed, both the child process and the parent process will return, and the child process returns 0, and the parent process returns the child process PID.

Experiment:

1. Remove the menu and clone the new menu.

2. View Help

3.GDB Commissioning

4. Set Breakpoints

5. Single-Step tracking

Summarize:

Where does the new process start?

1 // Copy the kernel stack 2 0 // Why the fork of the subprocess returns 0, here is the reason!  3  4long// top 5long for the kernel stack dispatched to the child process // the address of the first instruction when dispatched to a child process

Linux creates a new process by copying the parent process: Copy the parent process pcb--task_struct to create a new process and assign a new kernel stack to the new process.

Modify the copied process data, such as PID, process chain list and so on to perform copy_process and Copy_thread.

Set the top of the kernel stack when the SP is dispatched to the child process, the first instruction address when IP goes to the child process

Then, when the sub-process gets control of the CPU to start running, ret _ form fork can stack the back stack, return from Iret to the user state, and then switch to the child process user space to complete the creation of the new process.

20135327 Guo Hao--linux Kernel Analysis Description of the sixth week process and creation of the process

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.