Process operation under Linux, creation and control of----process

Source: Internet
Author: User
Tags terminates

---restore content starts---

A process is a process that a program executes at a time and is the basic unit of dynamic execution of the operating system.

The concept of the process is mainly two points: first, the process is an entity. Each process has its own virtual address space, including the text area, data area, and stack area. The text area stores the code executed by the processor, the data area stores variables and dynamically allocated memory, and the stack area stores the instructions and local variables that are called by the active process. Second, the process is an "executing procedure", which is fundamentally different from the program. The program is static, it is an ordered set of instructions stored on disk, and the process is a dynamic concept, it is a running program, contains the process of dynamic creation, scheduling and extinction process, is the basic Linux scheduling unit. Only when the processor is given a program life can it become an active entity called a process.

The kernel scheduler replicates the CPU execution time between all the processes, also known as the time slice, which takes turns to grab control from the process after the time slices that each process has been allocated.

Memory layout of the process:

Data area: A static variable used by the program.

Heap: Programs can dynamically allocate additional memory from this area.

Stack: A piece of memory that increases or decreases as a function is called, returned, to allocate storage space for local variables and function call link information.

Process ID:

Operating system OS assigns each process a unique integer ID, which is the identification number (PID) of the process. Process 0 is a scheduling process, which is often used as a switching process, it does not execute any program, is part of the kernel, and therefore is also a system process. The process has a parent process ID (ppid)in addition to its own ID. In other words, each process must have its parent process, and the operating system does not produce a new process for no reason. The ancestor process for all processes is the same process, which is called the init process, and the ID of the 1,init process is the first boot process after the kernel bootstrap. The INIT process is responsible for booting the system, starting the daemon (background) process, and running the necessary programs. It is not a system process, but it runs with system super User privileges.

The PID unique identification process is obtained by the following two functions:

pid_t getpid (void)//pid (itself)

pit_t getppid (void)//ppid (parent process)

First, you can use the PS command to query the running process;

such as: Ps-eo pid,comm,cmd command.

(-e means list all processes,-o pid,comm,cmd means we need pid,command,cmd information)

The user ID of the process and the group ID (the running identity of the process)

The process must have a user-like identity in order to control permissions on the process, by default, which logged-on user

Running the program, the program process has the identity of that user.

For example, assuming that the current logged-on user is Gotter, and he runs the LS program, LS has a gotter identity during the run, and the user ID and group ID of the LS process are gotter and gotter, respectively. This type of ID is called the real user ID of the process and the real group ID. Real user IDs and real group IDs can be obtained through functions Getuid () and Getgid ().

Corresponding to the real ID, the process also has a valid user ID and a valid Group ID property, and when the kernel accesses the process, it examines the process's valid user ID and valid group ID, not the real user ID and the real group ID. By default, the user (valid user ID and valid group ID) is the same as (real user ID and real group ID). Valid user IDs and valid group IDs are obtained through functions Geteuid () and Getegid ().

The ID command can display a true valid user ID (UID) and group ID (GID). The UID is a single identity for a user. The group ID (GID) corresponds to multiple UID.

Status of the process

A process is the process of executing a program, which can be divided into 3 states according to its life cycle, as shown in.

Execution state: The process is running, that is, the process is consuming CPU.

Ready state: The process already has all the conditions for execution and is waiting to allocate CPU processing time slices.

Wait state: The process cannot use the CPU and can wake up if it waits for the event to occur (the resource that is waiting to be allocated).

Process-related command actions:

Nice command : run processes by user-specified priority:

The nice command is used to set the priority, the priority value is -20~19, where the lower the priority, the higher the value the lower the priority, the highest priority of 20, and the lowest priority of 19. It is important to note that the normal user can only adjust the application's priority value between 0~19, only the superuser has the right to adjust the higher priority value (from -20~19).

Nice [-n < priority;] [--help] [--version] [execution instructions] options are described:

-N < priority > assign priority;

--help Help information;

--version version information;

View process Details

Ps-l Printing results:

F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD
0 S 12866 12864 1 0-1681-pts/0 00:00:00 Bash
0 R 12884 12866 1 0-1619-pts/0 00:00:00 PS

Some of these important information are:

UID: Representing the identity of the performer

PID: Represents the code for this process

PPID: Represents which process is derived from the process, that is, the name of the parent process

PRI: Represents the priority that the process can be executed, and the smaller the value, the sooner it is executed

NI: The nice value representing the process

The PRI is the priority of the process, and the smaller the value, the higher the priority of the process. And NI, the nice value that we're going to say, is set by the Nice command, which represents the number of priority corrections that a process can perform. As mentioned earlier, the smaller the PRI value, the faster it is executed, then the addition of the Nice value will make the PRI:pri (new) =pri (old) +nice. Therefore, the priority of the Nice command is not the final priority of the program, but only the corrected value of the priority.

The Renice command allows the user to modify the priority of a running process.

Usage: renice Priority [[-P] PID] [[-G] pgrp ...] [[-u] User name ...]

Example, fix the priority of the PID28390 process to 10:

[Email protected] test]# Renice 10 28390

28390:old priority 0, new priority 10

Viewing and switching of running processes in the background

The N objs command looks at the process running in the background.

Example:

[Email protected] test]#./a.out &

[1] 28607

The PID number that prompts the process to run

[[email protected] test]# jobs

[1]+ Running

./a.out &

Processes running in the background

The 1 here indicates the operating code of the background program.

When the program is running in the foreground, pressing CTRL + Z can put the program into the background and pause it.

FG moves commands in the background to the foreground to continue running.

BG changes a command that is paused in the background to continue execution.

Kill process KillCommand

The PID number of the process needs to be obtained before killing the process, which can be obtained using the PS command, which is the unique identifier of the process. Example:

[[Email protected] test]# PS

PID TTY Time CMD

27810 pts/0 00:00:00 su

27960 pts/0 00:00:00 Bash

28709 pts/0 00:04:18 a.out

General usage:

Kill PID Number

kill-9 PID number to kill the process can add 9 parameters to force kill

To view the status of a process:

Ps-aux: View the status of each process, including the status of Run ready wait.

Ps-aux| grep ' AA ': Find the specified (AA) process.

PS-EF: View information such as pid,ppid for all processes.

Ps-aux See%CPU (CPU Usage)%MEM (memory usage) PS View

Process creation and control:

Fork function pid_t fork (void);

In a Linux environment, the creation process uses the fork function.

The fork function in Linux is a very important function that creates a new process from the existing process. The new process is a child process, and the original process is the parent process.

The ORK function is called once and returns two times, with three different return values:

1. In the parent process, fork returns the PID number of the newly created child process

2, in the sub-process, fork returns 0;

3. If an error occurs, fork returns a negative value.

The fork function creates the process of the child process:

The child process that is obtained by using the fork function is a replica of the parent process that inherits the address space of the process from the parent process, including the process context, process stack, memory information, open file descriptor, signal control setting, process priority, process group number, current working directory, root directory, resource limit, control terminal, The child process is unique only to its process number, resource usage, and timers. After the child process is created by this replication, both the original process and the child process are returned from the function fork, and each continues to run down. In other words, fork not only replicates the resources of the process, but also replicates the running state of the original process, so the new process (subprocess) that is copied is the same as the parent process, but it runs from behind the fork function. However, the fork return value of the original process is different from the fork return value of the child process, in the original process, fork returns the PID of the child process, while in the child process, fork returns 0, if fork returns a negative value, the creation of the child process fails.

vfork function pid_t vfork (void);

Vfork (Building a new process)

Vfork () produces a new subprocess. However, the child process created by vfork shares the data segment with the parent process, and the child process created by Vfork () will run before the parent process.

Vfork () usage is similar to fork (), but there are differences, and the specific difference boils down to the following points:

Fork (): The child process copies the data segment of the parent process, the code snippet. Vfork (): The child process shares the data segment with the parent process.

Fork (): The order of execution of the parent-child process is indeterminate.

Vfork (): Guarantees that the child process runs first, shared with the parent process data before calling exec or _exit, and that the parent process may be scheduled to run after it calls exec or _exit.

Vfork () guarantees that the child process runs first, and the parent process may be scheduled to run after she calls exec or _exit. If the

The child process relies on further actions of the parent process before invoking these two functions, which results in a deadlock.

4 When you need to change the value of a variable in a shared data segment, copy the parent process.

* * Now, many of the embedded Linux system fork function calls are implemented using the Vfork function. In fact uClinux all of the multi-process management is implemented through Vfork.

Process termination

There are 5 ways to terminate a process:

The natural return return0 of the main function;

Call the Exit function

Call the _exit function

Receive a signal. such as CTRL + C SIGINT, ctrl+\ sigquit

Call the Abort function, which produces a SIGABRT signal, so it is a special case of the previous method.

The first 3 methods are normal termination, and the latter 2 are abnormal termination. However, either way, the process terminates with the same closed file, freeing up resources such as memory usage. Just the latter two terminations will cause some code in the program to not execute properly.

The exit and _EXIT functions are used to terminate the process. When the program executes to exit and _exit, the process unconditionally stops all remaining operations, clears the various data structures including the PCB, and terminates the operation of the program. But they are different.

The most important difference between the Exit function and the _exit function is that the Exit function checks the opening of the file before exiting, and writes the contents of the file buffer back to the file, which is the "clean I/O buffer" in the diagram.

