Part 1 Linux Chapter 4 Process Management
Part 1 Linux Chapter 4 Process Management
PROCESS MANAGEMENT
Introduction
■ A process = An execution of a program
■ A process is managed under Linux with following
information:
◻ Process ID (pid)
◻ Parent process ID (ppid)
◻ User ID of the owner(uid) và nhóm (gid)
◻ Standard input (stdin), standard output(stdout), standart error
chanel (stderr)
◻ CPU time and priority
◻ Current working directory of the process
◻ List of the references to the files that the process uses.
■ Processes are order for exploring CPU
1
Process type(1)
■ System process
◻ Usually belong to root
◻ No interaction interface
◻ Usually run in background mode (daemon)
◻ Responsible for common task.
◻ example:
■ lpsched: manage the printing service
■ cron: execute automatically a command at a specified time
in the future
■ inetd: manage network communication.
◻ Example:
■ cp
■ vi
■ man
■ …
2
ps command
■ Display the list of process:
◻ Without any option, the command ps displays the list of processes
that belongs to the terminal user.
$ ps
PID TTY TIME CMD
2803 pts/1 00:00:00 bash
2965 pts/1 00:00:00 ps
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.1 1104 460 ? S 15:26 0:03 init[3]
...
ttanh 951 0.0 0.3 1728 996 pts/0 S 16:09 0:00 bash
ttanh 953 0.0 1.9 6860 4916 pts/0 S 16:09 0:00 emacs
ttanh 966 0.0 0.3 2704 1000 pts/0 R 16:23 0:00 ps aux
...
Status of Process
■ S : sleeping
■ R : running
initiate
cancel
■ T : terminated
■ Z : zombie Running Terminate the process
Ctrl-C or kill
Terminated
Resume Sleeping
fg
3
Command kill
■ Send a signal to a process.
◻ Argument: Process ID
◻ By default the signal is 15 (SIGTERM – terminate a
process)
◻ Option –l: list all available signals .
◻ Ex: kill 935
■ Command killall:
■ Terminate all process that have been launched by a
command.
■ The command name is passed to killall as an
argument.
■ Ex: killall emacs
■ Only the owner can kill the process.
Process Priority
■ All process has the default priority 0
■ Priority values are in interval [-19 ,+19]
■ Smaller priority value indidates higher priority
◻ Root can reduce the priority value
◻ Ordinary user can only increase the priority value.
■ Command nice allows to set the priority to a process at
launching time
◻ $ nice [-n Value] [Command [Arguments ...]]
■ Command renice allows to change the priority of a
process while it is running.
4
Command top
■ Display the following information about running
processes with periodical update :
◻ %CPU
◻ % memory usage
◻ ….
■ $ top [–d]
◻ Option –d allows to specify the update period (in
seconds).
5
Running a process in background
Task management
■ A task = execution of a command
■ There is no more than 1 task running in foreground
■ There can be more than 1 task running in background
kill fg
END Background Foreground END
Ctrl-C
fg
bg
Stop
stop Ctrl-Z
6
Examples:
$ emacs &
[1] 756
$ stop 756
# or $ stop %1
$ bg 756
# or $ bg %1
$ kill 756
# or $ kill %1
Redirection of input/output
streams
■ Each process has:
◻ A standard input stream (keyboard by defaut)
◻ A standard output stream (terminal by defaut)
◻ A standard error stream (terminal by defaut)
■ Redirection of input stream (<)
$ tee < test.txt
7
Combine commands
■ Execute serveral commands independantly
◻ Using character ; for separating process
◻ Processes are executed consecutively but independantly
◻ $cp public/* personal; rm -r public
Pipeline
■ Pipeline mechanism allows to connect the input
stream of the first command to output stream of
the second command
■ Pipeline is set by using: |
◻ $ cmd1 | cmd2
■ Example :
◻ $ls –l | more #print page by page
8
Pipeline (2)
• The pipeline is normally used followed by a grep
command to filter data
• Ex. ps –axuc | grep hello
(find all processes start with hello)
17
Exercise
• 1) Write command seeking for all files named “test”
in your home directory
• 2) Revise the command so that all error messages
are redirected to a file log.txt
9
Exercise
• Given file data.dat
Phạm Đình Hảo? [email protected]?20090991
Phạm Duy Hoàn? [email protected]? 20091134
Nguyễn Xuân Hoàng? [email protected]? 20091171
Đặng Văn Khá[email protected]? 20091432
Lê Hoàng Kiên? [email protected]? 20091505
Ngô Hải Linh? [email protected]? 20091596
• -------
• 1) Extract the list of student full name
• 2) Extract the full name of the students with studentID X
grep “X$” dulieu.dat | cut –d? –f1
• 3) Sort students by studentID
sort –n –k3 –t? dulieu.dat
10