UNIX Process Features

Source: Internet
Author: User
Tags stack trace

This article summarizes and shares the UNIX processes that are frequently dealt with in operation and maintenance. A program is a collection of code, and a process is generated by a running program. So what are the characteristics of the process? And see below, some of the classic and difficult to understand the place, using Python code implementation, can let the reader better understanding and memory.

Process Features
    • Processes all have identifiers

?? All processes running in the system have a unique process identifier, called a PID.

?? The PID does not convey any information about the process, it is simply a sequential character identifier. Process, the nuclear eye is just a number.

PID is a simple and generic description of the process, which is independent of the content of the process.

    • Processes have parent processes

?? Each process running in the system has a corresponding parent process. Each process is aware of its parent process identifier (PPID).

?? In most cases, the parent process of a particular process is the one that called it.

?? Example: Start the terminal and enter the bash prompt to execute the LS command. Then bash's parent process is the terminal process, and the parent process of LS is the bash process.

    • Processes have file descriptors

?? Unix philosophy points out that in the Unix world, everything is file. Devices, sockets, pipelines, etc. are all files.?? The file descriptor represents the resource. Whenever you open a resource, you get a file descriptor number (descriptor. File descriptors exist only in the process to which they belong, and are not shared between unrelated processes. When the process is finished, it is closed with other resources that the process has opened.

?? The allocation of the file descriptor number begins with the smallest value that has not been used. Once the resource is closed, the corresponding file descriptor number can be used again. File descriptors are used only to track open resources, and resources that have been closed are not file descriptors.

    • Process resource Limits

?? Then, the file descriptor represents the open resource, so how many file descriptors can a process have, that is, how many resources can you have? This is determined by the system configuration. The kernel imposes certain resource restrictions on the process. For example, the number of concurrent, the maximum number of files, process stack segment size.

    • Processes have an environment

?? An environment variable is a key-value pair (Key-value pairs) that contains process data. All processes inherit environment variables from their parent processes. They are set by the parent process and are inherited by the quilt process. Each process has an environment variable, and the environment variable is global to a particular process. (ENV)

    • Processes have parameters

?? A special array named argv can be accessed by all processes. Different programming languages may be slightly different in implementation, but there will be argv.

?? ARGV's full name: argument vector. In other words, argv is a parameter vector or an array. It holds the parameters that are passed to the current process on the command line.

    • The process has a name.

?? There is little inherent way for UNIX processes to learn about each other's state. There are two mechanisms that run at the process itself level that can be used to communicate. One is the process name and the other is the exit code.

?? Each process in the system has a name.

    • Process has an exit code

?? When the process is about to end, it has the last chance to leave its own message: Exit code. All processes are exited with a digital exit code (0-255) that indicates whether the program ends normally. By convention, the exit code is 0 for the process to end successfully, the other exit code represents an error, different exit codes correspond to different errors. You can use Exit to exit the shell script and specify the exit code. such as: Exit 1

    • Process can derive/derive process

?? Derivation (forking) is one of the most powerful concepts in UNIX programming. The fork (2) system call allows a running process to create a new process in a programmatic form. This new process is identical to the original process.

?? When it is derived, the process called fork (2) is called the parent process, and the newly created process is called the child process.

?? The child process inherits everything in its occupied memory from the parent process, and all open file descriptors that belong to the parent process.

?? A child process is a completely new process that has its own unique PID. The ppid of the child process is the PID of the process that calls Fork (2).

?? At fork (2) invocation, the child process inherits all the file descriptors from the parent process and obtains the number of the file descriptors of the parent process. This allows the parent-child process to share resources such as open files, sockets, and so on.

?? The child process inherits everything in the parent process memory, that is, the child process actually has a copy of the loaded memory code library. A child process can arbitrarily change a copy of its memory content without any effect on the parent process.

Fork Example:

Handetiandemacbook-pro:~ handetian$ Cat 1. PY#!/usr/bin/env pythonImportOSPrint '1:%s'%os.getpid ()ifos.fork ():Print 'This process is 2:%s'%os.getpid ()Else:  Print 'This process is 3:%s'%os.getpid () Handetiandemacbook-pro:~ handetian$ python 1. PY1:21,198This process is2:21,198This process is3:21,199

?? From the above example, a call to the fork method actually returns two times. Fork creates a new process. So it is returned once in the calling process (the parent process) and again in the newly created process (the child process). The code in the IF statement block is executed by the parent process, while the code in the Else statement block is performed by the child process. The child process exits after executing the ELSE statement block, and the parent process continues to run.

The output of the above code is related to the return value of the fork. In the parent process, the value returned by fork is the PID of the newly created child process, so the parent process executes the code in the IF statement block. In a child process, the value returned by fork is Nil,nil, so the child process executes the code in the Else statement block.

?? Based on the example above and the result of the return, the program code can (and cannot be guaranteed) be assigned to multiple CPU cores by generating a new process.

?? Fork (2) creates a new process that is identical to the old process. If a process that uses 1GB of memory is derived, then 2GB of memory is occupied. If the operation is repeated several times, the memory is quickly exhausted. This is the so-called fork bomb, so we must ensure that the execution of the program before executing the concurrency action!!!

    • Orphan process

?? In the above we have found that many things become less simple if the sub-process is involved.

?? Starting a single foreground process through the terminal, only the process outputs to stout, and the keyboard input ctrl-c can exit the program.

?? Once the process has spawned the subprocess, how can we determine whether CTRL-C will end the parent or child process, or all of it? Understand that this principle can avoid the generation of orphan process.

?? What happens to the child process when the parent process is finished? The child process runs unharmed because the operating system does not discriminate between the sub-processes.

?? Who takes over the child (orphan) process after the parent process is finished? In a Linux system, the orphan (child) process is taken over by the Init (1th) process. In fact, the daemon process is also the orphan process.

    • The process is friendly

?? In the above we talked to fork (2) Creating a child process that is exactly the same as the parent process. It contains all the contents of the parent process in memory. If you do copy all of the data, the system overhead can be large, and modern UNIX systems use a write-time copy (Copy-on-write, CoW) mechanism to solve the problem. That is, the parent and child processes are actually content in shared memory, and only one of the processes needs to modify the data to process memory replication, which is a certain isolation between the different processes.

    • Process can wait

?? The sub-processes we used to derive from fork (2) are all at the same time as the parent process, so there is no problem if we want the child process to handle the transaction asynchronously. In this case, if the parent process exits first, the child process becomes an orphan process. So can the parent process wait for the child process to exit before exiting? The answer is yes, you can wait for the child process to finish by process.wait or Process.waitpid.

?? Example:

handetiandemacbook-pro:~ handetian$ Cat 1. PY#!/usr/bin/env pythonImportOs,timePrint 'This process is 1:%s'%os.getpid () PID=os.fork ()ifPID:Print 'This process is 2:%s'%os.getpid ()#os.wait ()Sleep (5)  #print "Prcess 1 is died!!!"Else:   forIinchXrange (10): Time.sleep (1)    Print 'This process is 3:%s'% Os.getpid ()

In the example above, add a comment (#os. Wait, #print ...), and the effect is that the parent and child processes are executed synchronously, and the child process continues to execute after the parent process exits. Dismiss the comment (#os. Wait, #print ...), the parent process waits for the child process to finish, then exits, and prints the process is died!!!

    • Zombie Process

?? The kernel retains state information for the child process that has exited until the parent process uses process.wait to request the information. If the parent process has not made a request, then the state information is retained by the kernel, resulting in a waste of kernel resources.

?? Example:

handetiandemacbook-pro:~ handetian$ Cat 1. PY#!/usr/bin/env pythonImportOs,timePrint 'This process is 1:%s'%os.getpid () PID=os.fork ()ifPID:Print 'This process is 2:%s'%os.getpid () while1: Time.sleep (1)Else:  Print 'This process is 3:%s'% Os.getpid ()

?? Execution Result:

handetiandemacbook-pro:~ handetian$ python1. PY &[3]21626Handetiandemacbook-pro:~ handetian$ This process is 1:21626This process is 2:21626This process is 3:21627Handetiandemacbook-pro:~ handetian$ ps-ho pid,state-p21626PID STAT21626Shandetiandemacbook-pro:~ handetian$ ps-ho pid,state-p21627PID STAT21627Z

The sample code is the fork (2) of a child process, which exits after the child process executes and the parent process sleeps. By executing the input at the result, you can see the parent process S (sleep), child process Z (zombie). This is the result of the parent process not reading the status information of the child process. The corresponding workaround is for the parent process to exit the read in the child process

    • Process can get signals

?? The parent process above can supervise the child process through process.wait. But Process.wait is a block call, and the call will not be returned until the child process has finished. In reality, the parent process cannot always have time to wait for the child process to end. So, the following will be the UNIX signal. What's the signal? A signal is an asynchronous communication. The process can connect to the received information to do the following: 1. Ignore the signal 2. Perform a specific action 3. Perform the default action

?? Where does the signal come from? Signals are sent by one process, passed through the kernel, and sent to another process. The kernel is the intermediary for sending signals.

?? Common signal Operation:

???? Term---indicates that the process will end immediately

???? Core--Indicates that the process ends immediately and cores are dumped (stack trace)

???? IGN----indicates that the process ignores the signal

???? Stop---indicates that the process will stop running (paused)

???? Cont, indicates that the process will resume running (continue)

?? Commonly used signals:

???? SIGHUP 1 term

???? SIGINT 2 Term

???? Sigquit 3 Core

???? Sigkial 9 Term

???? SIGTERM

???? SIGUSR1 30,10,16 Term #自定义信号

???? SIGUSR2 31,12,17 Term # custom signal

???? SIGSTOP 17,19,23 Stop #该信号不能被捕捉, blocking or ignoring.

    • Processes can be interconnected

?? There are many ways to implement interprocess communication (IPC), the two most common: pipes (pipe) and sockets (socket pairs)

?? A process opens a Wirte pipeline, a process opens the Read pipeline, and the REA pipeline receives the data until an EOF flag is received to stop receiving. Pipeline communication is one-way.

?? IPC is the communication between processes running on the same machine, and process communication between different machines needs to be implemented using TCP sockets.

UNIX Process Features

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.