Basic theory
Processes in linux(and elsewhere too) are basically programs currently running on your system. For example, you might have a VLC player process, a Pidgin process, 2 of firefox etc.
Here you should ask 'How are processes created'? Basically one process can spawn other new processes. Like, the terminal is one process, and entering 'emacs &' starts a new Emacs process. One important concept is of parent-child processes. In simplest terms, when a process A spawns processes B and C, the processes B and C are said to be the child of A and process A is the parent of B and C. This is important as parents and children can have some special mechanisms for inter-process communication(processes cannot exchange data among themselves in general). There is no concept of siblings though, and B has no special association with C, apart from them being the children, i.e. process group of A.
You might ask, how was the first process created? Well, the first process is always the 'init' process, which is formed into existance at linux bootup. All subsequent processes are basically the dynasty of init.
Commands
So much for theory, you surely want to see some demo of this stuff now. Give the command
at the command prompt. What you get, is the tree structure of the processes in your system. You can see 'init' is at the top, and it has (among others) cron and gnome-terminal in its children. Your own terminal is a subchild shell([bash] in my case) under gnome-terminal. Multiple processes corresponding to the same program are displayed at a number in the tree instead of multiple nodes(like 2*[bash], if I have two terminals open).pstree
Now for some action. Basically, to control processes(anything, for that matter), you need to specify which process,and what action. Process are specified by their PID, which is a unique number for each process, and actions are specified by signals. Signals are general identifiers, and each process has the its own action defined with respect to each of these identifiers. Processes need not specify all the signals though, in which case, nothing will happen if you pass those process those signals.
Give the command
This basically gives all the processes active on your system, sorted according to pid(read the man pages for the description of command line parameters).ps -aux
Now you can kill any process by typing
This gives the process a signal, whose corresponding action should instruct the process to destroy itself. This may not work always though, in which case running the commandkill process-pid
almost certainly destroys the process. The -KILL parameter, as you should have guessed, changes the signal(we need not go into the exact workings). In fact, you can use the kill command to send any signal to the process(check man), the command is named thus just because the default signal is -TERM (terminate anyone?).kill -KILL process-id
But scrolling through ps -aux listing is certainly no fun, and you would imagine linux provides a better way. It does. pgrep lets you use the awesome grep command to search for processes.
and it will return all process pids having 'fire' in their name. In my case, it just returns the pid of firefox. Now you can use pid and pass signals to it with kill.pgrep fire
I would like to wrap up now. An important segment we have left out are background/foreground processes. I hope to cover those soon. Until then, attain familiarity with everything here, use internet and man pages to read more about everything I have mentioned in bold.
Thanks for reading!


No comments:
Post a Comment