Programming Assignment Unit 2
CS 3307
University of the People
Q/1) Run the “top” command and explain the output. Which process is using the most
CPU and memory?
Answer:
Explanation of the output:
This command shows the Linux processes. This command will display a dynamic list
of processes that are currently running in a system.
First, it shows the entire statistics of the running processes in a system, like below
In the above statistics, we see all the tasks along with different states. It will also
display the entire memory available, the amount of cache, the amount of occupied and free
memory. Finally, it also shows the amount of memory from the hard disk which is being used
as the RAM in our virtual Linux terminal (GeeksforGeeks, 2019).
Then, we see the list of currently running processes with detailed information, like the
following:
PID refers to a unique process ID.
PR refers to the priority of the task.
NI refers to the nice value of the processes, where a negative nice value means a higher
priority.
VIRT refers to the total amount of virtual memory used by each task on the list.
RES refers to the total physical memory used by each task.
SHR refers to the amount of shared memory used by a particular task.
%CPU refers to the amount of CPU involved for each task in percentage to measure the CPU
usage.
%MEM refers to the amount of memory involved in each process execution.
TIME+ indicates the CPU time.
COMMAND column holds the commands that started the current process.
Q/2) How would you kill a process in a Linux terminal?
Answer:
First, we need to get out of the list of processes shown by the top command. For that,
we press q from our keyboard.
We can know the process id by the following command:
pidof <options> <program>
Then, we can kill the process using the following command:
[root@host-2-129 ~]# kill 1786
In the above command, 1786 is the process id.
We can also use the “killall” command to kill multiple processes at the same time, like
below: killall <process>
We also have a command named kill -9, that we use to kill any unresponsive processes.
We execute the command along with the process id like below: kill -9 <processID>
Q/3) Run the following commands and explain the output and describe the differences
ps & pstree Answer:
ps command:
We can view process-related information on Linux systems through a tool called ps,
which stands for "Process Status". The ps command is used to display a list of the currently
running processes along with their PIDs and other information depending on the options
(GeeksforGeeks, 2022).
An example run of the command is as follows.
In the above result, there are 4 columns that signify as follows:
PID column holds the unique process ID
TTY column holds terminal type that the user is logged into
TIME column holds the amount of CPU in minutes and seconds that the process has been
running.
CMD column holds the name of the command that launched the process. pstree
command:
Under Linux, this command displays the running processes as a tree, which makes the
output more visually appealing and conveniently displays the hierarchy of processes. Init or
the process with the given PID are the roots of the tree (GeeksforGeeks, 2020).
An example run of the command gives the following output:
Differences between ps and pstree command:
ps pstree
This command gives the current system This command shows the current system
processes information in a tabular format processes in a tree structure. Init or the
with four columns, which includes unique process with the given PID are the roots of
process ID, terminal type, CPU time, and the tree.
process.
By default, this command shows the We have to use this command with -p
process id. option like this: pstree -p to see the
process ids in the branches of the tree.
References
1. GeeksforGeeks. (2019, May 27). top command in Linux with Examples.
Retrieved September 12, 2022, from https://www.geeksforgeeks.org/top-
command-in-linux-withexamples/
2. Gite, V. (n.d.). How to force kill process in Linux using kill and killall. Cyberciti.
Retrieved September 12, 2022, from https://www.cyberciti.biz/faq/how-force-kill-
process-linux/
3. Kaplarevic, V. (2021, December 16). How to Kill a Process in Linux? Commands
to Terminate. Knowledge Base by phoenixNAP. Retrieved September 12, 2022,
from https://phoenixnap.com/kb/how-to-kill-a-process-in-linux
4. GeeksforGeeks. (2022, January 10). ps command in Linux with Examples.
Retrieved September 12, 2022, from https://www.geeksforgeeks.org/ps-
command-in-linux-with-examples/
5. GeeksforGeeks. (2020, June 4). pstree Command in Linux with Examples.
Retrieved September 12, 2022, from https://www.geeksforgeeks.org/pstree-
command-in-linux-withexamples/