Because of the standard library of Linux, there is an operation called "Buffered I/O", which is characterized by a buffer in memory for each open file. Each time a file is read, a number of records will be read sequentially, so that the next time you read the file can be read directly from the memory buffer, as well, each time you write a file, it is only written in memory buffer, and so on to meet certain conditions (such as a certain number or encountered a certain character), The contents of the buffer are then written to the file once. This technique greatly increases the speed of file read and write, but it also brings trouble to programming. For example, there are some data that have been written to the file, in fact, because they do not meet the specific conditions, they are only in the buffer, then use the _exit function to directly shut down the process, the data in the buffer is lost. Therefore, we recommend that you use the exit function if you want to ensure the integrity of your data.

N Exit and prototype of the _EXIT function:

#include <stdlib.h>//exit header #include <unistd.h>//_exit header file

void exit (int status); void _exit (int status);

Status is an integer parameter that can be used to pass the state at the end of the process. Generally speaking, 0 means the normal end; other values indicate an error and the process is not properly terminated.

Note: the ' \ n ' line break is the terminator of the buffer

Orphan process

When you start a child process with the fork function, the child process has its own life and will run independently.

If the parent process exits before the child process, the child process becomes an orphan process, which is automatically taken over by the PID 1 process (that is, init). After the orphan process exits, its cleanup work is automatically processed by the ancestor process init. However, before the INIT process cleans up the child process, it consumes the resources of the system, so try to avoid it.

Zombie Process

If the child process exits first, the system does not automatically clean up the child process's environment, and the parent process must call the wait or Waitpid function to complete the cleanup, and if the parent process does not clean up, the child process that has exited becomes a zombie process (defunct), If there are too many zombie (zombie) processes in the system, it will affect the performance of the system, so the zombie process must be processed.

Wait and Waitpid both pause the parent process, wait for a child process to exit, and perform cleanup work;

The wait function randomly waits for a child process to exit and returns the PID of the subprocess;

Waitpid waits for a child process that specifies PID to exit, or 1 to wait for all child processes to return the PID of the child process.

The status parameter is the outgoing parameter, which saves the child process's exit code to the variable that the status points to

The options are used to change the behavior of waitpid, the most common of which is Wnohang, which indicates that the execution of the caller will not be suspended, regardless of whether the child process exits immediately.

Waitpid (PID, &n, 0), where n is the integer variable that was previously defined.

The following two macros are typically used to obtain status information:

Wifexited (N): If the child process ends normally, it takes a value other than 0.

Wexitstatus (n): If wifexited nonzero, it returns the exit code of the child process, the value of the parameter of the exit () function of the child process, or the value returned by return.

View system all environment variables

You can use the ENV command to view environment variables.

exec function Family

the composition of the EXEC function family

extern char **environ; int execl (const char *path, const char *arg, ...); int EXECLP (const char *file, const char *arg, ...); int execle (const char *path, const char *arg, ..., char * const envp[]);

int execv (const char *path, char *const argv[]); int EXECVP (const char *file, char *const argv[]); int Execve (const char * path, char *const argv[], char *const envp[]);

The role of the EXEC function family is to run the executable program specified by the first parameter. But its working process is completely different from fork, fork is copying a copy of the original process, and the EXEC function executes the executable program specified by the first parameter, after the new program is running, it is also a process, and this process will overwrite the original process space, That is, all the contents of the original process are completely overwritten by the newly run process, so all the code behind the EXEC function is no longer executed, but its previous code is, of course, executable.

Path is the absolute path that includes the execution file name, file is either an absolute path or an executable file name

ARG is the full command-line argument of an executable file, which can be used more than one, noting that the last parameter must be NULL. (For example: "LS", "-l", NULL)

ARGV is an array of strings char *argv[]={"full path", "param1", "param2",... NULL};

ENVP Specifies the environment variable for the new process char *envp[]={"Name1=val1", "Name2=val2",... NULL};

There are two ways to pass the parameters of the EXEC function family: One is enumerated in a single way, and the other is to pass all parameters to an array of pointers. Here is the 5th letter of the function name to differentiate, the letter "L" (list) is enumerated in a way, its syntax is char *arg, the letter "V" (vertor) is the representation of all command line parameters of the whole construction pointer array, its syntax is char *const argv[ ]。

The function that ends with the letter P finds the path to the executable file for the new program by searching the environment variable for the system path. If an executable is not in the path defined by path, we need to pass the file name with the absolute path, including the directory, as a parameter to the function.

For a function call with parameter ENVP, which ends with the letter E, the function passes the ENVP to pass the string array as the environment variable for the new program.

The global variable in the new process environ the array of environment variables that the pointer points to will be replaced by the contents of the ENVP.

Note: For a function with parameter envp, it will use the programmer's custom environment variable, if the custom environment variable contains the path of the executable program that will be executed, then is it possible for us to write the full path in the first parameter? No, the full path must be written. Because

The environment variables that we customize are not used to look for this executable program, but to be used by the new process after the executable program is up and running.

You can use the env command to view environment variables.

---restore content ends---

Process operation under Linux, creation and control of----process

Related Article

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.