Difference between revisions of "OPS102 - Resources and Processes"
Chris Tyler (talk | contribs) (Created page with "Category:OPS102 == Computer Resources == Every digital computer system has finite resources, managed by the operating system. One or more of these three broad categories...") |
(No difference)
|
Latest revision as of 13:58, 5 October 2023
Contents
Computer Resources
Every digital computer system has finite resources, managed by the operating system.
One or more of these three broad categories of resources constrain (limit) the system's ability to execute each running program:
- Memory - The availability of adequate memory, and the performance of that memory.
- CPU - The ability of the CPU to execute the instructions contained in the program at a particular speed.
- Input/Output (I/O) - This is a very broad category that includes storage, networking, printers, keyboard and mouse, and so forth.
Processes
On a multitasking computer system, a process is a single instance of an executing computer program. For example, ssh
is one program. If there are no running instances of ssh on a computer system, then there are no ssh processes. If one user starts one ssh session, then one there is one ssh process. If five users start a combined total of twelve ssh sessions, then there are twelve ssh processes.
Each process is assigned a Process ID (PID) and is allocated its own resources, such as memory. Processes are scheduled to run (execute) on a CPU core for a fraction of a second, and then the operating system may intervene and switch the CPU core to run another processes. However, if a process is waiting for an I/O resources, such as data from a storage device, a packet of data from the network, or a keystroke from the user, it may inform the operating system that it does not need to be executed until that resource is available. A process which is ineligible for execution because it is waiting for a resources is called a sleeping process.
Resource Conflicts
When the resource requirements of running computer programs significantly exceed available resources, system performance will become unsatisfactory.
It is important to be able to identify the resource consumption of each process, and each operating system provides multiple tools to do this. They also provide tools to terminate processes if they are unstable or consuming excessive resources.
Challenges with Monitoring Resource Usage
It is difficult to accurately monitor a process' resource utilization because it will change from microsecond to microsecond. In addition, some resources are very difficult to accurately measure:
Memory
On contemporary computer systems, memory is very actively and dynamically managed by the Operating System using the hardware's virtual memory features. This enables many useful techniques, including:
- Demand Loading - Only the portion of programs which are used are loaded. Code for features that are not used is not loaded into memory. For example, in a word processor (such as LibreOffice/OpenOffice or Word), features such as table of content generation, endnotes, and watermarks are not used in many documents, and the code for those features will not be loaded in many cases.
- Sharing of Memory - Shared libraries (also called Dynamically Linked Libraries) may be used by many different processes. These libraries are loaded into memory only once, using demand loading, regardless of the number of processes using each library. Similarly, a program which is running in two or more processes will be loaded into memory only once, and shared between the processes. This significantly reduces memory requirements.
- Swap - When system memory (RAM) is approaching full utilization, the operating system may take some of the least-recently-used areas of memory and place them in storage (hard disk or solid state disk) or compress them in order to avoid running out of free memory. Areas of memory swapped out may be swapped back in if they are later required.
Due to the utilization of these techniques and others, the memory consumption of a program can be calculated in many different ways. For example:
- You could add up the full size of the program, all of the shared libraries, and the data used by a process, but that would yield a large number (a pessimistic view of memory consumption). The process is likely not occupying that much memory, and terminating the process will typically release only a fraction of that memory because most of the shared libraries will still be loaded into memory and in use by other programs.
- You could add up just the portion of the program in memory plus the data area in memory used by a process, but this would understate the full memory utilization because it does not include the shared libraries nor the portion of the program or data which has been swapped out or which has not (yet) been demand-loaded.
Therefore resource monitoring tools may present several different statistics about memory usage for each process, or may try to present a rough approximation of the amount of total system memory which can be reasonably attributed to a process.
CPU
Each CPU core is independently capable of executing programs. Most modern CPUs have multiple cores (Symmetric Multi Processing, SMP), enabling multiple programs to be executed simultaneously. Many modern CPUs can execute more than one thread on a core (Symmetric Multi Threading, SMT), meaning that they have hardware support for rapid switching between two or more processes (or, in some cases, sub-processes called threads) on one core -- so a 4-core CPU may be marketed as capable of running up to 8 tasks. However, this does not mean that 8 programs can be fully executed at once!
To use an analogy, a SMP processor is a bit like an apartment building, where each core is like an apartment ad can fully accommodate a family (whether a single, couple, roommates, or parents and children), or, in the case of the CPU, fully execute a process. SMT capability is a bit like Airbnb rentals, which enable those units to be shared out quickly when they're not in use by the main occupants, or in the case of the CPU, quickly run other threads when the core (or part of the core) is not in use.
Further complicating the issue is the fact that CPUs may have multiple cores of different specifications -- some fast, power-hungry cores used for heavy tasks, and other slower, energy-efficient cores for lighter tasks. Cores may be shut down and started up by the OS as needed. For example, some Seneca lab computers have an Intel CPU with 12 cores consisting of 10 performance cores (capable of running 2 threads) and 2 efficiency cores (capable of running 1 thread). In a similar way, a Google Pixel 8 phone has 4 energy-efficient/low-performance Arm Cortex-A510 cores, 4 mid-range Cortex-A715 cores, and one high-energy-usage/very high-performance Cortex-X3 core.
In addition, current CPUs can operate at a range of speeds, and the OS and hardware work together to dynamically tune the speed to balance temperature, energy usage, and performance.
Therefore, statistics reporting the percentage of CPU capability in use by a process may report the percentage of a core's capability in use, either with or without considering SMT capabilities, or they may report the percentage of the full CPU's capability (including all cores). When cores of different specifications are part of a single CPU, it may be difficult to determine the relative ratio of performance between the different types of cores, especially as the speed of those cores is being adjusted -- and therefore utilization percentages are almost always a rough approximation.
Monitoring and Terminating Processes
Monitoring and Terminating Processes on Windows
Graphical User Interface
To manage processes graphically on Windows, use the Task Manager tool. You can access this from the Start menu, or by pressing Ctrl-Alt-Delete and selecting Task Manager from the menu that appears.
The Task Manager display shows processes grouped by executable name, along with approximate resource usage.
To terminate a process, select it using the mouse, and then click on the End Task button in the lower-right corner of the display.
Command Line Interface using the CMD Shell
To manage processes using the CMD shell (the default shell on Windows), use these commands:
1. tasklist - displays a list of tasks (processes) active on the system. You can use options to select which processes are displayed, as well as the output format (table, list, or comma-separated values).
2. taskkill - terminates a process. By default, this command attempts to get tasks to terminate in a safe manner (for example, if the program is saving a file, that operation will be completed before the program terminates). If a program does not respond to request, you can specify the /f
option, which forcibly terminates the program.
You can specify the process to be terminated in one of two ways:
- By process ID, using the
/PID nnn
option, which will terminate the process with the specified process ID. - By image (program or library) name, using the
/IM name
option, which terminates processes with the given image name. Asterisk wildcard characters may be specified as part of the name. Names must include the extension (for example, usetaskkill /im firefox.exe
ortaskkill /im firefox*
instead oftaskkill /im firefox
).
The tasklist and taskkill commands also provide a mechanism for filtering the process selection; see the online documentation for details (help tasklist
or help taskkill
).
Command Line Interface using Powershell
Powershell is an alternate Windows shell. You can use these commands to view and terminate processes using Powershell:
1. get-process - Displays a list of current processes with basic information about each. You can specify a program name as a positional argument (without the filename extension); see the online documentation for other available options.
2. stop-process - Terminates a process. You may specify a PID using the -ID option (stop-process -id pid
) or a program name (without the filename extension) using the -NAME option (stop-process -name name
).
Monitoring and Terminating Processes on Linux
Command Line Interface
From the bash (default shell) command line on Linux, you can view the current process table using the process status command ps
. By default, it will show only processes associated with the current shell, and will show only the process ID (PID), terminal, total execution time, and command name:
$ ps PID TTY TIME CMD 303632 pts/4 00:00:00 bash 303771 pts/4 00:00:00 ps
There are many ps options available to control which processes are displayed and what information is displayed about each process. These are the more commonly-used:
-u user - Displays all processes owned by user -e - Shows every process -l - Shows long output, including process status (S), process ID (PID), parent process ID (PPID), approximate memory size (SZ), total execution time (TIME), and program name (CMD) -f - Shows full output, including process ID (PID), parent process ID (PPID), process start time (STIME), total execution time (TIME), and program name (CMD)
To terminate a process on Linux, send it a signal using the kill
command. Processes are specified by ID:
kill PID
By default, processes are send the terminate signal (signal 15, SIGTERM). This requests that the process terminate gracefully (for example, by completing pending operations before stopping). If a program does not respond to this signal, the kill signal (signal 9, SIGKILL) may be specified; this signal tells the operating system to abruptly and forcefully terminate the process. Either of these forms is accepted:
kill -9 PID kill -KILL PID
There are many other signals that may be sent to processes; to see a list of all available signals, run the command kill -l
or see the online documentation for signal(7) (by running the command: man 7 signal
).
To find all processes associated with a given program, use the pgrep command: pgrep name
To terminate processes by program name, use the killall command: killall name
Text User Interface
In addition to the command-line interface (CLI) tools mentioned above, there are several text user interface (TUI) tools available which display a full-screen view of the current process status which is updated periodically.
The most common TUI process monitoring tool is top
, which displays output like this:
top - 11:51:05 up 1 day, 22:23, 2 users, load average: 0.63, 0.62, 0.66 Tasks: 530 total, 1 running, 529 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.2 us, 1.4 sy, 0.0 ni, 97.1 id, 0.0 wa, 0.2 hi, 0.1 si, 0.0 st MiB Mem : 28797.0 total, 730.7 free, 17121.8 used, 10944.4 buff/cache MiB Swap: 8192.0 total, 8188.0 free, 4.0 used. 10960.2 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 299042 qemu 20 0 11.4g 8.2g 30336 S 18.5 29.2 23:06.23 qemu-system-x86 2382 chris 20 0 1313576 201884 78048 S 2.6 0.7 11:56.51 Xorg 7037 chris 20 0 33.0g 562524 275008 S 2.6 1.9 24:20.54 chrome 2654 chris 20 0 6512860 298252 136624 S 2.3 1.0 16:25.62 gnome-shell 304806 chris 20 0 1005964 54372 40312 S 2.3 0.2 1:00.46 gnome-system-mo 8265 chris 20 0 851044 64084 39280 S 1.0 0.2 14:41.79 gnome-terminal- 1111 polkitd 20 0 603836 12488 7528 S 0.7 0.0 3:37.71 polkitd 7089 chris 20 0 33.0g 324364 171304 S 0.7 1.1 5:50.08 chrome 7354 chris 20 0 1134.3g 355716 129476 S 0.7 1.2 6:32.51 chrome 296095 chris 20 0 1132.2g 160168 115772 S 0.7 0.5 1:14.49 chrome 299134 qemu 20 0 11.0g 2.9g 27264 S 0.7 10.2 1:25.34 qemu-system-x86 1 root 20 0 217576 28572 10624 S 0.3 0.1 0:54.85 systemd 1118 root 20 0 15232 7552 6784 S 0.3 0.0 0:25.38 systemd-machine 2823 root 20 0 249680 19604 8064 S 0.3 0.1 7:15.86 sssd_kcm 2955 chris 20 0 684472 17024 14720 S 0.3 0.1 9:13.72 gsd-smartcard 3340 root 20 0 542844 13952 11776 S 0.3 0.0 0:04.69 abrt-dbus 7090 chris 20 0 32.6g 155276 108568 S 0.3 0.5 13:29.98 chrome 7363 chris 20 0 1132.3g 335376 120948 S 0.3 1.1 3:16.12 chrome 295131 chris 20 0 1136.3g 426468 132800 S 0.3 1.4 3:13.88 chrome 311918 chris 20 0 225500 4224 3072 R 0.3 0.0 0:00.21 top 2 root 20 0 0 0 0 S 0.0 0.0 0:00.20 kthreadd 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp 5 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
This display is sorted by processor utilization (shown in the %CPU column); you can change the sort order to memory utilization (%MEM) by pressing Shift-M, and change back to processor utilization by pressing Shift-P.
By default, this display is updated every 3 seconds; you can change the update frequency by pressing S and typing a new update frequency (in seconds between updates).
To terminate a process, press K; the program will prompt you for the process ID to be killed (defaulting to the one at the top of the display, using the most resources), and the signal to be sent to that process (defaulting to signal 15, SIGTERM).
To see help messages about available options, press the ? key.
To exit from top, press the Q key (quit).
The top
command is available on most Linux and Unix-like systems, including matrix.senecacollege.ca. There are several newer commands available, including htop
and btop
, but these are not present on all systems.
Graphical User Interface
If you're using a GUI on a Linux system, many graphical process monitoring and control tools are available, including the Gnome System Monitor and Conky.