📄 ch15.htm
字号:
7 gethostbyname
($machine))) {<BR>
8 die
("Machine name $machine not found.\n");<BR>
9 }<BR>
10 print ("Equivalent addresses:\n");<BR>
11 for ($i = 0; $i < @addrlist; $i++) {<BR>
12 @addrbytes
= unpack("C4", $addrlist[$i]);<BR>
13 $realaddr
= join (".", @addrbytes);<BR>
14 print
("\t$realaddr\n");<BR>
15 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">gethostent</FONT></TT>, <TT><FONT FACE="Courier">sethostent</FONT></TT>,
and <TT><FONT FACE="Courier">endhostent</FONT></TT> functions
enable you to get one entry at a time from the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT>
file. The <TT><FONT FACE="Courier">sethostent()</FONT></TT> function
call rewinds the <TT><FONT FACE="Courier">/etc/host</FONT></TT>
file access to ensure that a call to the <TT><FONT FACE="Courier">gethostent()</FONT></TT>
function returns the record. The <TT><FONT FACE="Courier">endhostnet()</FONT></TT>
call closes further accesses to the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT>
file.
<P>
Here's the syntax for the <TT><FONT FACE="Courier">gethostent</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">($name, $aliases, $atype, $alen, $addrs)
= gethostent();</FONT></TT>
</BLOCKQUOTE>
<P>
The first call to <TT><FONT FACE="Courier">gethostent</FONT></TT>
returns the first element and each subsequent call returns the
next element. The five-item list returned by the <TT><FONT FACE="Courier">gethostent()</FONT></TT>
call has the same content as the list returned by <TT><FONT FACE="Courier">gethostbyaddr()</FONT></TT>
or <TT><FONT FACE="Courier">gethostbyname()</FONT></TT>. The list
contains the following:
<UL>
<LI><FONT COLOR=#000000>The first item is the host name corresponding
to the Internet address specified by </FONT><TT><FONT FACE="Courier">$inaddr</FONT></TT>.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$aliases</FONT></TT>
variable is assigned the list of aliases or alternative names
by which the host can be referred.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">addrtype</FONT></TT>,
like <TT><FONT FACE="Courier">inaddrtype</FONT></TT>, is always
<TT><FONT FACE="Courier">&AF_INET</FONT></TT>.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$addrs</FONT></TT>
is a list of addresses (the main address and alternatives) corresponding
to the host node named. Each address is stored as a four-byte
integer.
<LI><FONT COLOR=#000000>The variable </FONT><TT><FONT FACE="Courier">len</FONT></TT>
is the length of the <TT><FONT FACE="Courier">addrs</FONT></TT>
field; this length is always four multiplied by the number of
addresses returned in <TT><FONT FACE="Courier">addrs</FONT></TT>.
</UL>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
Be careful when cycling through <TT><FONT FACE="Courier">/etc/hosts</FONT></TT> because the file may be "faked out" with the use of the name server. Every query into the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT> entry may wind up being a
request for a host name to a site on the Internet. Calling <TT><FONT FACE="Courier">gethostent</FONT></TT> repeatedly might access and overload valuable resources unnecessarily. Use this function with a bit of restraint.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Here's the syntax for the <TT><FONT FACE="Courier">sethostent</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">sethostent (keepopen);</FONT></TT>
</BLOCKQUOTE>
<P>
If <TT><FONT FACE="Courier">keepopen</FONT></TT> is non-zero,
the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT> file is left
open for reading and any previously cached information about the
file is kept in memory. If <TT><FONT FACE="Courier">keepopen</FONT></TT>
is set to zero, any cached information in memory is flushed and
the file is read again with the first entry available for a call
to <TT><FONT FACE="Courier">gethostent()</FONT></TT>. The <TT><FONT FACE="Courier">endhostent()</FONT></TT>
function accepts no parameters and simply closes the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT>
file after flushing any buffers.
<H3><A NAME="WorkingwithProcessGroupsUsingthege">Working with
Process Groups Using the <TT><FONT SIZE=4 FACE="Courier">getpgrp()</FONT></TT><FONT SIZE=4>
Function</FONT></A></H3>
<P>
A process group in the UNIX environment is a set of processes
having the same process group ID. Several process groups can exist
at one time. Each process group is identified by a unique integer,
known as the process group ID. The <TT><FONT FACE="Courier">getpgrp</FONT></TT>
function retrieves the process group ID for a particular process.
<P>
Here's the syntax for the <TT><FONT FACE="Courier">getpgrp</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$pgroup = getpgrp ($pid);</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$pid</FONT></TT> is the process ID whose
group you want to retrieve, and <TT><FONT FACE="Courier">$pgroup</FONT></TT>
is the returned process group ID, which is a scalar value. A call
with no parameters (or <TT><FONT FACE="Courier">$pid</FONT></TT>
set to <TT><FONT FACE="Courier">0</FONT></TT>) returns the process
group ID of the current process.
<P>
You can change the process group of a process, provided you have
the correct permissions, by using the <TT><FONT FACE="Courier">setpgrp</FONT></TT>
function. Here's the syntax for the <TT><FONT FACE="Courier">setpgrp</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">setpgrp ($pid, $groupid);</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$pid</FONT></TT> is the ID of the process
whose group you will change, and <TT><FONT FACE="Courier">$groupid</FONT></TT>
is the process group ID you want to assign this process to.
<H4>The <TT><FONT FACE="Courier">getppid</FONT></TT> Function
</H4>
<P>
Each process in the UNIX environment has its own unique process
ID and parent. (There are some exceptions to this rule, such as
the <TT><FONT FACE="Courier">init</FONT></TT> process, but that
discussion is beyond the scope of this book.) The process ID is
always available as the special variable, <TT><FONT FACE="Courier">$$</FONT></TT>.
To retrieve the process ID for the parent process for itself,
the script can call the <TT><FONT FACE="Courier">getppid()</FONT></TT>
function.
<P>
Here's the syntax for the <TT><FONT FACE="Courier">getppid</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$parentid = getppid();</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$parentid</FONT></TT> is the parent process
ID of your program.
<P>
The most common use of the <TT><FONT FACE="Courier">getppid</FONT></TT>
function is to pass the process IDs of the parent to a child after
a <TT><FONT FACE="Courier">fork</FONT></TT>. You can use <TT><FONT FACE="Courier">getppid</FONT></TT>
with <TT><FONT FACE="Courier">fork</FONT></TT> to ensure that
each of the two processes produced by the <TT><FONT FACE="Courier">fork</FONT></TT>
knows the process ID of the other. Listing 15.7 illustrates a
sample call.
<HR>
<BLOCKQUOTE>
<B>Listing 15.7. Getting the parent process ID.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 #!/usr/bin/perl<BR>
2<BR>
3 $otherid = fork();<BR>
4 if ($otherid == 0) {<BR>
5 $parentID
= getppid();<BR>
6 printf "I am junior $$ child of
$$\n";<BR>
7 } else {<BR>
8 printf
"I am the parent with an ID of $$ \n";<BR>
9 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The output from a sample run would be
<BLOCKQUOTE>
<TT><FONT FACE="Courier">I am junior 5423 child of 5422<BR>
I am the parent with an ID of 5422</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="GettingandChangingthePriorityofaP"><FONT SIZE=5 COLOR=#FF0000>Getting
and Changing the Priority of a Process</FONT></A></H2>
<P>
Processes in the UNIX environments run at a priority level. Processes
with the highest priority are run by the UNIX scheduler before
processes with a lower priority. A Perl script can set its priority
within limits by calling the <TT><FONT FACE="Courier">setpriority</FONT></TT>
function. A process can get information on its priority values
by calling the <TT><FONT FACE="Courier">getpriority</FONT></TT>
function. Priority level numbers are system dependent.
<H3><A NAME="ThesetpriorityFunction">The <TT><FONT SIZE=4 FACE="Courier">setpriority</FONT></TT><FONT SIZE=4>
Function</FONT></A></H3>
<P>
Here's the syntax for the <TT><FONT FACE="Courier">setpriority</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">setpriority ($category, $id, $priority);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">$category</FONT></TT> variable indicates
what processes are to have their priorities altered. The values
that <TT><FONT FACE="Courier">$category</FONT></TT> can take are
listed in the <TT><FONT FACE="Courier">resources.ph</FONT></TT>
file. You can use one of the three following values based on the
action you want the <TT><FONT FACE="Courier">setpriority</FONT></TT>
function to take:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">PRIO_PROCESS</FONT></TT>
</TD><TD WIDTH=452>This call affects only one process whose process ID is specified in <TT><FONT FACE="Courier">$id</FONT></TT>. A value of <TT><FONT FACE="Courier">0</FONT></TT> for <TT><FONT FACE="Courier">$id</FONT></TT> indicates the current process.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">PRIO_PGRP</FONT></TT>
</TD><TD WIDTH=452>This call affects the entire group whose group ID is specified in <TT><FONT FACE="Courier">$id</FONT></TT>. A value of <TT><FONT FACE="Courier">0</FONT></TT> for <TT><FONT FACE="Courier">$id</FONT></TT> indicates the group of the current
process.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">PRIO_USER</FONT></TT>
</TD><TD WIDTH=452>This call affects all the processes belonging to the user whose <TT><FONT FACE="Courier">uid</FONT></TT> is specified in <TT><FONT FACE="Courier">$id</FONT></TT>. A value of <TT><FONT FACE="Courier">0</FONT></TT> for <TT><FONT
FACE="Courier">$id</FONT></TT> indicates the current user with his or her real (not effective) user ID.
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The <TT><FONT FACE="Courier">$priority</FONT></TT> variable is
the new priority for the process, group, or user that you selected.
The numbers can range from <TT><FONT FACE="Courier">0</FONT></TT>
to <TT><FONT FACE="Courier">31</FONT></TT>, or <TT><FONT FACE="Courier">99</FONT></TT>
for a UNIX machine, though this value is a very system-dependent
issue. For example, the priority levels range from -19 to 20 on
a Solaris machine where -8 runs at a higher priority than a process
running at 9.
<H3><A NAME="ThegetpriorityFunction">The <TT><FONT SIZE=4 FACE="Courier">getpriority</FONT></TT><FONT SIZE=4>
Function</FONT></A></H3>
<P>
The function <TT><FONT FACE="Courier">getpriority()</FONT></TT>
gets the current priority for a process, process group, or user.
You can set the priority relative to the value returned by <TT><FONT FACE="Courier">getpriority()</FONT></TT>.
A lower priority causes the affected processes to be set to run
while allowing other higher priority tasks to run. A higher priority
allows the process to hog more system resources.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
The <TT><FONT FACE="Courier">setpriority()</FONT></TT> function is a bit dangerous to use and is therefore only available to the root user on most UNIX systems.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Here's the syntax for the <TT><FONT FACE="Courier">getpriority</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$priority = getpriority ($category, $id);</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$category</FONT></TT> and <TT><FONT FACE="Courier">$id</FONT></TT>
are specified in the same manner as they are for the <TT><FONT FACE="Courier">setpriority()</FONT></TT>
call. For example, look at the following fragment of code, which
is used to raise the priority of a Perl program:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">require "resource.ph";<BR>
$currentpriority = getpriority(&PRIO_PROCESS,$$);<BR>
setpriority (&PRIO_PROCESS, $userid, $currentpriority + 1);</FONT></TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -