Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the Linux process

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to analyze the Linux process, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

It just briefly describes the basic concepts of Linux, illustrates the basic Linux application through a few examples, and then ends with the basic kernel construction of Linux. So let's take a closer look at the Linux kernel to understand the basic concepts of Linux-processes and threads. The system call is the interface of the operating system itself, which is important for creating processes and threads, memory allocation, file sharing, and Icano.

We will discuss it from the commonness of each version.

Basic concept

A very important concept of Linux is the process, the Linux process and what we see in the

Processes and threads written for busy people

The process model discussed in is very similar. Each process runs a separate program and has a separate control thread at initialization. In other words, each process has its own program counter, which is used to record the next instruction to be executed. Linux allows the process to create additional threads at run time.

Linux is a multiprogramming system, so there are independent processes running at the same time. In addition, each user has several active processes at the same time. Because if it is a large system, there may be hundreds of processes running at the same time.

In some user spaces, even if the user logs out, there are still some background processes running, called daemons (daemon).

There is a special type of daemon in Linux called the scheduling daemon (Cron daemon), which wakes up every minute to check if there is work to do, and then goes back to sleep until the next wake up.

"Cron is a daemon that can do whatever you want, such as regular system maintenance, regular system backups, etc. There are similar programs on other operating systems, such as the Cron daemon on Mac OS X, which is called launchd. On Windows, it can be called scheduled task (Task Scheduler).

In Linux systems, processes are created in a very simple way, and the fork system call creates a copy (copy) of the source process. The process that calls the fork function is called the parent process (parent process), and the process created using the fork function is called the child process process. Both the parent and child processes have their own memory images. If the parent process modifies some variables after the child process is created, then the child process cannot see these changes, that is, after the fork, the parent process and the child process are independent of each other.

Although the parent process and child process remain independent of each other, they can share the same file. If the parent process opened a file before fork, the parent process and child process still share the open file after fork. Changes to the shared file are visible to both the parent and child processes.

So how do you distinguish between parent and child processes? Child processes are just copies of the parent process, so they are almost the same in almost all cases, including memory images, variables, registers, and so on. The key to distinguish lies in the return value after the fork function call. If a non-zero value is returned after fork, the non-zero value is the process identifier (Process Identiier, PID) of the child process, and a zero value is returned to the child process, which can be represented by the following code

Pid = fork (); / / call the fork function to create the process if (pid

< 0){ error() // pid < 0,创建失败 } else if(pid >

0) {parent_handle () / / parent process code} else {child_handle () / / child process code}

The parent process gets the PID of the child process after the fork, and this PID represents the unique identifier of the child process, that is, PID. If the child process wants to know its own PID, it can call the getpid method. When the child process ends running, the parent process will get the PID of the child process, because a process will fork many child processes, and the child process will also fork the child processes, so PID is very important. The process after the first call to fork is called the original process, and an original process can generate an inheritance tree.

Linux interprocess communication

The communication mechanism between Linux processes is usually called Internel-Process communication,IPC. Let's talk about the communication mechanism between Linux processes. Generally speaking, the communication mechanism between Linux processes can be divided into six types.

Let's give an overview of them respectively.

Signal signal

Signal is the first inter-process communication mechanism used in UNIX system, because Linux inherits from UNIX, so Linux also supports signal mechanism, which is realized by sending asynchronous event signals to one or more processes, the signal can be generated from the keyboard or access to a location that does not exist, and the signal sends the task to the child process through shell.

You can type kill-l on the Linux system to list the signals used by the system. Here are some of the signals I provided.

Processes can choose to ignore incoming signals, but there are two that cannot be ignored: SIGSTOP and SIGKILL signals. The SIGSTOP signal notifies the currently running process to perform a shutdown operation, and the SIGKILL signal notifies the current process that it should be killed. In addition, the process can choose the signal it wants to process, and the process can choose to block the signal. If it does not block it, it can choose to handle it itself, or it can choose to do kernel processing. If you choose to leave it to the kernel for processing, the default processing is performed.

The operating system will interrupt the process of the target program to send a signal to it. In any non-atomic instruction, execution can be interrupted. If the process has registered a new number handler, then the process will be executed. If it is not registered, it will be handled by default.

For example, when a process receives a signal of a SIGFPE floating-point exception, the default action is to dump and exit it. The saying that the signal has no priority. If two signals are generated for a process at the same time, they can be presented to the process or processed in any order.

Let's take a look at what these signals are for.

SIGABRT and SIGIOT

