Linux process (i)---basic concepts

Source: Internet
Author: User

I. Definition OF the process


The          process is the concept of the operating system, and whenever we execute a program, a process is created for the operating system, along with the allocation and release of resources. A process can be thought of as a process of execution of a program.   The difference between process and program         program is static, it is an ordered set of instructions saved on disk, without any concept of execution. The     process is a dynamic concept, which is the process of program execution, including creation, scheduling, and extinction.   III. representation of processes in Linux systems      in Linux systems, processes are described by a struct called task_struct, which means that each process in Linux corresponds to a task_struct struct. The structure records everything about the process. Let's take a look at its core fields.  struct task_struct{     //This is the running state of the process, 1 means not running, 0 is operational, and the >0 representative has stopped.     volatile Long state;     /*         flags is the current status flag for the process, as follows:          0x0000 0002 indicates that the process is being created          0x0000  0004 indicates that the process is preparing to exit          0x0000  0040 indicates that the process was forked out, but did not execute exec         0x0000   0400 indicates that this process was killed due to other processes sending related signals     */    unsigned int flags;     //represents the run priority of this process     unsigned     int   RT_PRIORITY;     //the structure records the status of process memory usage     struct   mm_struct *mm ;      //process number, which is the unique identity of the process     pid_t   pid;      //Process Group number     pid_t  tgid;     //real_parent is the "biological father" of the process, Whether or not they are "foster"     struct  task_struct  *real_parent;     // The parent is the process now, and it may be "stepfather"     struct   task_struct  *parent;      //here children refers to the process child's linked list that can get all the child's process descriptors     struct   List_head    children;      //the same, sibling the linked list of the process brothers, that is, all the children of their parent process     struct     List_head    sibling;     //This is the process descriptor of the main thread, and perhaps you might wonder why the thread is represented by a process descriptor because Linux does notThere is a separate implementation of the thread of the relevant structure, only a              process to replace the thread, and then do some special processing.     struct   Task_struct  *group_leader;     // This is the link list of threads for the process     struct   List_head   thread_group;     // This is the information that the process uses for CPU time, Utime is the time that is executed in the user state, Stime is the execution time in the kernel state     cputime_t   utime,stime;      //comm is a character array that holds the name of the process for a maximum length of 15, because Task_comm_len is 16    char   comm[ task_comm_len];     //Open File related information structure     struct  files_struct  *files;       //handle of information related to signal     struct   SIGNAL_ struct  *signal;    struct   sigband_struct  *sighand; };     TASK_STRUCT structure is very large, we do not need to understand all of its fields, just need to focus on the more important fields to pay attention to it. From the above analysis can be seen, a process at least a bit  1. Process number (PID), just like our ID, everyone is different.Sample. The process ID, too, is its only indication.  2. Status of the process, identifies whether the process is in run state, waiting state, stop state, or dead state  a. Running state: The process is either running or ready to run  b. Wait state: The process is waiting for an event to occur or some system resource  c. Stop state : At this point the process is terminated  d. Dead state: This is a terminated process, but also in the process vector array, occupies a task_struct structure.  3. The priority and time slice of the process. Different priority processes are scheduled to run differently, typically high-priority processes run first. A time slice identifies the time  4 a process will be run by the processor. Virtual memory     Most processes have some virtual memory (kernel threads and daemons are not), and Linux must track how memory is mapped to system physical memory.  5. Processor-related contexts       a process can be considered a sum of the current state of the system. Whenever a process runs, it uses the processor's register, stack, and so on, which is the context of the process. Also, whenever a process is paused, all CPU-related contexts must be stored in the task_struct of the process. When the process is restarted by the scheduler, its context resumes from here.    Linux Process files  linux operating system Each process has two data structure describing file related information.


First: Fs_struct, which contains the current working directory and root directory of this process, umask. Umask is the default mode in which new files are created, which can be changed by a system call.   Second: Files_struct, which contains information about all the files that this process is using. The F_mode field describes what mode the file was created in: read-only, read-write, or write-only. F_pos saves the location where the next read or write in the file will occur. F_inode describe the VFS index node of a file, whereas F_ops is a pointer to a routine vector, each representing a function that wants to be applied to the action of the file.      One of the free file pointers in Files_struct is used to point to the new document structure each time one of the files is opened. The Linux process starts with three file descriptors opened, which are standard input devices, standard output devices, and standard error devices, and are typically inherited from the parent process that created the process. All access to the file is made by standard system calls that pass or return a file descriptor. These descriptors are the index of the process fd vectors, so the standard input devices, standard output devices, and standard error devices correspond to file descriptors 0, 1, and 2, respectively.   Virtual memory in process       in the Linux operating system, when we run a two-level executable, the operating system creates a process. In this case, it would be wasteful to load all the code and data of this executable binary into physical memory. Because they cannot be used at the same time. As the number of processes in the system increases, this waste will be multiplied and the system will run very inefficiently. In fact, Linux uses a technique called request paging (demand-paging): its corresponding data is loaded into physical memory only when the process is using its virtual memory. Therefore, the code and data are not loaded directly into physical memory. The Linux kernel modifies only the page tables of the process, identifying the virtual memory pages that exist but their corresponding data is not in memory. When a process wants to access code or data, the system hardware generates a page fault and gives control to the Linux kernel to resolve. Therefore, for each memory area in the process address space, Linux needs to know where the virtual memory comes from and how to load it into memory to resolve the failure.         When a process allocates virtual memory, Linux does not really reserve physical memory for it. It simply creates a new VM_AREA_STRUCT data structure to describe the virtual memory, which is linked into the virtual memory list of the process. A page failure occurs when the process attempts to write a virtual address that is located in the newly allocated virtual memory area. The processor attempted to convert the virtualBut because there is no page table entry for this memory, it discards and produces a page fault exception that is left to the Linux kernel for resolution. Linux to see if the referenced virtual address is the virtual memory address space located in the current process. If it is Linux, create the appropriate PTE and allocate a page of physical memory for this process. The code or data may need to be read into physical memory from the file system or the swap hard disk. The process can then be restarted from the instruction that caused the page failure, and because the physical address of the memory is present, it can continue to execute. If not, it is the "segment error" that we often see.      hehe, more slender introduction look at the Linux process address space step by step exploration.  from:http://blog.chinaunix.net/uid-26833883-id-3193588.html

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.