📄 linux tutorial-3.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0066)http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux3.htm -->
<HTML><HEAD><TITLE>Linux Tutorial</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2800.1226" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId></HEAD>
<BODY>
<TABLE width=600 border=0>
<TBODY>
<TR>
<TD width=11></TD>
<TD width=575>
<P align=center><I><B><A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/index.htm">Home</A>
- <A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux1.htm">(1)
Shells</A> - <A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux2.htm">(2)
Files & Directories</A> - <A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux3.htm">(3)
Processes</A> - <A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux4.htm">(4)Shell
scripting</A> - <A
href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux5.htm">(5)Makefiles</A></B></I></P></TD></TR>
<TR>
<TD width=11></TD>
<TD width=575><FONT face="Times New Roman" size=5>Linux Tutorial - User's
view (3)</FONT>
<P><FONT face="Times New Roman">Subject Summary</FONT></P>
<OL>
<LI><FONT face="Times New Roman">Processes explained, how paths and
libraries are used</FONT>
<LI><FONT face="Times New Roman">Priorities (niceness) properties and
general process management</FONT>
<LI><FONT face="Times New Roman">Output redirection using files as data
sinks and pipes to allow inter-process communication</FONT> </LI></OL>
<P><FONT face="Times New Roman" size=5><B><I>1. Executing
programs</I></B></FONT></P>
<P><FONT size=4>1.1 Paths and libraries</FONT></P>
<P><FONT face="Times New Roman">Programs may be run by simply typing their
name at the command line. However, the system must be able to locate files
associated to the executable program (remember such files are designated
by an <B>x</B> in the privileges part of the program details - view this
via <B>ls -la</B>) and any associated files the program requires to run
correctly. The system realizes such files with the aid of the <B>$PATH</B>
variable (where is the executable program) and sometimes the
<B>$LD_LIBRARY_PATH</B> variable (identifying the files that contain the
library code required by the program at run time). Therefore, you must
make sure that the <B>$PATH</B> (and possibly <B>$LD_LIBRARY_PATH</B>) are
up to date (usually set in your <B>.profile</B>). Details of this are
mentioned in "<I>Linux Tutorial - User's view (1)</I>". For example, if
you wish to add two further directories in which you want the system to
search for a program, say <B>/bin </B>and <B>/usr/bin</B>, you must
type:</FONT></P>
<UL type=circle>
<LI><B>PATH=$HOME:/bin:/usr/bin</B>
<LI><B>NOTE - The above command may well remove some important PATH
information. WHen setting PATH you should usually put $PATH in the right
side of the assignments:</B>
<LI><B>PATH=$PATH:$HOME:/bin:/usr/bin</B> </LI></UL>
<P>Notice how the two directories are separated by a colon. Notice also
that we are including <B>$HOME</B> in this as well, ensuring our home
directory is in the <B>$PATH</B>. Most programs are put in the directories
<B>/usr/local/bin</B> or <B>/usr/bin</B> by the system administrator so
these should be in your <B>$PATH</B>. The <B>.</B> (full stop) is used to
denote "current directory". It is considered bad practice to put this in
the <B>$PATH</B>, simply because it is thought appropriate to quite
specifically identify the actual location of files to execute (there may
be ambiguity if some files share the same name in your current directory
and this is sometimes considered a security risk). If you have a file in
your current directory, say <B>doit</B>, and you wish to execute this file
in your current directory you simply write:</P>
<UL type=circle>
<LI><B>./doit</B> </LI></UL>
<P>The <B>./</B> means the <B>doit</B> in the current directory, not the
one that may be specified by <B>$PATH</B>.</P>
<P><FONT size=4>1.2 Processes</FONT></P>
<P>The execution of a program is encapsulated within a process. A process
is simply an instance of a program execution. You may view the processes
running on your computer via the <B>ps</B> command: </P>
<UL type=circle>
<LI><B>ps</B> - Processes you have run
<LI><B>ps -A</B> - All processes running on the machine. </LI></UL>
<P>You can <B>man ps</B> to get more detail of what other parameters
<B>ps</B> can take.</P>
<P>Sometimes you may want to run a process from the command line and
continue to use the command line. Usually, when you run a process from the
command line the command line blocks until the process finishes running.
To continue using the command line after running a program, say
<B>netscape</B>, then add a <B>&</B> (ampersand) symbol after the
program name:</P>
<UL type=circle>
<LI><B>netscape&</B> </LI></UL>
<P>After running the process you may wish to remove (kill) the
process from running (especially if the program provides no exit
mechanism). If you didn't use the <B>&</B> symbol you can do this by
pressing the keys <B>ctrl</B> and <B>c</B> together in command line window
you ran the process from. However, if you used the <B>&</B> symbol you
will have to put a bit more effort into killing a process. Every process
has a process ID (you can see these via the <B>ps</B> command). Once you
know the process ID of the process you wish to kill you can use the
<B>kill</B> command. For example, if netscape has a process ID of 435 then
we may kill it using the following command:</P>
<UL type=circle>
<LI><B>kill -9 435</B> </LI></UL>
<P>An interesting aspect of processes is that they may be given a priority
by the user. The priority is used by an operating system to determine how
much time a process may have on the CPU. However, as the operating system
will almost certainly be pre-emptive (the operating system, not the user,
decides what process should execute at any given time on the CPU), the
priority system tends to be a very rough guide. Priority settings are
commonly referred to a <B>niceness</B> in UNIX systems. The priority of a
process may be dictated by the user via the <B>renice</B> command. The
range of "niceness" may be between -20 and +20. The greater the number the
more "nice" a process is (i.e., the lower the priority). Basically, you
are been nice by reducing the priority of your processes. For example, if
we want a low priority for netscape then we may type:</P>
<UL type=circle>
<LI><CODE><B>renice +19 435 (note - as a regular user you
can't make processes 'more important' with renice)</B></CODE> </LI></UL>
<P>To actually see how resource hungry processes are (what their CPU and
memory usage is) you can use the <B>top</B> command:</P>
<UL type=circle>
<LI><B>top</B> </LI></UL>
<P><FONT size=5><B><I>2. Redirection</I></B></FONT></P>
<P><B>2.1 Using files</B></P>
<P>Typing in commands (such as <B>ls</B>) results in an output consisting
of text. By default, this text is displayed to the screen (command line).
However, it is sometimes desirable to log this output within a file. This
makes it much easier to examine, especially if there is a substantial
amount of text. There are three redirection symbols available for use to
enable output to be sent to a file:</P>
<UL type=circle>
<LI><B>ls > myfiles</B> - If 'myfiles' file exist in your current
directory it will be overwritten without any type of warning.
<LI><B>ls >> myfiles</B> - To output Linux-commands result to end
of file. If file exist , it will be opened and new information/data will
be written to end of file, without losing previous information/data, And
if file is not exist, then new file is created.
<LI><B>cat < myfiles</B> - To take input to Linux-command from file
instead of key-board. (Check to see what the cat command does by using
<B>man</B>). </LI></UL>
<P><FONT size=4>2.2 Pipes</FONT></P>
<P>Pipes are used to connect the output of one program to another. You can
think of a pipe in terms of a real pipe connecting a data source (sending
program) with a data sink (receiving program). For example, when you use
<B>ps</B> there may be more resultant data that you can see on a single
screen. Therefore, a pipe may be used between <B>ps</B> and the
<B>more</B> command to enable prompting and scrolling of a screen of data
at a time:</P>
<UL type=circle>
<LI><B>ps | more</B> </LI></UL>
<HR>
<P><FONT size=5>Exercices</FONT></P>
<P><B>1)</B> Add the "bin" directory you created in the last exercise to
your PATH.</P>
<P><B>2)</B> If "nedit" is not running, then run it. Now change the
priority of "nedit" to make it <I>least important</I>. </P>
<P><B>3)</B> Kill the "nedit" process from the command line.</P>
<P><B>4)</B> Create a file named "listing.txt" in your directory "tmp"
that contains the list of all the files in tmp.</P>
<P> </P>
<P><BR></P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P><BR></P>
<P> </P>
<P> </P></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -