Signal Number
Signal Name
Meaning
1
SIGHUP
Hang up
2
SIGINT
Terminal interrupt
3
SIGQUIT
Terminal quit
4
SIGILL
Illegal instruction
5
SGTRAP
Trace trap
6
SIGIOT
IO Trap
7
SIGBUS
Bus error
8
SIGFPE
Floating point exception
9
SIGKILL
Kill
150
◾
Linux with Operating System Concepts
an exception is raised so that, if handled by the software, the error can be handled. It
is possible, based on the program, that after handling the exception, the program can
continue executing. For instance, a floating point exception (overflow, division by 0)
might be handled by the program by displaying a message that it could not perform the
computation and request new input. In the case of signal number 9 (kill), or SIGKILL,
the signal is never passed to the process. Instead, Linux immediately kills the process
with no attempt to shut down properly or save the process. Thus, SIGKILL is the most
powerful signal used in the kill command in that it assures immediate termination of
the process.
When using the kill command, you include the signal as
–s
#
where
#
is a number from
1 to 9. You might specify one of the following to kill the process with PID 12413.
kill –s 9 12413
kill –s SIGKILL 12413
kill
−
9 12413
Notice in the third example, we have omitted the –s in favor of using
−
9.
If you wish to kill all processes (other than the init process), you can use
kill
−
1
(the
number one). However, as most of the processes running are not owned by you the user,
you would be unable to do this (the system administrator could). If you want to see all of
the signals, use
kill
−
l
(lower case L).
A variation of kill is the instruction
killall
. This instruction will kill all instances of
the named process(es). For instance, you might enter
killall man vim
to kill all instances of man and vim that are running. As with kill, killall accepts a signal.
However, if none is given, it defaults to signal 15, which is SIGTERM. The killall command
has an interactive mode, specified with option –i, which pauses to ask the user before kill-
ing each process. Each process is presented to the user by name and PID. Aside from speci-
fying programs by name, you can specify users in killall using the option
–u userlist
.
This will kill all processes of the named user(s). Unless you are root, you would be unable
to kill processes owned by other users.
You can also kill or exit processes using the Resource Manager tool. From the Processes
tab, select a process, right click, and select one of End Process or Kill Process. The End
Process selection will terminate the process normally while the Kill Process operates as if
you pass -9 to the kill command.
4.6.3 Methods to Shut Down Linux
To wrap up this section, we look at commands that are used to shut down the Linux
operating system. These are intended for the system administrators and not indi-
vidual users. However, they are worth exploring here as we need to assure that the
operating system is shut down properly. If the Linux operating system is shut down
Managing Processes
◾
151
abnormally, such as by just turning the computer off, the result could be corruption of
the file system.
The first command is
shutdown
. We specify a time unit in shutdown to send out a
warning message to users. Additionally, 5 minutes before that time has elapsed, new logins
become disabled. For instance, if time is
+
20, then the system will be shut down in 20 min-
utes and new logins disabled starting in 15 minutes. The time parameter can also be
now
, or
a combination of hours and minutes using the notation hh:mm as in 15:30 to indicate 3:30
p.m. (military time). The shutdown command can include a string that is used as the shut
down warning message. If no message is provided, a default message is used in its place.
The idea behind shutdown is to let users shut down their software themselves. For
instance, a user might be editing a file in vi or emacs and this will allow the user to save the
file and exit. When the time limit expires, shutdown automatically kills all running user
processes and then system processes.
The shutdown command permits other options. The –r option indicates that after shut-
ting down, the system should reboot automatically. The –c option forgoes a time unit so
that it can shut down immediately and the –k option sends out the message and disables
logins, but does not actually shut the system down.
The shutdown program actually leaves the computer running but has shut down the
Linux GUI and most of the Linux services as well as most of the Linux kernel. You are
provided a command line which permits limited input. Three of the commands available
at this point are
halt
,
reboot,
and
poweroff
. The halt instruction terminates the
operating system entirely. The reboot instruction causes Linux to reboot. Finally, power-
off shuts down the hardware after halting the system. It should be noted that shutdown,
halt, reboot, and poweroff are only available to root. The user can start the shutdown and
reboot operations through the GUI. We discuss how users can execute system administra-
tor operations later in the text when we introduce the sudo command.
4.7 CHAPTER REVIEW
Concepts and terms introduced in this chapter:
• Background—Processes which run when needed and when the CPU has time to run
them. Because these processes are not running consistently as with multitasked pro-
cesses, they cannot run interactively, so input and output must be done through files
or offline.
• Batch processing—A form of process management whereby the operating system
assigns a single process to the CPU to execute. Like background processes, batch pro-
cesses do not run interactively so I/O must be done through files or offline.
• Child—A process spawned (started) by another process. In Linux, all processes form
a parent–child relationship with only one process, init, having no parent.
• Context switch—The CPU switching from one process to another by swapping pro-
cess status information.
152
◾
Linux with Operating System Concepts
• Core dump—The result of an abnormally terminating program is a file which stores
a memory dump of the process at the time the error arose. Core dumps can help pro-
grammers debug programs but are often large files.
• EUID—The effective user ID of the running process. Typically, a process runs
under the UID of the user who started the process, but it is sometimes useful for a
process to run under a different UID instead. In such a case, the EUID differs from
the UID.
• Exec—A system call to start a new process in place of the current process. The new
process inherits the old process’ PID and environment.
• FIFO—First in first out, or first come first serve, a simple scheduling strategy for
ordering waiting processes (jobs) in a queue.
• Foreground—Processes which are being executed by the CPU (whether single task-
ing or multitasking) and currently have interactive access by the user.
• Fork—A system call to start a new process which is a duplicate of the parent process.
• Interrupt—A signal received by the CPU to suspend the current process and perform
a context switch to an interrupt handler in order to handle the interrupting situation.
• Interrupt handler—A part of the operating system kernel set up to handle a specific
interrupting situation.
• IRQ—An interrupt request made by a hardware device. Each IRQ is assigned to an
interrupt handler.
• Multiprocessing—A form of process management in which processes are divided up
by the operating system to run on different processors. Although this was reserved
for parallel processing computers in the past, today’s multicore processors can also
accomplish this.
• Multiprogramming—In batch processing, if a process requires time-consuming I/O,
the CPU idles during this time. In multiprogramming, the operating system causes
a context switch so that the current process is moved to an I/O waiting queue while
another process is selected to execute (or resume execution). This is also called coop-
erative multitasking.
• Multitasking—By adding a timer to count the elapsed clock cycles of an executing
process, the operating system can force a context switch when the timer reaches 0
and raises an interrupt. In this way, we improve on multiprogramming so that no
process can monopolize the CPU. This is also known as preemptive multitasking.
Commonly, the processor maintains several running processes and switches off
between them, visiting each in a round robin fashion.
• Multithreading—Multitasking over threads as well as processes.
Managing Processes
◾
153
• Niceness—The value that indicates the priority level of a Linux process. The higher
the niceness, the more willing the process will voluntarily give some of its cycles to
other processes, thus lowering it’s a priority. Niceness values range from
−
20 (least
nice, highest priority) to
+
19 (most nice, lowest priority).
• Orphan—An active process whose parent has terminated. As processes need to
report before they terminate, any process requires a parent. An orphan is automati-
cally adopted by the init process.
• PATH—An environment variable storing various directories. When issuing a com-
mand in a shell, the current directory and all directories in PATH are checked to find
the executable program.
• PID—The process ID number, assigned when the process begins execution. PIDs are
assigned sequentially so that with each new process, the PID increases by 1. PIDs are
used in a variety of instructions including renice and kill.
• Priority—A value used by a scheduler to determine when a process should execute.
In Linux, priorities determine the amount of time the CPU will focus on the process
before moving on to the next process.
• Process—A running program which includes code, data, status (values for its regis-
ters), and resources allocated to it.
• Process management—How the operating system manages running processes. Older
operating systems permitted only two processes to be in memory running at a time,
the user process and the operating system. Later operating systems began using mul-
tiprogramming and multitasking. Today, most operating systems are multithreading
and possibly multiprocessing.
• Process status—The collection of information that describes what a process is cur-
rently doing. This will be the important register values as well as allocated resources
and locations in memory of such objects as the run-time stack and page table.
• Queue—A waiting line, used in operating systems to store processes waiting for
access to the CPU or some device like a printer. Most queues store items in a FIFO
manner but some use a priority scheme.
• Round robin—A scheduling algorithm where processes wait in a queue for service by
the CPU which moves down the queue one process at a time until it reaches then end.
It then resumes with the first process.
• Scheduling—The task of an operating system to organize waiting processes and
decide the order that the processes will gain access to the CPU.
• Signal—A value or message passed to a process indicating the level of seriousness of
the command. This is used in the kill instruction to indicate to what level the operat-
ing system should end a process.
154
◾
Linux with Operating System Concepts
• Single tasking—An old form of process management whereby the operating system
only permits one process to be active and in memory at a time (aside from the oper-
ating system). In single tasking, the next process can only begin once the current
process ends.
• Sleeping—A process’ status which has moved itself into a waiting state for some cir-
cumstance to arise. This may be a specified amount of time (e.g., wait 1 hour) or for
another process to complete its task.
• Sticky bit—An indicator, displayed in a directory’s permissions, denoting that the
directory is set up to allow other users to have write access to the directory but not
to its contents.
• Suspended process—Using control
+
z from the command line, the current process
enters a suspended state so that the user can gain control of the command line. The
user can also use this to move a process from foreground to background.
• System monitor—A Linux GUI program that displays process status information,
CPU, memory, swap space usage and network history for the last minute, and infor-
mation about the file system.
• Threads—A variation of processes whereby a collection of threads share the same
program code and possibly some of the same data but have their own unique data
as well. A context switch between threads is faster than between processes so that
multithreaded operating systems can be more efficient when running threads than a
multitasking system.
• Time slice—The amount of time a process gets with the CPU until a context switch
is forced.
• Zombie—A process which has terminated and freed up its resources but has not
vacated the system because it has to report its end status to its parent, but its parent is
currently unavailable (for instance, the parent may be suspended or sleeping).
Linux commands covered in this chapter:
• bg—To move a command-line issued process to the background.
• fg—To move a command-line issued process to the foreground.
• jobs—List the processes that are suspended or running in the background in this
terminal window.
• kill—To terminate a (or multiple) running process.
• killall—To terminate a collection of processes all owned by the same user or group or
of a particular name (e.g., killall bash).
Managing Processes
◾
155
• halt—When shutting down the Linux operating system, the shutdown command
leaves the system in a text-based mode in which there are few commands available.
Halt is used to stop the system whereas shutdown enters this particular mode.
• init—The first process run by the Linux operating system after kernel initialization so
that init has PID of 1, and is used to start up the system and then become the parent
of any orphaned process. The init process runs during the entire Linux session.
• nice—To alter a process’ default priority, the higher the niceness the lower its priority.
• ps—Print the process snapshot of all running processes in the current terminal win-
dow (or through options, all processes of this user or all users).
• renice—Change the niceness of a running process.
• shutdown—Change the system to single user, text-based mode. From this state, the
system administrator can issue a command of reboot, halt, or poweroff.
• top—A text-based program that displays process status and computer resource usage. It
is interactive and updates itself every 3 seconds (although this duration can be altered).
REVIEW QUESTIONS
1. From the desktop, you start a terminal window. Your default shell is Bash. From the
terminal window, you type an mv command but add & after and then type ps. Show
the tree of processes.
2. What registers make up a process’ status?
3. Why is it important to save the PC value as part of the process’ status?
4. How does batch processing differ from single tasking?
5. How does multiprogramming differ from batch processing?
6. How does multitasking differ from multiprogramming?
7. How does multithreading differ from multitasking?
8. Under what circumstances might a process give up access to the CPU?
9. What does the timer count? What happens when the timer reaches 0?
10. What is the ready queue and what does the ready queue store?
For questions 11–14, assume we have the following partial long listing of three exe-
cutable programs:
permissions
owner group
filename
–rwsr–xr–x user1 group2 foo1
–rwsr–sr–x user1 group2 foo2
–rwxr–xr–x user1 group2 foo3
156
◾
Linux with Operating System Concepts
11. If user foxr runs foo1, foo2, and foo3, which will run under EUID user1?
12. If user foxr runs foo1, foo2, and foo3, which will run under EGID group2?
13. Assume foo1, foo2, and foo3 all access files that are readable and writable only to
user1. If foxr attempts to run all three programs, which can successfully read and
write to this file?
14. If user user1 wishes to change the permissions of foo3 to match those of foo1, what
four-digit value would user1 submit with the chmod command?
15. Assume that you have written an executable program, myprog, in your home directory/
home/username. If you are currently in your home directory, what command would you
issue to execute myprog? If you were instead in some other directory, what command
would you issue to execute myprog using an absolute path to your home directory?
16. You have launched the program prog1 from the command line. This is an interactive
program that will take several minutes to run. You wish to suspend it, start prog2 in
the background, and resume prog1. How do you do this?
17. You have launched the program prog1 from the command line. This is an interactive
program that will take several minutes to run. You wish to move it to the background
and then start prog2. How do you do this?
18. You want to read the man pages for ps and top but you do not want to exit either man
page as you wish to make some comparisons. How can you start both man com-
mands and switch between them?
19. How does a job number, listed when you issue the jobs command, differ from a pro-
cess’ PID?
For questions 20–23, assume you have the following output from the jobs command:
[1]
Stopped prog1
[2]
−
Stopped prog2
[3]
+
Stopped
prog3
[4]
Running prog4
20. Of the processes listed, which one(s) were invoked using & as in prog1 & ?
21. Which process would resume if you typed fg?
22. If you were to kill prog3, which process would resume if you then typed fg?
23. What happens if you type bg 1?
For questions 24–27, use the system monitor GUI (Applications --> System Tools -->
System Monitor). Use the Processes tab for questions 24–26.
24. How many running processes are there? How many sleeping processes are there?
What is a sleeping process?
Managing Processes
◾
157
25. What Nice values do the processes have?
26. What is the range of PIDs?
27. Click on the resources tab. How long is the history of CPU, memory, and network
usage?
For questions 28–32, run top (hint: see the man pages).
28. In the display at the top of the output, what do CPU%us,%sy,%ni,%id,%wa,%hi,%si,
%st each mean?
29. In the list of processes, what are VIRT, RES, and SHR?
30. For status, you might see S, R, D, Z. What do each of these represent?
31. With top running, type f followed by b. What column is added? What does this col-
umn tell you?
32. With top running, type f followed by g. What column is added? What does this col-
umn tell you?
33. What option(s) would you supply to ps to view the parent–child relationships between
processes?
34. What option(s) would you supply to ps to view all running processes?
35. What option(s) would you supply to ps to view processes of only root?
36. How does ps av differ from ps au?
37. Using ps a, you will see the state of running processes. What do each of these stand
for? S, Ss, S
<
, R, R
+
, D, X, Z?
38. Assume you want to run some process, foo, with a lower priority. How would you do
this through the command line? Provide a specific instruction making up your own
priority value.
39. Imagine a process, foo, is running. You want to lower its priority. Describe two ways
to do this.
40. As a normal user, you want to increase one of your process’ priorities. Are you allowed
to?
41. What is the difference between SIGHUP and SIGKILL?
42. Provide an instruction that would stop the process whose PID is 18511.
43. You are running a script call myscript.sh but it seems to be hanging. Explain how you
can stop this process step-by-step.
44. As a system administrator, you detect a user, foxr, is running processes that he should
not be. You decide to end all of his processes. How will you do this?
45. What is the difference between shutdown and halt?
159
C h a p t e r
5
Linux Applications
T
his chapter’s learning objectives are
• Understanding the role of text editors in Linux and specifically how to use vi and/or
emacs
• Learning about the available productivity software available in Linux
• Understanding the use of encryption in Linux and how to apply openssl
• Understanding the basic usage of the mail and sendmail programs
• Understanding the use of IP aliases and addresses and the domain name system
(DNS) of the Internet
• Understanding the basic network commands available in Linux
5.1 INTRODUCTION
One of the strengths of the Linux platform is that much of it has been developed by the
open-source community. Not only does this mean that the operating system itself is
available for free, it also means that a lot of the application software for Linux is free. We
briefly explore the open-source community and what it means to obtain the source code in
Chapter 13. This chapter though is dedicated to examining some of the most popular and
useful Linux applications software.
In this chapter, we will first focus on the two common text editors, vi and emacs. The
vi editor is part of the Linux installation. The emacs editor may or may not be part of your
installation. We also briefly look at the gedit text editor and the most popular of the Linux
productivity software suites, OpenOffice. We limit our examination of OpenOffice because
it should be software that you are able to figure out with little effort based on your experi-
ence using other productivity software such as Microsoft Office. We also briefly examine
the text-based word processor LaTeX. LaTeX is very popular among Linux users as a pow-
erful word processor. Most word processors are WYSIWYGs (what you see is what you get,
160
◾
Linux with Operating System Concepts
or GUI based). LaTeX is a very different kind of word processor and a throwback to the
earliest word processors where commands are embedded within the text itself.
We then focus on encryption software based on the openssl tool. In Section 5.5, we also
examine what encryption is and why we might want to use it. The chapter ends with some
of the network programs that a user may wish to use. There are other network programs
that a system administrator will use and these are covered in Chapter 12.
5.2 TEXT EDITORS
As you are aware by now, operating Linux from the command line has a number of advan-
tages over using the GUI. The primary advantage is the power that Linux commands offer
through the use of options, which can be typically combined in a variety of ways. Although
you can accomplish most tasks similarly through the GUI, it may take a greater amount
of system resources, more time, and more operations (more clicks of the mouse) than you
would face when entering commands. These same things can be said about using a GUI-
based word processor over a text-based text editor.
In Linux, there are two common text-based text editors. These are vi (now called vim
for vi improved) and emacs. The vi editor is the native editor, found in all Linux and Unix
systems. The emacs editor is perhaps as commonly used although in most cases, it has to
be first installed. Linux and Unix users typically consider the editor that they learn first to
be their favorite of the two. Both have their own strengths and weaknesses, and both can
be challenges to learn because they do not offer a traditional interface. A third editor that
comes with the Gnome desktop is gedit. This editor only runs in the GUI environment
though; so, relying on it may be a mistake as you might not always have access to the GUI.
You might wonder why you might use a text editor at all. In Linux, you will often find
that you need a text editor for quick editing jobs. You will create text files for at least two
reasons. First, you will write your own scripts (we examine scripts in Chapter 7). Scripts
are written in text editors. Second, many Linux programs (including scripts) will use text-
based data files for input. Some of the programs that operate on text files include grep, sed,
awk (we look at these in Chapter 6), cat, less, more, diff, cmp, and wc (which we saw in
Chapter 3) just to name a few. A third reason exists for system administrators. Many of the
Linux-operating system services use configuration files that are text based.
Because of the extensive use of text files as a Linux user, and especially a Linux system
administrator, you must be able to create and edit text files. GUI-based editors are simple
enough to master but there are reasons why you might choose a text-based editor instead
that echo the same strengths as using the command line over the GUI to control the oper-
ating system. These are efficiency, speed, and to a lesser extent, power. You might also
find yourself in a situation where you only have access to the command line (e.g., when
remotely logging in or if the GUI itself has failed). The cost of using these text-based text
editors is the challenge of learning them. Both vi and emacs have very different styles of
control (interface) over the GUI-based editors.
It should be noted that you can also use a word processor to create or edit a text file,
but the word process brings a lot of overhead that is not needed, nor desired. Additionally,
you have to remember to save a text-based file as a text file when done or else the word
Linux Applications
◾
161
processor will save it under its own format such that it would not be usable by the Linux
programs mentioned earlier.
Here, we look at vi and emacs in detail and then briefly look at gedit. Although emacs
may require installation, the emacs editor is more powerful than vi and provides a great
deal of support for programming. For instance, if you have the CTRL
++
module in place,
emacs performs brace and parenthesis matching. Or, if you have common lisp installed,
you can actually run your common lisp code from inside the emacs browser. As mentioned
above, Linux users who learn vi first tend to like vi far more than emacs while those who
learn emacs first will favor it over vi.
5.2.1 vi (vim)
The first thing to know about vi is that the interface operates in one of the three modes.
The main mode is known as the
command
mode. In this mode, keystrokes entered oper-
ate as commands rather than characters entered into the document. For instance, typing
G
moves the cursor to the end of the document and typing
k
moves the cursor up one line
rather than entering the letter ‘G’ or ‘k’ into the document.
The other two modes are
Do'stlaringiz bilan baham: |