Process (i) Operating system principles and Linux process source language __linux

Source: Internet
Author: User
Tags data structures

This part is a bit messy, first of all, the state of the process, then some concepts, and finally Linux, some of the special process and source language.

Status of the process
In general, processes have four state-ready (Ready), execution (running), blocking (blocked), suspend (suspend) states.

1) Ready state
The process obtains all resources except the CPU, and there may be several processes in the system that are ready, often consisting of a queue called a ready queue.

2) Execution Status
Process in the process of execution always involves the user program and the operating system kernel program two parts, therefore, the execution state of the process can be further divided into user execution state and system execution state, and the main reason of distinguishing user and system State is to separate the user program and the system program so as to facilitate the protection and sharing of the program.

3) Blocking State
The process being executed is temporarily unable to continue because of an event that occurs. If the process requests that the memory cannot be satisfied or requests the I/O device and the device is busy, the process can only discard the CPU and enter a paused state, that is, the execution of the process is blocked, the pause state is called a blocking state (also known as the Wait state). Depending on the cause of the process blocking (such as waiting for IO, waiting for memory), the blocked process can be queued up, and when the event occurs (the device releases IO), a process is released from the corresponding blocking queue to the ready state.

4) Suspend state
The process of blocking in memory is temporarily exchanged to external storage according to a certain algorithm (corresponding to suspend operation suspend), and is inserted into the pending queue of the response, thereby emptying the memory to transfer the process (corresponding to the active operation Active) that will run in the external storage suspend queue to implement the virtual store management functionality. The thread is not suspended and will not enter the external memory because of the exchange. The following is a transition relationship between each state of the process.

The composition of the process and PCB
A process is typically composed of process control blocks (processes controls block, PCB), related program segments, and corresponding data structures (stacks, data, etc.). The system is aware of the process of PCB and the existence of processes, and through the PCB control, management process, PCB is the only sign of the process exists. The process of creating a new process is the process of establishing a PCB for the corresponding program. When the process is finished, the system recycles its PCB and the process dies.

Process space
Any process has a living address space of its own, which is the process space or virtual space (is txt, data, stack and other segments of the address space), in order to ensure that the system is safe and reliable, Linux divides process space into user space and system space (each process has its own virtual 3G user space in 32-bit situations, while the remaining 1G system space is shared by all processes). And in order to prevent user space access to the system caused by the destruction of the system, the operating system set the register state to different operating modes, that is, user mode and system mode to restrict user process access to system resources. System mode, you can run all of the system's instructions, access to all registers and memory areas, and user mode is limited.

Context of the process
program counters, registers, program status words, stack content is the main basis for process control, they reflect the process of implementation of the current situation together with the system changes of data, called the processor context. When the process switches the corresponding PCB, processor context will change, so the process of switching corresponds to the context of the switch.

process and source language in Linux

1. With regard to the resources occupied by the process

Can be divided into two categories, one is the process of the file descriptor and the storage space, are automatically released by the kernel before the process exits. There is also a portion of resources and information that needs to be notified after the process has been completed (each process has a parent process), including the process number, the termination status of the process (normal or abnormal, what the exception is abnormal), and the total amount of CPU used by the process.

2. About fork () and exec ()

Fork is the process of generating a subprocess, and the stack space and data space used are copies of the parent process if you do not call exec. If you call the EXEC function in fork, you replace the stack, data space, and body contents of the current process with a completely new program. More fork are used on the network server, the parent process accepts the request, receives a request to produce a subprocess to complete the request, possibly in a multiple-process manner to separate the space and security considerations from the various processes.

Linux starts when the system runs in the nuclear mindset, at this point, just create a idle process with PID number 0, which creates a kernel thread that eventually executes the init file after a series of initialization actions, and the result of the file init operation is the system running mode from system to user state. The process then evolves to the user process init its PID is 1. This init process is a very important process, and all subsequent processes in the system are his descendants. Init can create a new process through fork, and when the process completes, the process calls exit () to terminate itself, and when the process is undone, the resources involved in the process are also notified of the parent process. The exit () code is in kernel/exit.c, its primary function is Do_exit (), Do_exit () releases most of the process's resources, then goes to the Task_zombie state, and invokes Exit_notify () to notify the parent and child processes.

3. About the init process

This process can be the parent process of many processes, when a process is finished, the operating system accesses all processes to see if the process is a subprocess of the end process and, if so, sets the process's parent process to the INIT process (pid=1), (PS command View Init's parent process number is 0, But Stevenson says the process of Init's parent can be seen as itself. The init process is also a bit special, and when one of its subprocess ends, it immediately calls the wait function to accept the information returned by the subprocess, reclaim the remaining resources, and prevent the zombie process from being generated.

4. About Zombie Processes

If a process ends, but the parent process of the process does not invoke the wait function to do the cleanup (get the information, release the resources), the process is the zombie process (the process is finished, but still occupies the process number). If a process is running or a process is running for a long time, and a subprocess is generated, a lot of zombie processes can be generated.

5. With regard to the withdrawal of the process

Divided into normal and abnormal two cases. Normally, exit () and _exit () two functions are performed, and exit () performs some processing functions (terminating handlers, using atexit (void) (*fun) (void)), similar to destructors, and scavenging io. _exit () does not perform these functions, or whether to clean IO to see the implementation of different operating systems.

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.