When you run a program in *nix, the OS loads it into memory and sets up some other information concerning your program. The loaded and running program is called a process. When the program terminates, so does the process.
Every process has a system-wide unique identifier called the process id, or pid; it is just an integer. The process is owned by the user who launched it. The ps command will show you a list of the running processes of which you are the owner.
$ ps
PID TTY TIME CMD
17776 pts/18 00:00:00 bash
19041 pts/18 00:00:00 ps
I am running two programs right now: bash and the ps program itself. The PID of each process is shown. The time column is the cumulative amount of CPU time the process has consumed—this is not the same as wall time, i.e., how many seconds it has been since you ran the program. To see all of the processes running on the system, type ps -A or ps -e. The command ps -f will display full process information including the user id of the process owner. It can be combined with -A or -e. To sort this list by user id and view it in less, use
$ ps -ef | sort | less.
When you run a program (i.e., a process) you can ask the OS to run it in the background. Do this by putting a & on the end of the command line,
$ some-program &
$ now-you-can-type-another-command
Now some-program will run in the background and you can get back to work typing commands. To bring a running program back into the foreground, type the fg command,
$ fg
Now some-program would be running and if it is displaying output, the output would be going to the console window. To put the program back into the background, hit Ctrl+Z to pause it, and then type bg to send it back to the background.
To see a list of your processes running in the background use the jobs command. If you have more than one background job they will be numbered with a job number 1, 2, 3, .... To bring job number 3 to the foreground, type fg %3. To send this job back to the background, hit Ctrl+Z, and type bg.
To terminate a runaway process, use the kill command. The syntax is kill pidnumber. You cannot kill processes that you do not own. To kill a process running in the background, issue a jobs command to see your running processes and the determine the job number. Then use kill job number to kill the job; e.g., kill %3 to kill job number 3.
The top command will display a table of the processes which are currently using the most CPU time. It is a highly configurable program; try man top. (Be aware that top is a fairly resource-intensive program; it's not cool or nice to run top alot).
The nice command was designed to allow a user to run a program with lower than normal priority, so it will not hog the CPU as much. The idea is that other people may be peeved if you're sucking up too much CPU time, so you can be nice about it and still run your program. The syntax is nice -n adjust command where command is the program you want to run (e.g., it could be a big make process). Adjust is an integer which determines the adjusted process priority: -20 is highest, to 19 lowest. No one ever uses nice.
Thursday, October 8, 2009
Linux - Processes
Labels:
background,
foreground,
kill,
linux,
nice,
pid,
process id,
processes,
top
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment