Skip to content

WA2. Linux commands

Statement

  1. Run the “top” command and explain the output. Which process is using the most CPU and memory?
  2. How would you kill a process in a Linux terminal?
  3. Run the following commands and explain the output and describe the differences
    • ps
    • pstree

Solution

1. Run the “top” command and explain the output. Which process is using the most CPU and memory?

  • By running the top commands on my machine I got the following results:

top

  • The processes that are using most memory are kernel_task and WindowServer and both of them are related to the operating system.
  • The first one is the kernel process and the second one is the file system and folders view program (Finder and desktop GUI).

2. How would you kill a process in a Linux terminal?

  • To kill a process, you must uniquely identify the one process that you want to kill and pass that identifier as an argument to the kill executable.
  • A good identifier would be the process id (PID) which can be supplied to the -9 option.
  • Some options may include using ports (kill any processes running on port X) or piping the grep function to search for processes that match a specific name.
  • Below, I will kill the top process that we started in the previous question with its PID:
    • we can see from the previous image that the PID for the top function is 47688
    • We will use the command: kill -9 47688 in a new tab while the old tab is still open.
    • The command itself has nothing to report, but the terminal where I was running the top reported that the process is killed and updating the numbers has stopped.
    • kill

3. Run the following commands and explain the output and describe the differences - ps - pstree

  • Both of these commands prints the running process; but while ps print them as a list, pstree prints them as a nested list that help identify the parent/child relationships between processes.

Conclusion

  • We started the top process to get more info about the state of all processes across the system.
  • We used the output of the top command to get the PID of the top process itself.
  • We passed the PID of that top process to the kill command which caused it to terminate.
  • We used ps and pstree to also know some more information about the system.

References