Practice 3B - Process Management on Linux
Practice 3B - Process Management on Linux
PRACTICE 10:
Process Management on Linux
1. Boot Ubuntu. From the command line, read the manpage about the ps (man
ps) command.
Run "ps" and show the processes that are running at the moment.
The first value that appears is the process identifier (PID). The second is the
terminal that is associated with that process. Then we can also observe the
accumulated time of CPU usage, and finally the name of the program that has
led to this process.
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
2. By default, ps command only shows the processes associated with the same
terminal and user. We will retrieve the options that allow us to display all
processes.
Run the command "ps -e". Check the list of processes that are running on
your machine. Which has the PID equal to 1? What processes are running on
the terminals tty1, tty2 ... tty6?
Remember what happened when you pressed "Ctrl + Alt + F1" to "Ctrl + Alt
+ F6"; Check the name of each of these terminals. Returns to the terminal
where the graphical environment is located. What process is running on tty7?
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
3. The ps command can still give us more information about running processes.
For example, who started each process on our machine? We will use the
following ps command options: "a" allows us to know all the processes that
have an associated terminal; "x" those who do not have a terminal; The "u"
option shows the output in a more readable format.
Look at the headers for the information you have obtained from each process.
4. All the commands and options we have seen so far provided static information
about the processes. This information is extracted from the system "/proc"
directory. There are some applications that also allow us to know, in real time,
the characteristics of each process.
Run the "man top" command. What does the top command do?
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
Run “top”.
As you can see, information about the system is refreshed every 3 seconds
(you can change that parameter). Otherwise, the top user interface is not
particularly nice, although it is easy to modify certain options and adjust to
our requirements. Press "q" to exit.
5. Now run the "top -u usuario" command. How has the output of the
command changed? Who do the processes you observe now belong to?
6. Run “top”. Take note of the PID of the Xorg process. Go to the "/proc" folder.
Execute “ls”. Whom do the different directories present in it belong to? Find
the directory whose name matches the Xorg PID. Move to the directory. List
the contents of it.
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
Check what type of file is limits (file limits). Watch your content with the help
of "sudo less limits". What kind of information does it contain?
7. Now let's look at some keyboard shortcuts that allow us to manage processes.
Check the function of the yes command. Although it may not seem very useful
to us, it will help us to see how we can stop and "kill" processes.
Run the "yes hello" command. Notice that the message appears infinitely
many times.
8. Let's "kill" that task. Try to exit the task with "q". The "q" key allows you to
exit certain running applications, but not to end a task. Type the shortcut "Ctrl
+ C". It should end with the active process. What happened? The keyboard
shortcut "Ctrl + C" is responsible for finishing (or killing) a task. The shortcut
"Ctrl + Z" is only in charge of stopping it (although the process continues
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
"alive" and can be resumed in the state in which it stopped). Finally, the
shortcut "Ctrl + Y" suspends a process until it has a new entry.
9. We can now redirect the output of the command to a file (note that this could
give us serious memory problems on our machine). In order to redirect the
output of commands to a file without danger that this will collapse our
memory, Linux has a file called / dev / null.
Check the properties of "/dev/null". It points out the type of file that is (c), its
permissions, owner and size. Can we write to it? Can we read its contents?
As you can see, the running task does not let you continue to use the terminal.
Type "Ctrl + C" again to stop it again. What size is the /dev/null file on disk
now? Where did all the information we sent go?
10. Run the command “yes how are you > /dev/null”.
11. Open a new terminal and locate the process PID "yes" started. You can use
ps or top command. Check the utility of the kill command. As you can see, kill
allows us to send signals to a process.
Signal Value Action Comment
──────────────────────────────────────────────────────────────────────
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at terminal
SIGTTIN 21,21,26 Stop Terminal input for background process
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
Read Sections 12.1.1.3 and 12.1.2 of the previous link. Take note of some of
the most common signals that can be sent to running processes and how to
do it (by keyboard, or by means of kill). Interrupt the active process "yes"
using the kill command (kill -s 15 PID). Notice the difference between SIGKILL
and SIGTERM.
12. Envía la señal SIGTERM al proceso yes antes iniciado (kill –s señal PID).
Vuelve a ejecutar el proceso “yes que tal > /dev/null” y, desde la otra
terminal, envíale ahora una señal de SIGKILL o -9. Comprueba que el
resultado externo ha sido el mismo que antes.
13. Not all processes running on a terminal have to run in the foreground
(blocking the terminal). We can also do what is known as background
execution.
The way to make a program run in the background is by typing the program
at the shell followed by a & symbol; Check the following command:
14. Let's now use the jobs command. First check your function through "help
jobs". The difference between a "job" and a "process" is that the "jobs" are
necessarily initiated from a terminal and are associated with it (they are "child"
processes of the terminal).
Run the "jobs" command on the same terminal that you have executed "yes".
What state is the process in?
Check it also with the “ps” and “top “commands. Observe the percentage of
CPU consumed.
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
15. There is always a defined hierarchy among Linux processes, since each
process must have a parent process (except the initialization process or init).
This hierarchy acquires relevance since "killing" a parent process usually ends
also the child processes. In some cases, a parent process and their children
may even share memory. Check the hierarchy of processes on your machine
by means of the “pstree” command (you can see some of its options in man
ps). Check your ancestors. Write down on your report what process is the
ancestor of “yes” command.
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
16. Re-execute “yes” on the same terminal and also in the background (yes
mensaje > /dev/null &). Check the process tree again by means of
"pstree -h". Using “top”, write in your report the percentage of CPU
(approximate) that these two processes add together.
17. Check the status of the processes started in this shell by means of "jobs".
Verify that the two tasks started appear and that the second appears the +
symbol indicating that it is the last one that has been executed.
18. The fact that the tasks are running in the background prevents us from
sending a keyboard signal (for example, Ctrl + Z, Ctrl + C). Check it.
Through "jobs", each task that is running from our terminal receives a new
number (1, 2 ...). These numbers can be used with “fg” (foreground) to bring
these tasks to the foreground (fg 1, fg 2), or to send them to the background
(bg 1, bg 2 ...).
19. Kill the second task started. For example, you can run top and capture your
PID, and send a kill signal.
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
20. Check, using “job” command, that only one active task remains.
21. Run " yes menssage2 > /dev/null ". By means of the keyboard (Ctrl + Z),
or by means of kill (with the signal SIGSTOP or 19 and with the corresponding
PID) sends to the process a signal of "stopped".
23. To recover a paused task just run “fg” (foreground) or “bg” (background),
depending on whether you want the task to run in the foreground or
background. Check with “fg” that the task returns to the foreground.
24. With a signal or the keyboard shortcut, stop the task again. Start it again, this
time in the background (bg). It stops again without bringing it to the
foreground. For this you must use signals by means of kill.
25. Create new tasks by means of "yes" in the foreground and background and
try to stop them and resume them by means of “bg”, “fg” and “kill” signals.
26. Let us now recover the idea that all processes depend on their "parent"
process. Run a new terminal. Run the command " yes hola > /dev/null &"
I. E. S “Zaidín-Vergeles” 1º ISO
Computing Systems
several times in succession. On your other terminal now check the "pstree"
structure. Who is the direct ancestor of all of them?
Run "top -u usuario" to check which tasks you have running and which ones
are the most resources you consume. Now close the terminal where the "yes"
processes are running. Re-run "pstree".
ps [a,u,x] Show processes Top Show processes kill [-9] Stop processes
Fg Run a task in Bg Run in background Free free memory space
foreground
Nice run cmd with modified Renice alter priority Crontab Scheduled processes
priority
Jobs list of background Nohup run a cmd Pstree Processes tree
inmune
I. E. S “Zaidín-Vergeles” 1º ISO