Medium Pimping with System Resources

May 25th, 2008

That’s right– medium pimping. The pimping discussed in this post is not so grandiose as to be considered, “big pimping,” but still of plenty value. I will cover how to manage processes in your computer via the terminal, with the commands kill, ps, top, and df.

There are good GUI tools out there(graphical user interface), but I’m covering the terminal for a couple of reasons. Every linux distro comes with a terminal. Linux without the terminal is like Cheech with no Chong, bread without butter, or St. Patty’s without beer… Not only is the terminal a near universal tool, but it is also one of the last things you’ll lose the opportunity to work with if your computer goes kaputz. As a bonus, terminal tools are light on system resources, which really helps things when you’re stretching your computer to its limits.

The system resources I’m referring to in this article are processes. Processes are groups of tasks that your computer does in order to make you see this high quality article. The main processes you’re most likely using right this moment to read this article are xorg and mozilla, your GUI manager and your web browser (Firefox). Linux acts like an orchestra composer for all the programs that need to run in order for your box to work. It tells each process exactly what resources it can have: how much memory, cpu time, and etc.

I’m a farm boy, so I’ll make a farm analogy. On many small farms they use ditches to irrigate fields.

Top Down Ditch Drawing

So here is my top-down ditch drawing. The water flows through the ditch at the top, and metal tubes divert the flow of the water into the field along furrows (the lines). This is comparable to your computer’s resources, in that, the more water that one tube and furrow use, the lower the water in the ditch and the less water that other processes can use. In real fields, all the tubes are the same size, but in your computer, Linux dictates how big the tubes are. So if your computer is near freezing as you attempt to watch a dvd, play World of Warcraft, move ten gigabytes of songs, and surf the internet, while spinning the cube in your desktop effects… These are all big tubes and your ditch probably has no water left for you to use to accomplish any of this.

Top Down Ditch Drawing With little water.

What happens when you over tax your computer is that lights in your house begin to flicker, and a loud grinding sound comes from your computer’s hard drive as smoke pours out the fan… Or possibly more likely your mouse cursor doesn’t work as well and the computer appears to be close to freezing. In my experience (and theoretically), the Linux kernel will take control when processes start to crash your computer and things eventually return to a normal state. Eventually being key. More often than not, it will be preferable to use one of the following techniques to end the processes:

1. kill When I was in bootcamp we had to yell, “kill” outside the chowhall before we could all file inside and eat (and people wonder why so many Marines degenerate into cannibals). Kill is how you end uncooperative processes (it actually has many other functions; man kill). The two signals for kill that you should know about are 15 and 9. Signal 15 is where the linux kernel asks your process, “Hey there buddy, you mind stopping what you are doing?” Signal 9 is where the linux kernel says, “You’re stopping that right meow.” In order to use kill, you’ll need a process’s id number or PID. The syntax is as follows: kill -15 14962 (command, signal, PID).

2. ps axf | grep “process”
ps is a command that gives you a list of the running processes; with the axf options it will give you all the processes running in a tree format, where you will be able to see what processes spawned what other processes. Below is a screenshot of such a tree:

ps axf tree.

You can see the process used to launch the ps axf tree, Process ID number 12828. Konsole launched a bash shell (the terminal) which spawned the ps axf command. (I did all three by using the Konsole to launch the shell and then using the shell to type in ps axf, but this is not always the case).

Generally, when a process is giving your computer a hard time, you will know which one it is. For example, I had a problem with Firefox in Xubuntu. Occasionally, it would crash and the window would just sit there. Unlike Gnome or KDE, my window manager(XFCE) never offered to close the zombie’d window and so I would open a terminal and type: “ps axf | grep ‘firefox’

ps axf | grep.

This command creates our process tree and then passes it to the “grep” script and tells it to look for the word, “firefox.” Why this is useful, is that it returns a PID (process id) that you can then use to kill the application.

3. top

One of the problems with using ps axf to kill processes is that occasionally things are moving too quickly on your computer to get a fix on the process. What I mean by this, is that after typing ps axf, by the time you enter the kill command, the PID has changed. An easy remedy for this is to use the top command.

top

Top is a snapshot of your system’s processes that updates every few seconds. It ranks them in order of heaviest to lightest. The processes that are using all your juice will be at the top of the list, and the lighter processes will be at the bottom.

Top is great for handling runaway processes because it will hold a process’s id number for as long as you need to type it in. All you have to do is hit the “k” key on the keyboard and top will prompt you for a PID and a signal to kill the process. So why not use top all the time? If a process has zombie’d it may not be using enough resources to show up on the top snapshot.

Another nice feature of top is re-niceing. The nice level of a process is how much cpu time a process has. The more cpu time a process has the faster it will complete its task (and vice versa). Nice is a measurement of how “nice” a process is to other processes. The highest nice level is 20. This is like the person who is inviting people to cut in front of them in the supermarket line. The lowest nice level is -20. A process with a -20 nice level is like an overweight forty-year old guy who is shoving kids out of the way so that he can order first at the ice cream truck. To Renice a process in top, you hit R, Type in the PID, and the nice level you want to put on the process.

4. Finally, we’ll touch on the df command. df tells you how much disk space is being used on your partitions. The reason you might need to know about df is if you were using the terminal to transfer a large amount of files from one area to another, and etc. For example, when installing a new Linux distribution, I like to create a partition on my hard drive, copy all my music, writing, and web development files to it, and then clear the rest of the hard drive out and install the distro there. Afterwards, I just copy them back into the new distribution. This last time around, I kept using df to watch my files fill up the partition. I had to cancel a few cp(copy) processes because they were going to max out my space. (You can cancel any program or process that you started from a terminal by hitting Control-C in that terminal.)

So that’s it– master these and you will be ready for all those medium hoes that give your computer a hard time.