From windows to * nix (3) multitasking

Source: Internet
Author: User

We know that windows is a multi-task operating system, and Tasks running at the same time can be switched at will, as well as the task manager, as well as * nix. You will not be able to execute only one program at a time and wait until the program ends before executing the next one. Let's take a look at how to do this:

 

Start the background

Find an example that takes a long time to illustrate the problem:

$ Sleep 10 <br/> (10 seconds later) <br/> $ 

This is the scenario where the program runs in the foreground. If you can't wait, you can run it in the background during execution:

$ Sleep 10 & <br/> [1] 31415 <br/>$ <br/> [1] + done sleep 10 <br/> $ 

Add a "&" symbol to the end of the command. At this time, shell will give you a job number and process ID, indicating that the task has entered the background. Then you can perform some operations at Will (if you do not do any operation, you will not be notified). In my example, press enter several times. After the program is running, you will be notified.

 

Frontend and backend Switch

Sleep example

$ Sleep 20 <br/> ^ z <br/> [1] + stopped sleep 20 <br/> $ jobs <br/> [1] + stopped sleep 20 <br/> $ BG 1 <br/> [1] + sleep 20 & <br/> $ jobs <br/> [1] + running sleep 20 & <br/> $ FG 1 <br/> sleep 20 <br/> $ 

^ Z: press Ctrl + Z to suspend the foreground program and assign job number 1.

Jobs: view the current job.

Bg 1 sets the job with job number 1 to run in the background. At this time, the program continues to run.

Fg 1 switches the job with job number 1 to the foreground

These operations are generally used in a program like a full-screen editor. If a common * nix program runs in the background, we will redirect its output to a file or record the log file by itself, so there is no need to switch to the foreground, unless I want to -- kill it

 

Kill Process

There are two methods:

1. if it is on the frontend, press Ctrl + C to send an interruption signal to it, and it will usually die. In the background, use the fg command mentioned earlier to switch to the foreground and then press Ctrl + C.

2. If it does not respond to Ctrl + C, or is not in the job scope, that is, there is no job number, but only one process ID, then use the kill command.

The premise of kill command is that you need to know the process ID. To obtain the process ID, you can use

$ Sleep 100 & <br/> [1] 31505 <br/> $ ps <br/> PID tty time CMD <br/> 31371 pts/0 00:00:00 bash <br/> 31505 pts/0 00:00:00 sleep <br/> 31506 pts/0 00:00:00 ps <br/> $ kill 31505 <br/> [1] + terminated sleep 100 <br/> $ <br/> 

If you encounter a problem where kill cannot be killed, try kill-9, which sends a signal that cannot be blocked by the program.

In another extreme case, the program neither responds to Ctrl + c nor hangs (CTRL + Z), so it has to open another terminal.

 

Terminal connection

In general, although * nix shell has limited multitasking features, it is far from enough for people who have used Windows.

Therefore, we will establish connections from multiple terminals to the host.

Method 1:

If it is a terminal directly connected to the host, for example, if you have installed a * nix on the local machine or you ran to the data center to manage the server, press Alt + F1, ALT + F2... you can switch between different terminals.

Method 2:

Software that uses a graphical interface, such as securecrt or putty, is connected to * nix through SSH or telnet. You can establish multiple connections to the host in these software and switch between different connections using tabs.

When you have multiple terminals, such as debugging the C/S architecture software, you can run the server on one terminal and the client on the other. For example, you can write and compile code in one terminal, and run the code in another terminal to view the result.

 

Supplement

The top command provides a Job Manager interface equivalent to windows, but this kind of thing is better for GUI programs. Generally, use top to check the resource usage of the current system and which programs occupy the CPU. Press <space> manual refresh and press Q to exit.

Top-13:30:05 up 1 day, 2 users, load average: 0.00, 0.00, 0.00 <br/> tasks: 68 total, 1 running, 67 sleeping, 0 stopped, 0 zombie <br/> CPU (s): 0.0% us, 0.0% Sy, 0.0% Ni, 100.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% st <br/> mem: 124720 k total, 85944 K used, 38776 K free, 20216 K buffers <br/> swap: 530136 k total, 2048 K used, 528088 K free, 30844 K cached <br/> PID user PR Ni virt res shr s % CPU % mem time + command <br/> 1 root 20 0 1656 568 520 s 0.0 0.5. 70 init <br/> 2 root 15-5 0 0 0 s 0.0 0: 00. 01 kthreadd <br/> 3 root RT-5 0 0 0 s 0.0 0.0. 00 migration/0 <br/> 4 root 15-5 0 0 0 s 0.0 0.0. 87 ksoftirqd/0 <br/> 5 root RT-5 0 0 0 s 0.0 0.0. 00 watchdog/0 <br/> 6 root 15-5 0 0 0 s 0.0 0.0. 20 events/0 <br/> 7 root 15-5 0 0 0 s 0.0 0: 00. 03 khelper <br/> 12 root 15-5 0 0 0 s 0.0 0.0. 00 async/Mgr <br/> 117 root 15-5 0 0 0 s 0.0 0: 00. 20 kblockd/0 <br/> 120 root 15-5 0 0 0 s 0.0 0.0. 00 kacpid <br/> 121 root 15-5 0 0 0 s 0.0 0: 00. 00 kacpi_notify <br/> 122 root 15-5 0 0 0 s 0.0 0.0. 00 kacpi_hotplug <br/> 170 root 15-5 0 0 0 s 0.0 0.0. 00 kseriod <br/> 216 root 20 0 0 0 0 s 0.0 0.0. 00 khungtaskd <br/> 217 root 20 0 0 0 0 s 0.0 0.0. 67 pdflush <br/> 219 root 15-5 0 0 0 s 0.0 0: 01. 65 kswapd0 <br/> 220 root 15-5 0 0 0 s 0.0 0.0. 00 AIO/0 <br/> 221 root 15-5 0 0 0 s 0.0 0: 00. 00 nfsiod <br/> 222 root 15-5 0 0 0 s 0.0 0: 00. 00 xfs_mru_cache <br/> 223 root 15-5 0 0 0 s 0.0 0.0. 00 xfslogd/0 <br/> 224 root 15-5 0 0 0 s 0.0 0.0. 00 xfsdatad/0 <br/> 225 root 15-5 0 0 0 s 0.0 0.0. 00 xfsconvert <MCE: Script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"> </MCE: SCRIPT> <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> D/0 <br/> 226 root 15-5 0 0 0 s 0.0 0: 00. 00 crypto/0 

 

The uptime command displays the current system running time and load, which is the first line displayed by the top command.

 

PS-Ef provides more detailed process lists than PS commands. Simple PS only lists the user's own processes, and this command lists all the user and system processes.

 

CTRL + Q, CTRL + S if you accidentally press Ctrl + Q, the terminal will pause any display, but still accept your input, you feel that the system has not responded, at this time, press Ctrl + S to continue the display. Therefore, when the system does not respond, do not press it. Pay attention to pressing CTRL + q.

 

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.