[Linux] Process (i)--Basic concepts

Source: Internet
Author: User

1, the basic concept of the process
In short, the process is the execution period of the program, but the process is not this, it also includes other resources (such as open files, suspended signals, etc.)
Linux is a multi-tasking multi-user operating system, a task is a process (process), that is, processes = tasks, in a Linux system, processes and threads share a series of resources (address space, files, signals, namespaces, etc.)
2, identification of the process;
Each process has a unique structure, the struct task_struct, and all information about a process can be identified in the struct, and each process's task_struct is composed of a task queue. With a doubly linked list: In addition, each process also corresponds to a unique number, that is, the Pid,pid maximum value is 32767 (32768=8*4096,32-bit system A page is 4096 bytes, each byte is 8bit), the next process created by the PID is the previous process PID plus 1, When the PID reaches the maximum value and starts from the minimum, Linux uses the Pidmap_array bitmap to know which PID is used, the PID used by all threads in a process is the same, but has its own different thread ID, and the thread ID is only valid in that process environment. The PID of the process is unique throughout the system, [CPP]View Plaincopy
    1. struct TASK_STRUCT
    2. {
    3. //......  
    4. pid_t pid;
    5. }
Task_struct How is the result body allocated? Slab dispenser? 3, how the kernel accesses the current process: [CPP]View Plaincopy
    1. #define Current get_current ()
    2. static inline struct task_struct *get_current (vold)
    3. {
    4. return Current_thread_info ()->task;
    5. }
Refer to another article about struct thread_info and kernel stacks: For each process, Linux puts two different data structures in a dynamic storage area of the kernel, size 8k, one struct thread_info, one kernel stack, On the ARM processor, the kernel obtains the THREAD_INFO structure address of the current CPU running process through the SP register pointer, in the Thread_info.h file 4, status of the process:Ready state, blocking state, operating state
The kernel saves the state of the process in the domain of the struct task_struct: [CPP]View Plaincopy
    1. struct TASK_STRUCT
    2. {
    3. volatile long state; /*-1 unrunnable, 0 runnable, >0 stopped */
    4. //......  
    5. }
Zombie Status: Process Zombie State: A state that represents the end of a process but not yet extinct. At this point the process has finished running and freed up most of the resources (discarding almost all memory space, without any executable code, and cannot be dispatched), but has not yet released Task_struct (which is left, so called Zombies), which records information such as the exit status of the process for other processes to collect, in addition, The zombie process no longer occupies any memory space. During the exit process, the kernel sends a signal to its parent process (the default is SIGCHLD, but can be set when the child process is created through the clone system call), notifying the parent process to "corpse". The parent process can wait for the exit of one or some of the child processes through a system call to the wait series, such as WAIT4, Waitid, and get its exit information. Then the system call of the wait series will also release the child process's corpse (task_struct) If his parent process is not installed SIGCHLD signal handler calls wait or Waitpid () waits for the child process to end, and does not explicitly ignore the signal, then it remains zombie state , the body of the child process (TASK_STRUCT) will not be released.
How to handle a zombie process: Find the parent process number and then kill the parent process, under Android, the child process is killed together
A function that sets the state of a process in the kernel: set_task_sate (task,state); 5, the priority of the process:There are four variables in task_struct that are related to the priority of a process: [CPP]View Plaincopy
    1. Sched.h
    2. struct TASK_STRUCT
    3. {
    4. //......  
    5. int Prio, Static_prio, Normal_prio;
    6. unsigned int rt_priority;
    7. //......  
    8. }
①prio refers to the current dynamic priority of a task whose value affects the scheduling order of the task.
②normal_prio refers to the general priority of a task, which is calculated based on the Static_prio and scheduling policy.
③static_prio refers to the static priority of a task, which is assigned when the process is created, which affects the length of time slices assigned to the task and the calculation of the dynamic priority of non-real-time tasks. Range is 120+nice value, nice value range is 20 to 19
④rt_priority refers to the real-time priority of a task. If 0 represents a non-real-time task, [1, 99] represents a real-time task, and the higher the value, the higher the priority.
For non-real-time tasks, Prio = Normal_prio = Static_prio Real-time process: Prio = Normal_prio = max_rt_prio–1–rt_priority The static priority of the child process inherits the static priority of the parent process, the child process's dynamic Priority inherits from normal priority of parent process ~ 6, each process relationshipThe Linux system process has an inheritance relationship, each process in the system has a parent process, init (PID 1, created by the Idle process (PID 0)) process is the ancestor of all user processes, and Kthreadd is the ancestor process relationship of all kernel processes in addition to father and son brother degrees, There may be other relationships, such as process groups, logon sessions, and so on. How the process derives the task_struct structure from the PID: The kernel mainly uses four hash tables according to the PID Tgid pgrp session four different meanings. [CPP]View Plaincopy
    1. {
    2. Pidtype_pid, //PID of the process
    3. Pidtype_tgid, //PID of thread group lead process
    4. Pidtype_pgid, //PID of Process group lead process
    5. Pidtype_sid, //PID of the session lead process

[Linux] Process (i)--Basic concepts

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.