SIGABRT and SIGIOT signals are sent to the process to tell it to terminate, which is usually started by the process itself when the abort () function of the C standard library is called

SIGALRM 、 SIGVTALRM 、 SIGPROF

When the set clock function times out, SIGALRM, SIGVTALRM, SIGPROF will be sent to the process. SIGALRM is sent when the actual time or clock time expires. A SIGVTALRM is sent when the CPU time used by the process times out. A SIGPROF is sent when the CPU time used by the process and the system on behalf of the process times out.

SIGBUS

SIGBUS will send to the process when it causes a bus interrupt error.

SIGCHLD

When a child process is terminated, interrupted, or resumed, the SIGCHLD is sent to the process. A common use of this signal is to instruct the operating system to clear the resources used by a child process after it terminates.

SIGCONT

The SIGCONT signal instructs the operating system to continue the process that was previously paused by the SIGSTOP or SIGTSTP signal. One of the important uses of this signal is in job control in Unix shell.

SIGFPE

The SIGFPE signal is sent to the process when performing incorrect arithmetic operations, such as dividing by zero.

SIGUP

When the terminal controlled by the SIGUP signal is closed, it is sent to the process. Many daemons will reload their configuration files and reopen their log files instead of exiting when this signal is received.

SIGILL

The SIGILL signal is issued when attempting to execute an illegal, malformed, unknown, or privileged instruction

SIGINT

When the user wants to interrupt the process, the operating system sends a SIGINT signal to the process. The user enters ctrl-c to interrupt the process.

SIGKILL

The SIGKILL signal is sent to the process to terminate it immediately. Compared with SIGTERM and SIGINT, this signal cannot be captured and ignored, and the process cannot perform any cleanup operations after receiving this signal. Here are some exceptions.

The zombie process cannot be killed because the zombie process is dead and is waiting for the parent process to capture it

A blocked process will not be dropped by kill until it is awakened again.

The init process is the initialization process of Linux, which ignores any signals.

SIGKILL is usually used as a signal to kill the process in the end, and it is usually sent to the process when the SIGTERM does not respond.

SIGPIPE

Sent to the process when SIGPIPE tries to write to the process pipe when it finds that the pipe is not connected and cannot be written

SIGPOLL

When an event occurs on an explicitly monitored file descriptor, a SIGPOLL signal is sent.

SIGRTMIN to SIGRTMAX

SIGRTMIN to SIGRTMAX is a real-time signal

SIGQUIT

When the user requests to exit the process and perform the core dump, the SIGQUIT signal will be sent to the process by its control terminal.

SIGSEGV

When the SIGSEGV signal makes an invalid virtual memory reference or a segmentation error, it is sent to the process when a segmentation violation is performed.

SIGSTOP

When SIGSTOP instructs the operating system to terminate for later recovery

SIGSYS

When the SIGSYS signal passes the error parameter to the system call, the signal is sent to the process.

SYSTERM

We briefly mentioned the term SYSTERM above, and this signal is sent to the process to request termination. Unlike the SIGKILL signal, the signal can be captured or ignored by the process. This allows the process to perform a good termination, freeing resources and saving state when appropriate. SIGINT is almost the same as SIGTERM.

SIGTSIP

The SIGTSTP signal is sent to the process by its control terminal to request the terminal to stop.

SIGTTIN and SIGTTOU

When SIGTTIN and SIGTTOU signals attempt to read or write from tty in the background, respectively, the signal is sent to the process.

SIGTRAP

When an exception or trap occurs, the SIGTRAP signal is sent to the process

SIGURG

When the socket has readable emergency or out-of-band data, the SIGURG signal is sent to the process.

SIGUSR1 and SIGUSR2

SIGUSR1 and SIGUSR2 signals are sent to the process to indicate user-defined conditions.

SIGXCPU

When the SIGXCPU signal runs out of CPU for more than a predetermined value that can be set by a user, it is sent to the process

SIGXFSZ

When the SIGXFSZ signal grows beyond the maximum allowable size of the file, the signal is sent to the process.

SIGWINCH

The SIGWINCH signal is sent to the process when its control terminal changes its size (window change).

Pipeline pipe

Processes in a Linux system can communicate by establishing a pipeline pipe

Between two processes, a channel can be established, into which one process writes a byte stream, and the other reads a byte stream from this channel. The pipe is synchronous, and when a process tries to read data from an empty pipe, the process is blocked until data is available. The pipeline pipelines in shell is implemented in pipelines, when the shell discovers the output

Sort

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report