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 realize the Terminal and process of Linux

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize the terminal and process of Linux". In the daily operation, I believe that many people have doubts about how to realize the terminal and process of Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to realize the terminal and process of Linux". Next, please follow the editor to study!

The ordinary process of Linux (except the daemon) is the child process of the terminal, and the existence of the process depends on the terminal to provide space for it, including standard input, standard output and standard error. For example, start a terminal connected to linux on the client side of ssh, run the pstree command, and you can see the following result:

You can see that pstree is equivalent to a child process started by the current terminal.

Then, we enter the sleep 2000 & command to let the terminal start a sleep process running in the background, and then run pstreee

As you can see, a process with a pid of 1354 is started, and then the parent process of the process is sshd, which is the terminal started by the ssh service

Then, under / proc/1354/fd, you can see the following:

0, 1 and 2 refer to standard input, standard output and standard error, respectively, and they all point to / dev/pts/0

The pts here refers to: pseudo-terminal slave, which is an implementation of the virtual terminal, and 0 is the number of the virtual terminal. You can type ll / dev/pts/ to see the result:

You can see that there are three virtual terminals running in the / dev/pts directory.

Now go back to the terminal interface on the virtual machine, that is, the terminal on system X, and run a sleep in the background, and then go to the fd directory corresponding to this process, and you can see the following results:

The standard input, standard output, and standard errors here point to / dev/tty1,tty is the real terminal, and pts is virtual to it.

While the process is running (even the process running in the background), when the terminal exits, the process will receive the SIGHUP signal. If the program does not capture and process this signal, the process will exit by default. Take the sleep running in the background in the virtual terminal as an example:

This is the case where sleep is running, and when I shut down its terminal, it looks like:

One bash is missing, and sleep is missing.

Because the virtual terminal of a ssh is closed, a bash is naturally missing, and the sleep, which is the terminal process, exits at the same time.

So how do you make a program continue to work after the terminal exits?

First of all, a clear idea is that the parent process of the process to be started can be set to init, so that no matter how the current terminal, it will not affect the operation of the process. You can do this:

Setuid ping localhost > / dev/null &

Or

(ping localhost > / dev/null &)

As you can see, ping directly becomes a child process of init.

Alternatively, it can be done with the nohup command.

At this point, the study on "how to realize the terminal and process of Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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