Fully Understand Linux Process Technology

Source: Internet
Author: User
Tags sleep function

When you enable your computer, you will find that the operating system is essential. Microsoft accounts for a large part of the operating system, but Linux has also caught up with Microsoft in recent years. Linux is a multi-user and multi-task operating system. Here we will introduce the knowledge of Linux, so that you can learn how to use Linux. Today I will talk about Linux Process technology. I hope you will remember the Linux Process Technology.

I. process attributes

Process Definition: A process is an execution process of a program, and the program is static. It is a set of executable code and data stored on the disk, A process is a dynamic concept. It is the basic scheduling unit of the Linux system. A process consists of the following elements:

1. The read context of the program, which indicates the status of the program reading and executing.
2. Current execution directory of the program.
3. files and directories of program services
4. program access permissions.
5. Memory and other system resources allocated to the process.

The most well-known attribute in a Linux Process is its process ID (PID) and its parent Process ID (PPID ). PID and PPID are both non-zero positive integers. A pid uniquely identifies a process. A process creates a new process called a child process ). On the contrary, the process that creates a child process is called the parent process. All processes trace their ancestor and eventually fall into the process with process number 1, which is called the init process. Is the first process started after kernel self-lifting. The init process plays the role of ending the parent process. Because the init process will never be terminated, the system can always be sure of its existence and take it as a reference when necessary. If a process is terminated before the end of all its child processes, the init must be used as a reference. At this time, all child processes that have lost their parent processes will use init as their parent processes. If you execute the ps-af command, you can list many processes with the parent Process IDParent Process ID and PPID) 1. Linux provides a pstree command that allows you to view the inheritance relationships between running processes in the system. Enter pstree directly in the command line. The program will list the inheritance relationships between processes running in the system in a tree structure.

2. Three states in which a process is running:

Running status: the CPU is being executed, that is, the process is occupying the CPU.
Waiting status: all conditions for the execution of a process, Waiting for the CPU to be allocated.
Stoped status: the process cannot use the CPU.
The kernel uses processes to control access to CPU and other system resources, and uses processes to determine the program running on the CPU, how long it will run, and what features it uses to run it. The kernel scheduler is responsible for allocating CPU execution time among all processes, called time slice. It takes turns to get control from the process after the time slice of each process is used up. If the processor is faster, you will think that the time slice is very small, as if several processes are running at the same time. When the Linix operating system is started, the system automatically creates system processes such as init to manage memory resources and schedule user processes. A process is dynamic. A program can run multiple processes simultaneously or simultaneously. The advantage of executing multiple processes at a time is that you can send emails while listening to music.

3. Understand the process structure in Linux

A process in Linux has three parts of data in the memory: "data segment", "Stack segment", and "code segment". Based on the I386 compatible central processor, these three register segments are available to facilitate the operation of the operating system.

Code segments are data that stores program code. If several processes on the machine run the same program, they can use the same code segment. The data segment stores the global variables, constants, and dynamic data space of the program. The stack segment stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. The stack segment includes the process Control Block in the Worker Process Control Block. The PCB is at the bottom of the Process core stack and no additional space is allocated.

Iv. Types of Linux Processes

The Linux operating system includes three different types of processes, each of which has its own characteristics and attributes.
A) interaction process-a process started by a shell. Interactive processes can run either on the foreground or on the background.
B) Batch Processing Process-this process is not associated with the terminal and is a process sequence.
C) monitoring process (daemon process)-a process started at Linux Startup and run in the background.

V. Create a Linux Process

In Linux, the system call that generates a new process is the fork function. This function name indicates "Forks" in English. Why is this name used? Because a process is running, if fork is used, another process is generated, so the process is "Forked", so this name is very good. Fock Syntax:
# Include pid_t fork ();
Note: The system calls a new process, called a sub-process, and the calling process is called a parent process.
Fork () system calls are often used in Linux network programming. For example, in a network environment built by a client/Web server, the Web server can often meet the requirements of many clients. If a client needs to send a request to access the Web server, the server generates a parent process, and then the parent process generates a child process by calling the fork () system, at this time, the client request is completed by the sub-process. The parent process can return to the waiting status to continuously serve other users.

