Processes
Each activity on a unix/linux system outside the kernel is performed in a process.A process is :
- an executing program
- identified by a unique PID (process identifier) which is unique
- has an owner and a group
- has a number of open files
Looking at processes
ps command is used to see the processes.To see your current shell's processes:
% ps PID TTY S TIME COMMAND 25830 ttyp1 S 0:29.55 nedit -server Shell.html 29618 ttyp1 IW + 0:00.12 more -svf 32077 ttyp1 IW 0:01.56 -csh (csh)
Life-cycle of a process
A process is created when an existing process is cloned.- fork
- new process is called the child
- duplicate of its parent
- A process chooses to execute a program using exec
Parent and child processes
Processes and jobs
Foreground and background jobs
A process may be in the foreground, in the background, or be suspended.Foreground process
In general the shell does not return the UNIX prompt until the current process has finished executing.
Foreground process has access to the terminal for its input and output.
Background process
Background process doesn't has access to the terminal for its input and output.
Foreground and background jobs can be - suspended and resumed OR terminated.
Some processes take a long time to run and hold up the terminal. Back-grounding a long process has the effect that the UNIX prompt is returned immediately, and other tasks can be carried out while the original process continues executing.
Job numbers
When background processes are invoked the shell tags them with a job number.To background a process, type an & at the end of the command line. For example, the command sleep waits a given number of seconds before continuing. Type
% sleep 10 This will wait 10 seconds before returning the command prompt %. Until the command prompt is returned, you can do nothing except wait.
To run sleep in the background, type
% sleep 10 & [1] 6259
Here [1] is the job no and 6259 is process id. The & runs the job in the background and returns the prompt straight away, allowing you do run other programs while waiting for that one to finish.
The first line in the above example is typed in by the user; the next line, indicating job number and PID, is returned by the machine. The user is be notified of a job number (numbered from 1) enclosed in square brackets, together with a PID and is notified when a background process is finished. Backgrounding is useful for jobs which will take a long time to complete.
Backgrounding a current foreground process
To put an already running job in the background, first suspend it with CRTL-Z and then use the "bg" command:myprog - execute a process CTRL-Z - suspend the process bg - put suspended process in backgroundNote: do not background programs that require user interaction e.g. vi
Foreground a process
To move a background job to the foreground, find its "job" number and then use the "fg" command. In this example, the jobs command shows that two processes are running in the background. The fg command is used to bring the second job (%2) to the foreground.jobs [1] + Running xcalc [2] Running find / -name core -print fg %2
Stop a job running in the background
Use the jobs command to find its job number, and then use the stop command. You can then bring it to the foreground or restart execution later.jobs [1] + Running xcalc [2] Running find / -name core -print stop %2
Kill a job running in the background, use the jobs command to find its job number, and then use the kill command. Note that you can also use the ps and kill commands to accomplish the same task.
jobs [1] + Running xcalc [2] Running find / -name core -print kill %2
Some notes about background processes:
- If a background job tries to read from the terminal, it will automatically be stopped by the shell. If this happens, you must put it in the foreground to supply the input.
- The shell will warn you if you attempt to logout and jobs are still running in the background. You can then use the jobs command to review the list of jobs and act accordingly. Alternately, you can simply issue the logout command again and you will be permitted to exit.
Listing suspended and background processes
When a process is running, backgrounded or suspended, it will be entered onto a list along with a job number. To examine this list, type% jobs An example of a job list could be
[1] Suspended sleep 1000
[2] Running netscape
[3] Running matlab To restart (foreground) a suspended processes, type
% fg %jobnumber For example, to restart sleep 1000, type
% fg %1 Typing fg with no job number foregrounds the last suspended process.
Killing a process
kill (terminate or signal a process) It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop)To kill a job running in the foreground, type ^C (control c). For example, run
% sleep 100
^C To kill a suspended or background process, type
% kill %jobnumber For example, run
% sleep 100 &
% jobs If it is job number 4, type
% kill %4 To check whether this has worked, examine the job list again to see if the process has been removed.
ps (process status) Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill PID_number
% sleep 1000 &
% ps PID TT S TIME COMMAND
20077 pts/5 S 0:05 sleep 1000
21563 pts/5 T 0:00 netscape
21873 pts/5 S 0:25 nedit To kill off the process sleep 1000, type
% kill 20077 and then type ps again to see if it has been removed from the list.
If a process refuses to be killed, uses the -9 option, i.e. type
% kill -9 20077 Note: It is not possible to kill off other users' processes !!!
Summary
Command | Meaning |
---|---|
ls -lag | list access rights for all files |
chmod [options] file | change access rights for named file |
command & | run command in background |
^C | kill the job running in the foreground |
^Z | suspend the job running in the foreground |
bg | background the suspended job |
jobs | list current jobs |
fg %1 | foreground job number 1 |
kill %1 | kill job number 1 |
ps | list current processes |
kill 26152 | kill process number 26152 |
References
0 comments:
Post a Comment