Lab 2C: Manage Processes

In this module we explore the following both in Linux and Windows Server 2019 Systems:

  1. view information about running processes in both Linux and Windows

  2. start processes with specified priorities in Linux, and to change the priority of running processes

  3. be familiar with shell job control in Linux

Task 1: Viewing process information in Linux

ps - to view process

Run the command in Linux run the following commnad to view process:

ps

Which processes does it show by default?

ps -ef

Now re-run the command with the –ef options.

What does it show?

ps -ef --forest

Finally run the command with both the –ef options and also --forest (note two dashes for forest). What does it show?

It might be helpful if you pipe the ps output to more (or less), so you can see the column headings. Also, note the process ID (PID) of init. Recall that init is the precursor of all processes in Linux and has a PID of 1 (one).

Now view a continually updated list of running processes using the top command. Which process(es) are often near the top of the list? (Just note one or two). The top command also shows other useful information.

How much physical memory is installed in your virtual machine? How much of the physical memory is currently being used? How much swap space is there, and how much is being used?

Task 2: Viewing and changing process priorities in Linux

Run the command:

dd if=/dev/zero of=/dev/null

This command will continually read input from the "zero" device (source of zeroes) and writes it to the "null" device (often called the “bit bucket” where unwanted output is dumped). The purpose of running this is simply to start a process that will consume a lot of system CPU, so we can monitor it using top. Leave the command running in the foreground.

Open a second terminal & run the top command. You should see the "dd" process at the top of the list, as it is now consuming the most CPU. Notice in the top listing the column labelled "NI" ("nice" value). What is the default nice value of the dd process?

Make a note of the process ID (PID) of the dd process. Exit from top (press “q”) and then use the kill command to kill the dd process by specifying its process ID. Switch back to the other terminal where dd was running. What appears on the screen?

Now re-run the dd command, but with a nice value:

nice –n 15 dd if=/dev/zero of=/dev/null

Run top in the other console and check the nice value.

Make a note of the process ID (PID) of the dd process. Exit from top (press “q”) and then use the kill command to kill the dd process by specifying its process ID. Switch back to the other terminal where dd was running. What appears on the screen?

Now re-run the dd command, but with a nice value:

nice –n 15 dd if=/dev/zero of=/dev/null

Run top in the other console and check the nice value.

Task 3: Job control in Linux

jobs

This shows all jobs (processes) associated with the current shell, and their statuses. Make the dd process continue running in the background by typing:

bg 1

where "1" is the job number shown as output from the jobs command. Run the jobs command again – what does it say the status of the dd process is this time? When a process is moved to the background it is the same as if you had originally started the process with an ampersand (&) on the end of the command.


Bring the dd command back to the foreground by typing:

fg 1

You'll notice that anything you type now is just echoed to the screen, and there is no response (at this time) from the shell. When a process is running in the foreground of a shell, it takes control of the input from the keyboard. If the running process does not consume the keystrokes you typed, the shell will receive these when it resumes after the foreground process terminates. Only then will the shell respond.

Finally, end the dd process by pressing Ctrl-C. This should terminate the process (the same as if you had used the kill command from another console).

Important note: For some strange reason, students sometimes use Ctrl-Z to “get out of” an editor. This is WRONG and often leads to confusion and errors, as the editor and open files are still “there”, just suspended. Please leave your edit sessions by properly terminating the editor, after saving or abandoning, as appropriate, your open files.

Remember – Ctrl-Z only suspends a foreground process, but does not kill it. To kill a foreground process, you must use Ctrl-C.

Finally, end the dd process by pressing Ctrl-C. This should terminate the process (the same as if you had used the kill command from another console).

Important note:

For some strange reason, students sometimes use Ctrl-Z to “get out of” an editor. This is WRONG and often leads to confusion and errors, as the editor and open files are still “there”, just suspended. Please leave your edit sessions by properly terminating the editor, after saving or abandoning, as appropriate, your open files.

Remember:

Ctrl-Z only suspends a foreground process, but does not kill it. To kill a foreground process, you must use Ctrl-C.

Task 4: Viewing processes in Windows

To view processes in Windows using the GUI we need to run Task Manager.


Ways to launch "Task Manager"

  1. Windows Taskbar> right-click > choose “Task Manager”.

(The Taskbar is the bar across the bottom of the screen with application icons).


  1. Ctrl-Shift-Esc use the key-combination (Control+Shift+Escape keys together).


  1. Windows Key + X


When the Task Manager appears, by default it shows only foreground running applications. To show more, choose “More details” near the bottom and you should see a tab showing “Processes”.

You can see even more again in the “Details” tab. You can sort by any column by clicking on the column heading, e.g. to see which processes are using the most CPU or the most memory.

In the Details tab, we can also choose what columns you want displayed – there are in fact many hidden columns with even more information about processes that an administrator can use to diagnose problems on a system. In the details tab, right-click on the column headings and choose “Select columns”. A separate window should appear allowing you to choose other columns you’d like displayed for each process.

To view processes in Windows using the PowerShell

To see process information from the command-line in Windows, using PowerShell.

Open a PowerShell window and type:

Get-Process

1. Use Get-Process to show only information about the powershell process.

2. Show all Windows processes where the “working set” is size 50 MB (use 50,000,000 bytes) or greater.

Note there is another command we can try too, which works in both PowerShell and a simple command prompt:

tasklist

Task 5: Killing a process in Windows

We’re going to try killing this process several ways.

Kill Process using the GUI,

open the Task Manager. Search for the command prompt process – it’s friendly name that appears in the Process list might be “Windows Command Processor”, but if we go over to the Details tab, you find it named “cmd.exe”. Right-click on the process in the list and choose “End task”. Simple.


Kill Process using cmd

Open Command Prompt window. It will be a different process with a different process ID (PID).

taskkill /PID XXXX where XXXX is the PID number of the process


Kill Process Using PowerShell

follow the process in the previous task to find the process that corresponds to your Command Prompt window (“cmd” or “cmd.exe”). Note the number in the “Id” or “PID” column for the command prompt.

To kill the process using it’s ID in PowerShell, type:

Stop-Process –ID XXXX where XXXX is the PID number of the process


Note that Windows does also let to kill a process using its name, but that can be dangerous if there are several processes running with the same name. For the reason, using the ID is a more targeted method.