Description of the notes process for the sixth week of the Linux kernel analysis and the creation of the process

Source: Internet
Author: User

Description of the process and creation of the process

I. Description of the process

1. Process descriptor TASK_STRUCT Data structure (i)

The three main functions of the operating system: Process Management (CORE), memory management, file system.

Process Control block Pcb--task_struct (Process Descriptor): 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.

    • struct TASK_STRUCT data structure is very large, there are about 400 lines of code

    • 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?

There are three states in the operating system principle: Ready state, running state. Blocking state.

When the fork is called to create a new process, the actual state is task_running (ready but not running), when the scheduler chooses a task or switches to task_running, (for what?). When the process is task_running state is operational, but whether to run or whether to get control of the CPU (there is no actual execution on the CPU)) process call Do_exit () abort execution, enter Task_zombie (zombie process)

A running process enters a blocking state while waiting for a specific event or resource, and when the blocking condition is gone, it enters the ready state.

    • Process-coded PID

PID and Tpid are used to identify processes

    • 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

2. Process descriptor TASK_STRUCT Data structure (ii)

Reading Comprehension TASK_STRUCT Data structure

structtask_struct {
1236volatile LongState/*state is a running state */
1237void*stack;/* Specifies the kernel stack for the process */
1238atomic_tusage;
1239unsigned intFlags/* Identifier */
1240unsigned intPtrace
1241
1242#ifdefconfig_smp/* conditional Compilation multiprocessor used to */
1243structLlist_nodewake_entry;
1244intON_CPU;
1245structTask_struct *last_wakee;
1246unsigned LongWakee_flips;
1247unsigned LongWakee_flip_decay_ts;
1248
1249intWAKE_CPU;
1250#endif
1251inton_rq;/* run queue and process scheduling related programs */
1252
1253intPrio, Static_prio, Normal_prio;
1254unsigned intrt_priority;
1255Const structSched_class *sched_class;
1256structSched_entityse;
1257structSCHED_RT_ENTITYRT;
1258#ifdefConfig_cgroup_sched
1259structTask_group *sched_task_group;
1260#endif
1295structList_head tasks;/* Process Chain list */

/* Doubly linked list */
1296#ifdefConfig_smp
1297structPlist_nodepushable_tasks;
1298structRb_nodepushable_dl_tasks;
1299#endif
1300
1301structMm_struct *mm, the address space of the *active_mm;/* process management process is relevant * * Each process has a separate process address space 4g,32 bit x86.
1302#ifdefConfig_compat_brk
1303unsignedBrk_randomized:1;
1304#endif
1305/* Per-thread VMA caching */
1306u32 Vmacache_seqnum;
1307structVm_area_struct *vmacache[vmacache_size];
1308#if defined(split_rss_counting)
1309structTask_rss_statrss_stat;
1310#endif
1330 pid_t pid;/* Logo */
1331pid_ttgid;
1337/*
1338 * pointers to (original) parent process, youngest child, younger sibling,/* process parent/son relationship */
1339 * older sibling, respectively. (P->father can replaced with
1340 * p->real_parent->pid)
1341 */
1342struct task_struct __rcu *real_parent; / * Real Parent process */
1343struct task_struct __rcu *parent; /* recipient of SIGCHLD, WAIT4 () reports */
1344/*
1345 * children/sibling forms the list of my natural children
1346 */
1347struct list_head children; / * List of my children */
1348struct list_head sibling; / * Linkage in my parent ' s children list */
1349struct task_struct *group_leader; / * Threadgroup leader */



1411/* Current task and CPU-related states play an important role in process context switching.
1412struct thread_struct thread;
1413/* FileSystem information * /

< span class= "C" >
1414 struct fs_struct *fs;/* file System Related data structure */
1415/* Open File Information */
1416 struct files_struct *files;/* Open File Descriptor list */
1417/* namespaces */
1418 struct Nsproxy *nsproxy;
1419/* signal handlers *//* work related to signal processing */

Ii. creation of the process

1, process creation overview and fork a process user-state code

The process descriptor is the gschirnwirt of the whole system management.

Understand how the process was created? How do I schedule a switchover between processes?

  

The creation of a process: Copy the PCB of process No. 0, then modify it according to the needs of process 1th, then load an init execution program.

Fork the code of a child process

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int Main (int argc, Char * argv[])
  5. {
  6. int pid;
  7. / * Fork Another process * /
  8. pid = fork();/* system call to create a child process in the user state */
  9. if (PID < 0)/* ERROR handling */
  10. / * ERROR occurred * /
  11. fprintf(stderr,"Fork failed!" );
  12. exit( -1);
  13. Else if (pid = = 0)/* co-executes with else */
  14. {
  15. / * Child process * /
  16. printf ("This is a childprocess!\n");
  17. Else
  18. / * Parent Process * /
  19. printf("This is theParent process!\n");
  20. / * Parent'll wait for the complete*/
  21. Wait (NULL);
  22. printf("Child complete!\n");
  23. }
  24. }

The fork system call returns once for each child process and parent process. The return value of the PID in the child process is: 0. The return value of the PID in the parent process is: The ID of the child process.

Two processes after fork.

2. Understand how the process creates complex code

In the process of process scheduling, scheduling to a new non-scheduled process, the start of execution is the IP of my process set.
Creating a new process is accomplished by copying the information of the current process.
A parent process creates a child process, where a copy of the sub-process's PCB is copied, and the duplicated PCB is modified.
To assign a new kernel stack to the new process.

Review: Process creation process called by system

  

There are three functions in the creation process in Linux:

    • Fork, create child process
    • Vfork, similar to fork, but the parent-child process shares the address space, and the child process runs before the parent process.
    • Clone, primarily for creating threads

It is worth noting that the threads in Linux are implemented by the simulation process, and the thread libraries used by newer kernels are generally NPTL.

  3. Browse key code related to process creation process

4. Where does the new process of creation start?

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

IP pointing to Ret_from_fork

The fork () system call produces a child process that executes from ret_from_fork during system call processing.

Only part of the kernel stack is copied

5. Use GDB to track the process of creating a new process

To reduce the effect on subsequent experiments, delete test_fork.c and test.c and compile the kernel:

GDB debugging, Set breakpoints:

Executes the fork and finds that the fork function is parked in the parent process.

Paying special attention to where the new process starts? Why does it go smoothly? That is, the execution starting point is consistent with how the kernel stack is guaranteed.

    • Ret_ From_ Fork determines the first instruction address of the new process.
    • The child process starts at the ret_ From_ Fork.
    • Because before ret_ From_ Fork, that is, in the copy_ thread () function * childregs = * Current_ pt_ regs (), this sentence assigns the regs parameter of the parent process to the kernel stack of the child process.
    • * The Childregs type is Pt_ regs, which stores the parameters of the Save_ all in the stack, so it can be executed smoothly in the subsequent restore all.

Iii. Summary

The fork () function creates a new process through the following series of functions: Fork (), Sys_clone (), Do_fork (), Dup_task_struct (), copy_process () Copy_thread (), Ret_from_fork (). The three main functions of the operating system: Process Management (CORE), memory management, file system. Process Control block Pcb--task_struct (Process Descriptor): 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. Linux creates a new process by copying the parent process, and the fork system call returns one time between the child process and the parent process. The return value of the PID in the child process is: 0. The return value of the PID in the parent process is: The ID of the child process.

Description of the notes process for the sixth week of the Linux kernel analysis and the creation of the process

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.