There is a simpler function system for executing other programs. The string parameter is passed to a command interpreter (generally sh) for execution, that is, string is interpreted as a command, sh executes the command. if the string parameter is a null pointer, check whether the command interpreter exists. this command can be in the same form as the command in the same command line, but because the command is placed as a parameter in the system call, you should pay attention to the processing of special characters during compilation. command search is defined according to the PATH environment variable. the consequences of command generation generally do not affect the parent multi-process programming. return Value: When the parameter is a null pointer, the return value is non-zero only when the command interpreter is valid. if the parameter is not a null pointer, the return value is the return value of the return state (same as waitpid () of the command. if the command is invalid or the syntax is incorrect, a non-zero value is returned. The executed command is terminated. -1 is returned in other cases. it is a high-level function. In fact, it is equivalent to executing a command under Shell. In addition to the system, the system calls exec to execute an executable file to replace the execution image of the current process. The system calls the exit function to terminate the calling process. Sleep function call is used to specify the number of seconds for a process to be suspended. The wait function family is used to wait for and control processes. The poppen function is similar to the system function. The difference is that the output is processed in pipelines.

6. Process Management

The best way to manage processes in Linux is to use system commands under the command line.

1. Start the process
Enter the program name of the program to be run and execute a program. In fact, a process is started. In Linux, each process has a process ID (PID), which is used to identify and schedule processes. There are two main ways to start a process: Manual start and scheduling start. The latter is set in advance and started according to user requirements. If you run a command to directly start a process, you can manually start the process. However, manual process startup can be divided into many types. The actual results vary depending on the type and nature of the started process.
1) Start the foreground
Is the most common way to manually start a process. Generally, if you type a command "df", a process has been started and is a foreground process. At this time, the system is already in a multi-process state. Many processes running in the background and automatically started at system startup are quietly running. Some users enter the "df" command and use "ps-x" to view it. However, they do not see the df process, which is strange. In fact, this is because the process of df is too fast to end. When you use ps to view it, the process has ended. If you start a time-consuming process, such as running: find under the root command, and then use ps aux to view it, you will see a find process in it.
2) Background startup
Manual start of a process from the background is rarely used, unless the process is time-consuming and the user is not in a rush to get results. Assume that you want to start a process that requires a long time to run to format text files. To prevent the entire shell from being paralyzed during the formatting process, it is wise to start the process from the background.

2. Process Scheduling
When a foreground process needs to be interrupted, the Ctrl + c key combination is usually used. However, a background process cannot be solved by a key combination. In this case, the kill command must be used. this command can terminate background processes. there are many reasons for terminating the background process, maybe the process takes too much CPU time, or the process has crashed. this situation often occurs. The operating principle of the Kill command is to send a system operation signal and the process ID of a program to the Linux kernel, and then the system kernel can operate the process specified by the process ID number.
In addition to kill, the commands involved in the processes in Linux include the at: at command to execute the specified command sequence at the specified time; the cron: cron command to do repetitive work; and the crontab command: the crontab command is used to install, delete, or list the task tables used to drive cron background processes. Ps: The ps command mainly displays the processes in the system. The top command has the same basic functions as the ps command and displays the current processes and other States of the system; however, top is a dynamic display process, that is, you can press the keys to continuously refresh the current status. Nice: the nice command is run by the process according to the priority specified by the user. Renice: The renice command is used to change the priority of a running process.

The preceding section describes the Linux Process Technology.

  1. Linux virtualization technology Xen virtualization beginner's Guide
  2. Handling Linux kernel security details
  3. Basic Shell programming syntax in Linux
  4. How to program and run a Shell program in Linux
  5. Linux Shell programming bash Internal commands

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.