Introduction to linux processes and introduction to linux

Source: Internet
Author: User

Introduction to linux processes and introduction to linux
1. Several operating system modules

Process management, process scheduling, inter-process communication mechanism, memory management, interrupt Exception Handling, file system, I/O system, and network part.

2. Objective of the Operating System

Manage hardware devices to provide a good execution environment for upper-layer applications.

Basic Structure of 3linux

 

Process description PCB

Broadly speaking, all process information is put in a data structure called a process control block, which can be understood as a set of process attributes. In linux, the structure is pseudo-task_struct.

Basic Information

Identifier: the unique identifier of a process. It is used to differentiate other processes.
Status: Task status, exit code, exit signal, etc.
Priority: the priority relative to other processes.
Program counter: the address of the next one of the commands to be executed in the program.
Memory pointer: Includes pointers to program code, process-related data, and memory blocks shared with other processes.
Context data: the data in the register of the processor when the process is running.
I/O status information: displays the list of I/O requests, I/O devices allocated to processes, and files used by processes.
Accounting information: may include the total CPU time, the total number of clocks used, the time limit, and the account name.

The data structure for saving process information is task_struct, which can be found in include/linux/sched. h. All processes running in the system are stored in the kernel in the form of a task_struct linked list.
You can view the process information in the/proc system file folder. To obtain the process information with PID 400, You need to view the/proc/400 file folder. Most process information can also be obtained using user-level tools such as top and ps.

Get process identifier

 

Process location (process address space)

Test

Obtain and modify Environment Variables

Here: when the third parameter of setenv is 0, if the environment variable exists, it will not be modified and will be returned directly. If it is not 0, it is modified if it exists.

Process status

Static const char * const task_state_array [] = {
"R (running)",/* 0 */
"S (sleeping)",/* 1 */
"D (disk sleep)",/* 2 */
"T (stopped)",/* 4 */
"T (tracing stop)",/* 8 */
"X (dead)",/* 16 */
"Z (zombie)",/* 32 */
};

S is an interruptible sleep that can be awakened. D is an uninterruptible sleep that can only be stopped when you wake up.

Zombies is a special status. When the process exits and the parent process (using wait () system call
When the code returned from the sub-process exit is not read, a dead process is generated. The zombie process will remain in the terminated state.
In the progress table, and will always wait for the parent process to read the exit code.

The basic switching rule between processes sends a signal to the process to change the status.

Command kill, killall

Kill-l get the specific command number and information

Use man kill

In addition, the ps pstree top command is used to view the process status.

Process Priority

In the ps column

PRI: indicates the priority of the executable row of the process. The smaller the value, the earlier the execution.

NI: indicates the nice value of the process.

PRI is also easy to understand, that is, the priority of the process, or the general point is the order in which the program is executed by the CPU. The smaller the value, the higher the priority of the process.

Nice value, which indicates the value of the priority to which the process can be executed.

PRI (new) = PRI (old) + nice

 

Note: here the nice value is the priority of the running program, so the reference point is the default value of pri. Instead of the current value.

Process Execution

The process memory layout is divided into four different segments:
• Text Segment, containing the program's source commands.
• Data Segment, including static variables.
• Heap, dynamic memory partition area.
• Stack, dynamically growing and shrinking segments, saving local variables.

Zombie Process: A child process exits when its parent process does not call wait () or waitpid. This sub-process is a zombie process. If the parent process still exists and does not call wait, the zombie process cannot be recycled until its parent
After the process exits, the process will be recycled by init.

Orphan process: if a parent process exits and one or more child processes are still running, those child processes will become orphan processes. The orphan process will be adopted by the init process (process number 1) and collected by the init process.

Process Termination
There are eight methods for terminating a process. The first five methods are for terminating a process normally, and the last three methods are for terminating an exception:
1 returned from the main function;
2. Call the exit function;
3 call with _ exit or _ Exit;
4. The last thread is returned from the startup routine;
5. Use pthread_exit to call the last thread;
6. Call the abort function;
7. receive a signal and stop it;
8. The last thread responds to the cancellation request.
(1) exit Function
# Include <stdlib. h>
Void exit (int status );
Void _ Exit (int status );
# Include <unistd. h>
Void _ exit (int status );

Exit () to clear the corresponding resources and then return to the kernel. The other two directly return to the kernel.

The returned results of exit () and return are the same.

2) atexit Function
# Include <stdlib. h>
Int atexit (void (* fun) (void ));

It is used to register the running method at the end of the program, similar to the sethandler mechanism or playing the host machine.

In addition, the registration order is opposite to the execution order. Similar to the structure 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.