📄 processmanagementandcommunication.mht
字号:
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"cannot fork: =
$!"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $pid<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> CHILD_RDR<FONT =
color=3D#00ffff>;</FONT> <FONT color=3D#98fb98>close</FONT> =
CHILD_WTR<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>(</FONT>$line =3D <PARENT_RDR><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Child Pid =
$$ just read this: `$line'\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> PARENT_WTR <FONT =
color=3D#00cd00>"Child Pid $$ is sending this\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> PARENT_RDR<FONT =
color=3D#00ffff>;</FONT> <FONT color=3D#98fb98>close</FONT> =
PARENT_WTR<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch16/pipe6">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -w
# pipe6 - bidirectional communication using socketpair
# "the best ones always go both ways"
</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Socket</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::Handle</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># We say AF_UNIX because although *_LOCAL is the
# POSIX 1003.1g form of the constant, many machines
# still don't have it.
</FONT><FONT color=3D#98fb98>socketpair</FONT><FONT =
color=3D#00ffff>(</FONT>CHILD, PARENT, AF_UNIX, SOCK_STREAM, =
PF_UNSPEC<FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"socketpair: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
CHILD->autoflush<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>);</FONT>
PARENT->autoflush<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$pid =3D =
<FONT color=3D#98fb98>fork</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#98fb98>close</FONT> PARENT<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> CHILD <FONT =
color=3D#00cd00>"Parent Pid $$ is sending this\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>(</FONT>$line =3D <CHILD><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Parent Pid =
$$ just read this: `$line'\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> CHILD<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>waitpid</FONT><FONT =
color=3D#00ffff>(</FONT>$pid,<FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"cannot fork: =
$!"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $pid<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> CHILD<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT =
color=3D#00ffff>(</FONT>$line =3D <PARENT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Child Pid =
$$ just read this: `$line'\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> PARENT <FONT =
color=3D#00cd00>"Child Pid $$ is sending this\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> PARENT<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>socketpair</FONT><FONT =
color=3D#00ffff>(</FONT>READER, WRITER, AF_UNIX, SOCK_STREAM, =
PF_UNSPEC<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#98fb98>shutdown</FONT><FONT =
color=3D#00ffff>(</FONT>READER, <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># no more writing =
for reader
</FONT><FONT color=3D#98fb98>shutdown</FONT><FONT =
color=3D#00ffff>(</FONT>WRITER, <FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># no more reading =
for writer
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN881>Making a Process Look Like a File =
with Named=20
Pipes</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
<TBODY>
<TR>
<TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
#% mkfifo /path/to/named.pipe
#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FIFO, <FONT color=3D#00cd00>"< =
/path/to/named.pipe"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FIFO><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Got: =
$_"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>FIFO<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FIFO, <FONT color=3D#00cd00>"> =
/path/to/named.pipe"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> $!<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> FIFO <FONT color=3D#00cd00>"Smoke =
this.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>FIFO<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
#% mkfifo ~/.plan # isn't this everywhere yet?
#% mknod ~/.plan p # in case you don't have mkfifo
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch16/dateplan">download=
the following standalone program</A></FONT>
#!/usr/bin/perl -w
# dateplan - place current date and time in .plan file
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>FIFO, =
<FONT color=3D#00cd00>"> </FONT><FONT =
color=3D#cdcd00>$ENV</FONT><FONT =
color=3D#00cd00>{HOME}/.plan"</FONT><FONT color=3D#00ffff>)</FONT>
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Couldn't open </FONT><FONT =
color=3D#cdcd00>$ENV</FONT><FONT color=3D#00cd00>{HOME}/.plan for =
writing: $!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> FIFO <FONT color=3D#00cd00>"The =
current time is "</FONT>, <FONT color=3D#ff7f50>scalar</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>localtime</FONT><FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT> FIFO<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>sleep</FONT> <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch16/fifolog">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -w
# fifolog - read and record log msgs from fifo
</FONT>
<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>IO::File</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$SIG</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>ALRM</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#ffa500>sub</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#98fb98>close</FONT><FONT color=3D#00ffff>(</FONT>FIFO<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>};</FONT> <FONT =
color=3D#bebebe># move on to the next queued process
</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#98fb98>alarm</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># =
turn off alarm for blocking open
</FONT> <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FIFO, <FONT color=3D#00cd00>"< =
/tmp/log"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"Can't open /tmp/log : $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>alarm</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># =
you have 1 second to log
</FONT>
$service =3D <FIFO><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>next</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> =
$service<FONT color=3D#00ffff>;</FONT> <FONT color=3D#bebebe># =
interrupted or nothing logged
</FONT> <FONT color=3D#ff7f50>chomp</FONT> $service<FONT =
color=3D#00ffff>;</FONT>
$message =3D <FIFO><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>next</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> =
$message<FONT color=3D#00ffff>;</FONT> <FONT color=3D#bebebe># =
interrupted or nothing logged
</FONT> <FONT color=3D#ff7f50>chomp</FONT> $message<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>alarm</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># =
turn off alarms for message processing
</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$service <FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>"http"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#bebebe># ignoring
</FONT> <FONT color=3D#00ffff>}</FONT> <FONT =
color=3D#ffa500>elsif</FONT> <FONT color=3D#00ffff>(</FONT>$service =
<FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#00cd00>"login"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#bebebe># log to /var/log/login
</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>LOG, <FONT color=3D#00cd00>">> =
/tmp/login"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> LOG <FONT =
color=3D#ff7f50>scalar</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>localtime</FONT><FONT color=3D#00ffff>)</FONT>, <FONT =
color=3D#00cd00>" $service $message\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>(</FONT>LOG<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT =
color=3D#00cd00>"Couldn't log $service $message to /var/log/login : =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>POSIX</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:errno_h<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#cdcd00>$SIG</FONT><FONT color=3D#00ffff>{</FONT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -