home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Magazin: Amiga-CD 1996 July
/
AMIGA_1996_7.BIN
/
ausgabe_7_96
/
pd-programmierung
/
perl5_002bin.lha
/
man
/
catp
/
perlipc.0
< prev
next >
Wrap
Text File
|
1996-03-02
|
102KB
|
1,255 lines
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
NNNNAAAAMMMMEEEE
perlipc - Perl interprocess communication (signals, fifos,
pipes, safe subprocceses, sockets, and semaphores)
DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
The basic IPC facilities of Perl are built out of the good
old Unix signals, named pipes, pipe opens, the Berkeley
socket routines, and SysV IPC calls. Each is used in
slightly different situations.
SSSSiiiiggggnnnnaaaallllssss
Perl uses a simple signal handling model: the %%%%SSSSIIIIGGGG hash
contains names or references of user-installed signal
handlers. These handlers will be called with an argument
which is the name of the signal that triggered it. A
signal may be generated intentionally from a particular
keyboard sequence like control-C or control-Z, sent to you
from an another process, or triggered automatically by the
kernel when special events transpire, like a child process
exiting, your process running out of stack space, or
hitting file size limit.
For example, to trap an interrupt signal, set up a handler
like this. Notice how all we do is set with a global
variable and then raise an exception. That's because on
most systems libraries are not re-entrant, so calling any
_p_r_i_n_t_(_) functions (or even anything that needs to
_m_a_l_l_o_c(3) more memory) could in theory trigger a memory
fault and subsequent core dump.
ssssuuuubbbb ccccaaaattttcccchhhh____zzzzaaaapppp {{{{
mmmmyyyy $$$$ssssiiiiggggnnnnaaaammmmeeee ==== sssshhhhiiiifffftttt;;;;
$$$$sssshhhhuuuucccckkkkssss++++++++;;;;
ddddiiiieeee """"SSSSoooommmmeeeebbbbooooddddyyyy sssseeeennnntttt mmmmeeee aaaa SSSSIIIIGGGG$$$$ssssiiiiggggnnnnaaaammmmeeee"""";;;;
}}}}
$$$$SSSSIIIIGGGG{{{{IIIINNNNTTTT}}}} ==== ''''ccccaaaattttcccchhhh____zzzzaaaapppp'''';;;; #### ccccoooouuuulllldddd ffffaaaaiiiillll iiiinnnn mmmmoooodddduuuulllleeeessss
$$$$SSSSIIIIGGGG{{{{IIIINNNNTTTT}}}} ==== \\\\&&&&ccccaaaattttcccchhhh____zzzzaaaapppp;;;; #### bbbbeeeesssstttt ssssttttrrrraaaatttteeeeggggyyyy
The names of the signals are the ones listed out by kkkkiiiillllllll
----llll on your system, or you can retrieve them from the
Config module. Set up an @@@@ssssiiiiggggnnnnaaaammmmeeee list indexed by number
to get the name and a %%%%ssssiiiiggggnnnnoooo table indexed by name to get
the number:
uuuusssseeee CCCCoooonnnnffffiiiigggg;;;;
ddddeeeeffffiiiinnnneeeedddd $$$$CCCCoooonnnnffffiiiigggg{{{{ssssiiiigggg____nnnnaaaammmmeeee}}}} |||||||| ddddiiiieeee """"NNNNoooo ssssiiiiggggssss????"""";;;;
ffffoooorrrreeeeaaaacccchhhh $$$$nnnnaaaammmmeeee ((((sssspppplllliiiitttt(((('''' '''',,,, $$$$CCCCoooonnnnffffiiiigggg{{{{ssssiiiigggg____nnnnaaaammmmeeee}}}})))))))) {{{{
$$$$ssssiiiiggggnnnnoooo{{{{$$$$nnnnaaaammmmeeee}}}} ==== $$$$iiii;;;;
$$$$ssssiiiiggggnnnnaaaammmmeeee[[[[$$$$iiii]]]] ==== $$$$nnnnaaaammmmeeee;;;;
$$$$iiii++++++++;;;;
}}}}
So to check whether signal 17 and SIGALRM were the same,
just do this:
30/Jan/96 perl 5.002 with 1
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
pppprrrriiiinnnntttt """"ssssiiiiggggnnnnaaaallll ####11117777 ==== $$$$ssssiiiiggggnnnnaaaammmmeeee[[[[11117777]]]]\\\\nnnn"""";;;;
iiiiffff (((($$$$ssssiiiiggggnnnnoooo{{{{AAAALLLLRRRRMMMM}}}})))) {{{{
pppprrrriiiinnnntttt """"SSSSIIIIGGGGAAAALLLLRRRRMMMM iiiissss $$$$ssssiiiiggggnnnnoooo{{{{AAAALLLLRRRRMMMM}}}}\\\\nnnn"""";;;;
}}}}
You may also choose to assign the strings ''''IIIIGGGGNNNNOOOORRRREEEE'''' or
''''DDDDEEEEFFFFAAAAUUUULLLLTTTT'''' as the handler, in which case Perl will try to
discard the signal or do the default thing. Some signals
can be neither trapped nor ignored, such as the KILL and
STOP (but not the TSTP) signals. One strategy for
temporarily ignoring signals is to use a _l_o_c_a_l_(_)
statement, which will be automatically restored once your
block is exited. (Remember that _l_o_c_a_l_(_) values are
"inherited" by functions called from within that block.)
ssssuuuubbbb pppprrrreeeecccciiiioooouuuussss {{{{
llllooooccccaaaallll $$$$SSSSIIIIGGGG{{{{IIIINNNNTTTT}}}} ==== ''''IIIIGGGGNNNNOOOORRRREEEE'''';;;;
&&&&mmmmoooorrrreeee____ffffuuuunnnnccccttttiiiioooonnnnssss;;;;
}}}}
ssssuuuubbbb mmmmoooorrrreeee____ffffuuuunnnnccccttttiiiioooonnnnssss {{{{
#### iiiinnnntttteeeerrrrrrrruuuuppppttttssss ssssttttiiiillllllll iiiiggggnnnnoooorrrreeeedddd,,,, ffffoooorrrr nnnnoooowwww............
}}}}
Sending a signal to a negative process ID means that you
send the signal to the entire Unix process-group. This
code send a hang-up signal to all processes in the current
process group _e_x_c_e_p_t _f_o_r the current process itself:
{{{{
llllooooccccaaaallll $$$$SSSSIIIIGGGG{{{{HHHHUUUUPPPP}}}} ==== ''''IIIIGGGGNNNNOOOORRRREEEE'''';;;;
kkkkiiiillllllll HHHHUUUUPPPP ====>>>> ----$$$$$$$$;;;;
#### ssssnnnnaaaazzzzzzzzyyyy wwwwrrrriiiittttiiiinnnngggg ooooffff:::: kkkkiiiillllllll((((''''HHHHUUUUPPPP'''',,,, ----$$$$$$$$))))
}}}}
Another interesting signal to send is signal number zero.
This doesn't actually affect another process, but instead
checks whether it's alive or has changed its UID.
uuuunnnnlllleeeessssssss ((((kkkkiiiillllllll 0000 ====>>>> $$$$kkkkiiiidddd____ppppiiiidddd)))) {{{{
wwwwaaaarrrrnnnn """"ssssoooommmmeeeetttthhhhiiiinnnngggg wwwwiiiicccckkkkeeeedddd hhhhaaaappppppppeeeennnneeeedddd ttttoooo $$$$kkkkiiiidddd____ppppiiiidddd"""";;;;
}}}}
You might also want to employ anonymous functions for
simple signal handlers:
$$$$SSSSIIIIGGGG{{{{IIIINNNNTTTT}}}} ==== ssssuuuubbbb {{{{ ddddiiiieeee """"\\\\nnnnOOOOuuuuttttttttaaaa hhhheeeerrrreeee!!!!\\\\nnnn"""" }}}};;;;
But that will be problematic for the more complicated
handlers that need to re-install themselves. Because
Perl's signal mechanism is currently based on the
_s_i_g_n_a_l(3) function from the C library, you may somtimes be
so misfortunate as to run on systems where that function
is "broken", that is, it behaves in the old unreliable
SysV way rather than the newer, more reasonable BSD and
30/Jan/96 perl 5.002 with 2
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
POSIX fashion. So you'll see defensive people writing
signal handlers like this:
ssssuuuubbbb RRRREEEEAAAAPPPPEEEERRRR {{{{
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;; #### llllooooaaaatttthhhheeee ssssyyyyssssVVVV
$$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== wwwwaaaaiiiitttt;;;;
}}}}
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;;
#### nnnnoooowwww ddddoooo ssssoooommmmeeeetttthhhhiiiinnnngggg tttthhhhaaaatttt ffffoooorrrrkkkkssss............
or even the more elaborate:
uuuusssseeee PPPPOOOOSSSSIIIIXXXX """"wwwwaaaaiiiitttt____hhhh"""";;;;
ssssuuuubbbb RRRREEEEAAAAPPPPEEEERRRR {{{{
mmmmyyyy $$$$cccchhhhiiiilllldddd;;;;
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;; #### llllooooaaaatttthhhheeee ssssyyyyssssVVVV
wwwwhhhhiiiilllleeee (((($$$$cccchhhhiiiilllldddd ==== wwwwaaaaiiiittttppppiiiidddd((((----1111,,,,WWWWNNNNOOOOHHHHAAAANNNNGGGG)))))))) {{{{
$$$$KKKKiiiidddd____SSSSttttaaaattttuuuussss{{{{$$$$cccchhhhiiiilllldddd}}}} ==== $$$$????;;;;
}}}}
}}}}
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;;
#### ddddoooo ssssoooommmmeeeetttthhhhiiiinnnngggg tttthhhhaaaatttt ffffoooorrrrkkkkssss............
Signal handling is also used for timeouts in Unix, While
safely protected within an eeeevvvvaaaallll{{{{}}}} block, you set a signal
handler to trap alarm signals and then schedule to have
one delivered to you in some number of seconds. Then try
your blocking operation, clearing the alarm when it's done
but not before you've exited your eeeevvvvaaaallll{{{{}}}} block. If it
goes off, you'll use _d_i_e_(_) to jump out of the block, much
as you might using _l_o_n_g_j_m_p_(_) or _t_h_r_o_w_(_) in other
languages.
Here's an example:
eeeevvvvaaaallll {{{{
llllooooccccaaaallll $$$$SSSSIIIIGGGG{{{{AAAALLLLRRRRMMMM}}}} ==== ssssuuuubbbb {{{{ ddddiiiieeee """"aaaallllaaaarrrrmmmm cccclllloooocccckkkk rrrreeeessssttttaaaarrrrtttt"""" }}}};;;;
aaaallllaaaarrrrmmmm 11110000;;;;
fffflllloooocccckkkk((((FFFFHHHH,,,, 2222))));;;; #### bbbblllloooocccckkkkiiiinnnngggg wwwwrrrriiiitttteeee lllloooocccckkkk
aaaallllaaaarrrrmmmm 0000;;;;
}}}};;;;
iiiiffff (((($$$$@@@@ aaaannnndddd $$$$@@@@ !!!!~~~~ ////aaaallllaaaarrrrmmmm cccclllloooocccckkkk rrrreeeessssttttaaaarrrrtttt////)))) {{{{ ddddiiiieeee }}}}
For more complex signal handling, you might see the
standard POSIX module. Lamentably, this is almost
entirely undocumented, but the _t_/_l_i_b_/_p_o_s_i_x_._t file from the
Perl source distribution has some examples in it.
NNNNaaaammmmeeeedddd PPPPiiiippppeeeessss
A named pipe (often referred to as a FIFO) is an old Unix
IPC mechanism for processes communicating on the same
machine. It works just like a regular, connected
anonymous pipes, except that the processes rendezvous
using a filename and don't have to be related.
30/Jan/96 perl 5.002 with 3
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
To create a named pipe, use the Unix command _m_k_n_o_d(1) or
on some systems, _m_k_f_i_f_o(1). These may not be in your
normal path.
#### ssssyyyysssstttteeeemmmm rrrreeeettttuuuurrrrnnnn vvvvaaaallll iiiissss bbbbaaaacccckkkkwwwwaaaarrrrddddssss,,,, ssssoooo &&&&&&&& nnnnooootttt ||||||||
####
$$$$EEEENNNNVVVV{{{{PPPPAAAATTTTHHHH}}}} ....==== """"::::////eeeettttcccc::::////uuuussssrrrr////eeeettttcccc"""";;;;
iiiiffff (((( ssssyyyysssstttteeeemmmm((((''''mmmmkkkknnnnoooodddd'''',,,, $$$$ppppaaaatttthhhh,,,, ''''pppp''''))))
&&&&&&&& ssssyyyysssstttteeeemmmm((((''''mmmmkkkkffffiiiiffffoooo'''',,,, $$$$ppppaaaatttthhhh)))) ))))
{{{{
ddddiiiieeee """"mmmmkkkk{{{{nnnnoooodddd,,,,ffffiiiiffffoooo}}}} $$$$ppppaaaatttthhhh ffffaaaaiiiilllleeeedddd;;;;
}}}}
A fifo is convenient when you want to connect a process to
an unrelated one. When you open a fifo, the program will
block until there's something on the other end.
For example, let's say you'd like to have your _._s_i_g_n_a_t_u_r_e
file be a named pipe that has a Perl program on the other
end. Now every time any program (like a mailer,
newsreader, finger program, etc.) tries to read from that
file, the reading program will block and your program will
supply the the new signature. We'll use the pipe-checking
file test ----pppp to find out whether anyone (or anything) has
accidentally removed our fifo.
cccchhhhddddiiiirrrr;;;; #### ggggoooo hhhhoooommmmeeee
$$$$FFFFIIIIFFFFOOOO ==== ''''....ssssiiiiggggnnnnaaaattttuuuurrrreeee'''';;;;
$$$$EEEENNNNVVVV{{{{PPPPAAAATTTTHHHH}}}} ....==== """"::::////eeeettttcccc::::////uuuussssrrrr////ggggaaaammmmeeeessss"""";;;;
wwwwhhhhiiiilllleeee ((((1111)))) {{{{
uuuunnnnlllleeeessssssss ((((----pppp $$$$FFFFIIIIFFFFOOOO)))) {{{{
uuuunnnnlllliiiinnnnkkkk $$$$FFFFIIIIFFFFOOOO;;;;
ssssyyyysssstttteeeemmmm((((''''mmmmkkkknnnnoooodddd'''',,,, $$$$FFFFIIIIFFFFOOOO,,,, ''''pppp''''))))
&&&&&&&& ddddiiiieeee """"ccccaaaannnn''''tttt mmmmkkkknnnnoooodddd $$$$FFFFIIIIFFFFOOOO:::: $$$$!!!!"""";;;;
}}}}
#### nnnneeeexxxxtttt lllliiiinnnneeee bbbblllloooocccckkkkssss uuuunnnnttttiiiillll tttthhhheeeerrrreeee''''ssss aaaa rrrreeeeaaaaddddeeeerrrr
ooooppppeeeennnn ((((FFFFIIIIFFFFOOOO,,,, """">>>> $$$$FFFFIIIIFFFFOOOO"""")))) |||||||| ddddiiiieeee """"ccccaaaannnn''''tttt wwwwrrrriiiitttteeee $$$$FFFFIIIIFFFFOOOO:::: $$$$!!!!"""";;;;
pppprrrriiiinnnntttt FFFFIIIIFFFFOOOO """"JJJJoooohhhhnnnn SSSSmmmmiiiitttthhhh ((((ssssmmmmiiiitttthhhh\\\\@@@@hhhhoooosssstttt....oooorrrrgggg))))\\\\nnnn"""",,,, ````ffffoooorrrrttttuuuunnnneeee ----ssss````;;;;
cccclllloooosssseeee FFFFIIIIFFFFOOOO;;;;
sssslllleeeeeeeepppp 2222;;;; #### ttttoooo aaaavvvvooooiiiidddd dddduuuupppp ssssiiiiggggssss
}}}}
UUUUssssiiiinnnngggg _o_p_e_n_(_) for IPC
Perl's basic _o_p_e_n_(_) statement can also be used for
unidirectional interprocess communication by either
appending or prepending a pipe symbol to the second
argument to _o_p_e_n_(_). Here's how to start something up a
child process you intend to write to:
30/Jan/96 perl 5.002 with 4
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
ooooppppeeeennnn((((SSSSPPPPOOOOOOOOLLLLEEEERRRR,,,, """"|||| ccccaaaatttt ----vvvv |||| llllpppprrrr ----hhhh 2222>>>>////ddddeeeevvvv////nnnnuuuullllllll""""))))
|||||||| ddddiiiieeee """"ccccaaaannnn''''tttt ffffoooorrrrkkkk:::: $$$$!!!!"""";;;;
llllooooccccaaaallll $$$$SSSSIIIIGGGG{{{{PPPPIIIIPPPPEEEE}}}} ==== ssssuuuubbbb {{{{ ddddiiiieeee """"ssssppppoooooooolllleeeerrrr ppppiiiippppeeee bbbbrrrrooookkkkeeee"""" }}}};;;;
pppprrrriiiinnnntttt SSSSPPPPOOOOOOOOLLLLEEEERRRR """"ssssttttuuuuffffffff\\\\nnnn"""";;;;
cccclllloooosssseeee SSSSPPPPOOOOOOOOLLLLEEEERRRR |||||||| ddddiiiieeee """"bbbbaaaadddd ssssppppoooooooollll:::: $$$$!!!! $$$$????"""";;;;
And here's how to start up a child process you intend to
read from:
ooooppppeeeennnn((((SSSSTTTTAAAATTTTUUUUSSSS,,,, """"nnnneeeettttssssttttaaaatttt ----aaaannnn 2222>>>>&&&&1111 ||||""""))))
|||||||| ddddiiiieeee """"ccccaaaannnn''''tttt ffffoooorrrrkkkk:::: $$$$!!!!"""";;;;
wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTAAAATTTTUUUUSSSS>>>>)))) {{{{
nnnneeeexxxxtttt iiiiffff ////^^^^((((ttttccccpppp||||uuuuddddpppp))))////;;;;
pppprrrriiiinnnntttt;;;;
}}}}
cccclllloooosssseeee SSSSPPPPOOOOOOOOLLLLEEEERRRR |||||||| ddddiiiieeee """"bbbbaaaadddd nnnneeeettttssssttttaaaatttt:::: $$$$!!!! $$$$????"""";;;;
If one can be sure that a particular program is a Perl
script that is expecting filenames in @@@@AAAARRRRGGGGVVVV, the clever
programmer can write something like this:
$$$$ pppprrrrooooggggrrrraaaammmm ffff1111 """"ccccmmmmdddd1111||||"""" ---- ffff2222 """"ccccmmmmdddd2222||||"""" ffff3333 <<<< ttttmmmmppppffffiiiilllleeee
and irrespective of which shell it's called from, the Perl
program will read from the file _f_1, the process _c_m_d_1,
standard input (_t_m_p_f_i_l_e in this case), the _f_2 file, the
_c_m_d_2 command, and finally the _f_3 file. Pretty nifty, eh?
You might notice that you could use backticks for much the
same effect as opening a pipe for reading:
pppprrrriiiinnnntttt ggggrrrreeeepppp {{{{ !!!!////^^^^((((ttttccccpppp||||uuuuddddpppp))))//// }}}} ````nnnneeeettttssssttttaaaatttt ----aaaannnn 2222>>>>&&&&1111````;;;;
ddddiiiieeee """"bbbbaaaadddd nnnneeeettttssssttttaaaatttt"""" iiiiffff $$$$????;;;;
While this is true on the surface, it's much more
efficient to process the file one line or record at a time
because then you don't have to read the whole thing into
memory at once. It also gives you finer control of the
whole process, letting you to kill off the child process
early if you'd like.
Be careful to check both the _o_p_e_n_(_) and the _c_l_o_s_e_(_) return
values. If you're _w_r_i_t_i_n_g to a pipe, you should also trap
SIGPIPE. Otherwise, think of what happens when you start
up a pipe to a command that doesn't exist: the _o_p_e_n_(_) will
in all likelihood succeed (it only reflects the _f_o_r_k_(_)'s
success), but then your output will fail--spectacularly.
Perl can't know whether the command worked because your
command is actually running in a separate process whose
_e_x_e_c_(_) might have failed. Therefore, while readers of
bogus commands just return a quick end of file, writers to
bogus command will trigger a signal they'd better be
prepared to handle. Consider:
30/Jan/96 perl 5.002 with 5
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
ooooppppeeeennnn((((FFFFHHHH,,,, """"||||bbbboooogggguuuussss""""))));;;;
pppprrrriiiinnnntttt FFFFHHHH """"bbbbaaaannnngggg\\\\nnnn"""";;;;
cccclllloooosssseeee FFFFHHHH;;;;
SSSSaaaaffffeeee PPPPiiiippppeeee OOOOppppeeeennnnssss
Another interesting approach to IPC is making your single
program go multiprocess and communicate between (or even
amongst) yourselves. The _o_p_e_n_(_) function will accept a
file argument of either """"----||||"""" or """"||||----"""" to do a very
interesting thing: it forks a child connected to the
filehandle you've opened. The child is running the same
program as the parent. This is useful for safely opening
a file when running under an assumed UID or GID, for
example. If you open a pipe _t_o minus, you can write to
the filehandle you opened and your kid will find it in his
STDIN. If you open a pipe _f_r_o_m minus, you can read from
the filehandle you opened whatever your kid writes to his
STDOUT.
uuuusssseeee EEEEnnnngggglllliiiisssshhhh;;;;
mmmmyyyy $$$$sssslllleeeeeeeepppp____ccccoooouuuunnnntttt ==== 0000;;;;
ddddoooo {{{{
$$$$ppppiiiidddd ==== ooooppppeeeennnn((((KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE,,,, """"||||----""""))));;;;
uuuunnnnlllleeeessssssss ((((ddddeeeeffffiiiinnnneeeedddd $$$$ppppiiiidddd)))) {{{{
wwwwaaaarrrrnnnn """"ccccaaaannnnnnnnooootttt ffffoooorrrrkkkk:::: $$$$!!!!"""";;;;
ddddiiiieeee """"bbbbaaaaiiiilllliiiinnnngggg oooouuuutttt"""" iiiiffff $$$$sssslllleeeeeeeepppp____ccccoooouuuunnnntttt++++++++ >>>> 6666;;;;
sssslllleeeeeeeepppp 11110000;;;;
}}}}
}}}} uuuunnnnttttiiiillll ddddeeeeffffiiiinnnneeeedddd $$$$ppppiiiidddd;;;;
iiiiffff (((($$$$ppppiiiidddd)))) {{{{ #### ppppaaaarrrreeeennnntttt
pppprrrriiiinnnntttt KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE @@@@ssssoooommmmeeee____ddddaaaattttaaaa;;;;
cccclllloooosssseeee((((KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE)))) |||||||| wwwwaaaarrrrnnnn """"kkkkiiiidddd eeeexxxxiiiitttteeeedddd $$$$????"""";;;;
}}}} eeeellllsssseeee {{{{ #### cccchhhhiiiilllldddd
(((($$$$EEEEUUUUIIIIDDDD,,,, $$$$EEEEGGGGIIIIDDDD)))) ==== (((($$$$UUUUIIIIDDDD,,,, $$$$GGGGIIIIDDDD))));;;; #### ssssuuuuiiiidddd pppprrrrooooggggssss oooonnnnllllyyyy
ooooppppeeeennnn ((((FFFFIIIILLLLEEEE,,,, """">>>> ////ssssaaaaffffeeee////ffffiiiilllleeee""""))))
|||||||| ddddiiiieeee """"ccccaaaannnn''''tttt ooooppppeeeennnn ////ssssaaaaffffeeee////ffffiiiilllleeee:::: $$$$!!!!"""";;;;
wwwwhhhhiiiilllleeee ((((<<<<SSSSTTTTDDDDIIIINNNN>>>>)))) {{{{
pppprrrriiiinnnntttt FFFFIIIILLLLEEEE;;;; #### cccchhhhiiiilllldddd''''ssss SSSSTTTTDDDDIIIINNNN iiiissss ppppaaaarrrreeeennnntttt''''ssss KKKKIIIIDDDD
}}}}
eeeexxxxiiiitttt;;;; #### ddddoooonnnn''''tttt ffffoooorrrrggggeeeetttt tttthhhhiiiissss
}}}}
Another common use for this construct is when you need to
execute something without the shell's interference. With
_s_y_s_t_e_m_(_), it's straigh-forward, but you can't use a pipe
open or backticks safely. That's because there's no way
to stop the shell from getting its hands on your
arguments. Instead, use lower-level control to call
_e_x_e_c_(_) directly.
30/Jan/96 perl 5.002 with 6
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
Here's a safe backtick or pipe open for read:
#### aaaadddddddd eeeerrrrrrrroooorrrr pppprrrroooocccceeeessssssssiiiinnnngggg aaaassss aaaabbbboooovvvveeee
$$$$ppppiiiidddd ==== ooooppppeeeennnn((((KKKKIIIIDDDD____TTTTOOOO____RRRREEEEAAAADDDD,,,, """"----||||""""))));;;;
iiiiffff (((($$$$ppppiiiidddd)))) {{{{ #### ppppaaaarrrreeeennnntttt
wwwwhhhhiiiilllleeee ((((<<<<KKKKIIIIDDDD____TTTTOOOO____RRRREEEEAAAADDDD>>>>)))) {{{{
#### ddddoooo ssssoooommmmeeeetttthhhhiiiinnnngggg iiiinnnntttteeeerrrreeeessssttttiiiinnnngggg
}}}}
cccclllloooosssseeee((((KKKKIIIIDDDD____TTTTOOOO____RRRREEEEAAAADDDD)))) |||||||| wwwwaaaarrrrnnnn """"kkkkiiiidddd eeeexxxxiiiitttteeeedddd $$$$????"""";;;;
}}}} eeeellllsssseeee {{{{ #### cccchhhhiiiilllldddd
(((($$$$EEEEUUUUIIIIDDDD,,,, $$$$EEEEGGGGIIIIDDDD)))) ==== (((($$$$UUUUIIIIDDDD,,,, $$$$GGGGIIIIDDDD))));;;; #### ssssuuuuiiiidddd oooonnnnllllyyyy
eeeexxxxeeeecccc(((($$$$pppprrrrooooggggrrrraaaammmm,,,, @@@@ooooppppttttiiiioooonnnnssss,,,, @@@@aaaarrrrggggssss))))
|||||||| ddddiiiieeee """"ccccaaaannnn''''tttt eeeexxxxeeeecccc pppprrrrooooggggrrrraaaammmm:::: $$$$!!!!"""";;;;
#### NNNNOOOOTTTTRRRREEEEAAAACCCCHHHHEEEEDDDD
}}}}
And here's a safe pipe open for writing:
#### aaaadddddddd eeeerrrrrrrroooorrrr pppprrrroooocccceeeessssssssiiiinnnngggg aaaassss aaaabbbboooovvvveeee
$$$$ppppiiiidddd ==== ooooppppeeeennnn((((KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE,,,, """"||||----""""))));;;;
$$$$SSSSIIIIGGGG{{{{AAAALLLLRRRRMMMM}}}} ==== ssssuuuubbbb {{{{ ddddiiiieeee """"wwwwhhhhooooooooppppssss,,,, $$$$pppprrrrooooggggrrrraaaammmm ppppiiiippppeeee bbbbrrrrooookkkkeeee"""" }}}};;;;
iiiiffff (((($$$$ppppiiiidddd)))) {{{{ #### ppppaaaarrrreeeennnntttt
ffffoooorrrr ((((@@@@ddddaaaattttaaaa)))) {{{{
pppprrrriiiinnnntttt KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE;;;;
}}}}
cccclllloooosssseeee((((KKKKIIIIDDDD____TTTTOOOO____WWWWRRRRIIIITTTTEEEE)))) |||||||| wwwwaaaarrrrnnnn """"kkkkiiiidddd eeeexxxxiiiitttteeeedddd $$$$????"""";;;;
}}}} eeeellllsssseeee {{{{ #### cccchhhhiiiilllldddd
(((($$$$EEEEUUUUIIIIDDDD,,,, $$$$EEEEGGGGIIIIDDDD)))) ==== (((($$$$UUUUIIIIDDDD,,,, $$$$GGGGIIIIDDDD))));;;;
eeeexxxxeeeecccc(((($$$$pppprrrrooooggggrrrraaaammmm,,,, @@@@ooooppppttttiiiioooonnnnssss,,,, @@@@aaaarrrrggggssss))))
|||||||| ddddiiiieeee """"ccccaaaannnn''''tttt eeeexxxxeeeecccc pppprrrrooooggggrrrraaaammmm:::: $$$$!!!!"""";;;;
#### NNNNOOOOTTTTRRRREEEEAAAACCCCHHHHEEEEDDDD
}}}}
Note that these operations are full Unix forks, which
means they may not be correctly implemented on alien
systems. Additionally, these are not true multithreading.
If you'd like to learn more about threading, see the
_m_o_d_u_l_e_s file mentioned below in the the section on _S_E_E
_A_L_S_O section.
BBBBiiiiddddiiiirrrreeeeccccttttiiiioooonnnnaaaallll CCCCoooommmmmmmmuuuunnnniiiiccccaaaattttiiiioooonnnn
While this works reasonably well for unidirectional
communication, what about bidirectional communication?
The obvious thing you'd like to do doesn't actually work:
ooooppppeeeennnn((((PPPPRRRROOOOGGGG____FFFFOOOORRRR____RRRREEEEAAAADDDDIIIINNNNGGGG____AAAANNNNDDDD____WWWWRRRRIIIITTTTIIIINNNNGGGG,,,, """"|||| ssssoooommmmeeee pppprrrrooooggggrrrraaaammmm ||||""""))))
and if you forget to use the ----wwww flag, then you'll miss out
entirely on the diagnostic message:
30/Jan/96 perl 5.002 with 7
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
CCCCaaaannnn''''tttt ddddoooo bbbbiiiiddddiiiirrrreeeeccccttttiiiioooonnnnaaaallll ppppiiiippppeeee aaaatttt ----eeee lllliiiinnnneeee 1111....
If you really want to, you can use the standard _o_p_e_n_2_(_)
library function to catch both ends. There's also an
_o_p_e_n_3_(_) for tridirectional I/O so you can also catch your
child's STDERR, but doing so would then require an awkward
_s_e_l_e_c_t_(_) loop and wouldn't allow you to use normal Perl
input operations.
If you look at its source, you'll see that _o_p_e_n_2_(_) uses
low-level primitives like Unix _p_i_p_e_(_) and _e_x_e_c_(_) to create
all the connections. While it might have been slightly
more efficient by using _s_o_c_k_e_t_p_a_i_r_(_), it would have then
been even less portable than it already is. The _o_p_e_n_2_(_)
and _o_p_e_n_3_(_) functions are unlikely to work anywhere
except on a Unix system or some other one purporting to be
POSIX compliant.
Here's an example of using _o_p_e_n_2_(_):
uuuusssseeee FFFFiiiilllleeeeHHHHaaaannnnddddlllleeee;;;;
uuuusssseeee IIIIPPPPCCCC::::::::OOOOppppeeeennnn2222;;;;
$$$$ppppiiiidddd ==== ooooppppeeeennnn2222(((( \\\\****RRRReeeeaaaaddddeeeerrrr,,,, \\\\****WWWWrrrriiiitttteeeerrrr,,,, """"ccccaaaatttt ----uuuu ----nnnn"""" ))));;;;
WWWWrrrriiiitttteeeerrrr---->>>>aaaauuuuttttoooofffflllluuuusssshhhh(((())));;;; #### ddddeeeeffffaaaauuuulllltttt hhhheeeerrrreeee,,,, aaaaccccttttuuuuaaaallllllllyyyy
pppprrrriiiinnnntttt WWWWrrrriiiitttteeeerrrr """"ssssttttuuuuffffffff\\\\nnnn"""";;;;
$$$$ggggooootttt ==== <<<<RRRReeeeaaaaddddeeeerrrr>>>>;;;;
The problem with this is that Unix buffering is going to
really ruin your day. Even though your WWWWrrrriiiitttteeeerrrr filehandle
is autoflushed, and the process on the other end will get
your data in a timely manner, you can't usually do
anything to force it to actually give it back to you in a
similarly quick fashion. In this case, we could, because
we gave _c_a_t a ----uuuu flag to make it unbuffered. But very few
Unix commands are designed to operate over pipes, so this
seldom works unless you yourself wrote the program on the
other end of the double-ended pipe.
A solution to this is the non-standard _C_o_m_m_._p_l library.
It uses pseudo-ttys to make your program behave more
reasonably:
rrrreeeeqqqquuuuiiiirrrreeee ''''CCCCoooommmmmmmm....ppppllll'''';;;;
$$$$pppphhhh ==== ooooppppeeeennnn____pppprrrroooocccc((((''''ccccaaaatttt ----nnnn''''))));;;;
ffffoooorrrr ((((1111........11110000)))) {{{{
pppprrrriiiinnnntttt $$$$pppphhhh """"aaaa lllliiiinnnneeee\\\\nnnn"""";;;;
pppprrrriiiinnnntttt """"ggggooootttt bbbbaaaacccckkkk """",,,, ssssccccaaaallllaaaarrrr <<<<$$$$pppphhhh>>>>;;;;
}}}}
This way you don't have to have control over the source
code of the program you're using. The _C_o_m_m library also
has _e_x_p_e_c_t_(_) and _i_n_t_e_r_a_c_t_(_) functions. Find the library
(and hopefully its successor _I_P_C_:_:_C_h_a_t) at your nearest
CPAN archive as detailed in the the section on _S_E_E _A_L_S_O
30/Jan/96 perl 5.002 with 8
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
section below.
SSSSoooocccckkkkeeeettttssss:::: CCCClllliiiieeeennnntttt////SSSSeeeerrrrvvvveeeerrrr CCCCoooommmmmmmmuuuunnnniiiiccccaaaattttiiiioooonnnn
While not limited to Unix-derived operating systems (e.g.
WinSock on PCs provides socket support, as do some VMS
libraries), you may not have sockets on your system, in
which this section probably isn't going to do you much
good. With sockets, you can do both virtual circuits
(i.e. TCP streams) and datagrams (i.e. UDP packets). You
may be able to do even more depending on your system.
The Perl function calls for dealing with sockets have the
same names as the corresponding system calls in C, but
their arguments tend to differ for two reasons: first,
Perl filehandles work differently than C file descriptors.
Second, Perl already knows the length of its strings, so
you don't need to pass that information.
One of the major problems with old socket code in Perl was
that it used hard-coded values for some of the constants,
which severely hurt portability. If you ever see code
that does anything like explicitly setting $$$$AAAAFFFF____IIIINNNNEEEETTTT ==== 2222,
you know you're in for big trouble: An immeasurably
superior approach is to use the SSSSoooocccckkkkeeeetttt module, which more
reliably grants access to various constants and functions
you'll need.
IIIInnnntttteeeerrrrnnnneeeetttt TTTTCCCCPPPP CCCClllliiiieeeennnnttttssss aaaannnndddd SSSSeeeerrrrvvvveeeerrrrssss
Use Internet-domain sockets when you want to do client-
server communication that might extend to machines outside
of your own system.
Here's a sample TCP client using Internet-domain sockets:
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----wwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
mmmmyyyy (((($$$$rrrreeeemmmmooootttteeee,,,,$$$$ppppoooorrrrtttt,,,, $$$$iiiiaaaaddddddddrrrr,,,, $$$$ppppaaaaddddddddrrrr,,,, $$$$pppprrrroooottttoooo,,,, $$$$lllliiiinnnneeee))));;;;
$$$$rrrreeeemmmmooootttteeee ==== sssshhhhiiiifffftttt |||||||| ''''llllooooccccaaaallllhhhhoooosssstttt'''';;;;
$$$$ppppoooorrrrtttt ==== sssshhhhiiiifffftttt |||||||| 2222333344445555;;;; #### rrrraaaannnnddddoooommmm ppppoooorrrrtttt
iiiiffff (((($$$$ppppoooorrrrtttt ====~~~~ ////\\\\DDDD////)))) {{{{ $$$$ppppoooorrrrtttt ==== ggggeeeettttsssseeeerrrrvvvvbbbbyyyynnnnaaaammmmeeee(((($$$$ppppoooorrrrtttt,,,, ''''ttttccccpppp'''')))) }}}}
ddddiiiieeee """"NNNNoooo ppppoooorrrrtttt"""" uuuunnnnlllleeeessssssss $$$$ppppoooorrrrtttt;;;;
$$$$iiiiaaaaddddddddrrrr ==== iiiinnnneeeetttt____aaaattttoooonnnn(((($$$$rrrreeeemmmmooootttteeee)))) |||||||| ddddiiiieeee """"nnnnoooo hhhhoooosssstttt:::: $$$$rrrreeeemmmmooootttteeee"""";;;;
$$$$ppppaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppoooorrrrtttt,,,, $$$$iiiiaaaaddddddddrrrr))));;;;
$$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''ttttccccpppp''''))));;;;
ssssoooocccckkkkeeeetttt((((SSSSOOOOCCCCKKKK,,,, PPPPFFFF____IIIINNNNEEEETTTT,,,, SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,, $$$$pppprrrroooottttoooo)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
ccccoooonnnnnnnneeeecccctttt((((SSSSOOOOCCCCKKKK,,,, $$$$ppppaaaaddddddddrrrr)))) |||||||| ddddiiiieeee """"ccccoooonnnnnnnneeeecccctttt:::: $$$$!!!!"""";;;;
wwwwhhhhiiiilllleeee (((($$$$lllliiiinnnneeee ==== <<<<SSSSOOOOCCCCKKKK>>>>)))) {{{{
pppprrrriiiinnnntttt $$$$lllliiiinnnneeee;;;;
}}}}
30/Jan/96 perl 5.002 with 9
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
cccclllloooosssseeee ((((SSSSOOOOCCCCKKKK)))) |||||||| ddddiiiieeee """"cccclllloooosssseeee:::: $$$$!!!!"""";;;;
eeeexxxxiiiitttt;;;;
And here's a corresponding server to go along with it.
We'll leave the address as INADDR_ANY so that the kernel
can choose the appropriate interface on multihomed hosts.
If you want sit on a particular interface (like the
external side of a gateway or firewall machine), you
should fill this in with your real address instead.
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----TTTTwwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
BBBBEEEEGGGGIIIINNNN {{{{ $$$$EEEENNNNVVVV{{{{PPPPAAAATTTTHHHH}}}} ==== ''''////uuuussssrrrr////uuuuccccbbbb::::////bbbbiiiinnnn'''' }}}}
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
uuuusssseeee CCCCaaaarrrrpppp;;;;
ssssuuuubbbb llllooooggggmmmmssssgggg {{{{ pppprrrriiiinnnntttt """"$$$$0000 $$$$$$$$:::: @@@@____ aaaatttt """",,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee,,,, """"\\\\nnnn"""" }}}}
mmmmyyyy $$$$ppppoooorrrrtttt ==== sssshhhhiiiifffftttt |||||||| 2222333344445555;;;;
mmmmyyyy $$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''ttttccccpppp''''))));;;;
ssssoooocccckkkkeeeetttt((((SSSSeeeerrrrvvvveeeerrrr,,,, PPPPFFFF____IIIINNNNEEEETTTT,,,, SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,, $$$$pppprrrroooottttoooo)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
sssseeeettttssssoooocccckkkkoooopppptttt((((SSSSeeeerrrrvvvveeeerrrr,,,, SSSSOOOOLLLL____SSSSOOOOCCCCKKKKEEEETTTT,,,, SSSSOOOO____RRRREEEEUUUUSSSSEEEEAAAADDDDDDDDRRRR,,,,
ppppaaaacccckkkk((((""""llll"""",,,, 1111)))))))) |||||||| ddddiiiieeee """"sssseeeettttssssoooocccckkkkoooopppptttt:::: $$$$!!!!"""";;;;
bbbbiiiinnnndddd((((SSSSeeeerrrrvvvveeeerrrr,,,, ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppoooorrrrtttt,,,, IIIINNNNAAAADDDDDDDDRRRR____AAAANNNNYYYY)))))))) |||||||| ddddiiiieeee """"bbbbiiiinnnndddd:::: $$$$!!!!"""";;;;
lllliiiisssstttteeeennnn((((SSSSeeeerrrrvvvveeeerrrr,,,,SSSSOOOOMMMMAAAAXXXXCCCCOOOONNNNNNNN)))) |||||||| ddddiiiieeee """"lllliiiisssstttteeeennnn:::: $$$$!!!!"""";;;;
llllooooggggmmmmssssgggg """"sssseeeerrrrvvvveeeerrrr ssssttttaaaarrrrtttteeeedddd oooonnnn ppppoooorrrrtttt $$$$ppppoooorrrrtttt"""";;;;
mmmmyyyy $$$$ppppaaaaddddddddrrrr;;;;
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;;
ffffoooorrrr (((( ;;;; $$$$ppppaaaaddddddddrrrr ==== aaaacccccccceeeepppptttt((((CCCClllliiiieeeennnntttt,,,,SSSSeeeerrrrvvvveeeerrrr))));;;; cccclllloooosssseeee CCCClllliiiieeeennnntttt)))) {{{{
mmmmyyyy(((($$$$ppppoooorrrrtttt,,,,$$$$iiiiaaaaddddddddrrrr)))) ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppaaaaddddddddrrrr))));;;;
mmmmyyyy $$$$nnnnaaaammmmeeee ==== ggggeeeetttthhhhoooossssttttbbbbyyyyaaaaddddddddrrrr(((($$$$iiiiaaaaddddddddrrrr,,,,AAAAFFFF____IIIINNNNEEEETTTT))));;;;
llllooooggggmmmmssssgggg """"ccccoooonnnnnnnneeeeccccttttiiiioooonnnn ffffrrrroooommmm $$$$nnnnaaaammmmeeee [[[["""",,,,
iiiinnnneeeetttt____nnnnttttooooaaaa(((($$$$iiiiaaaaddddddddrrrr)))),,,, """"]]]]
aaaatttt ppppoooorrrrtttt $$$$ppppoooorrrrtttt"""";;;;
pppprrrriiiinnnntttt CCCCLLLLIIIIEEEENNNNTTTT """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee,,,, $$$$nnnnaaaammmmeeee,,,, iiiitttt''''ssss nnnnoooowwww """",,,,
ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee,,,, """"\\\\nnnn"""";;;;
}}}}
And here's a multithreaded version. It's multithreaded in
that like most typical servers, it spawns (forks) a slave
server to handle the client request so that the master
server can quickly go back to service a new client.
30/Jan/96 perl 5.002 with 10
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----TTTTwwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
BBBBEEEEGGGGIIIINNNN {{{{ $$$$EEEENNNNVVVV{{{{PPPPAAAATTTTHHHH}}}} ==== ''''////uuuussssrrrr////uuuuccccbbbb::::////bbbbiiiinnnn'''' }}}}
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
uuuusssseeee CCCCaaaarrrrpppp;;;;
ssssuuuubbbb ssssppppaaaawwwwnnnn;;;; #### ffffoooorrrrwwwwaaaarrrrdddd ddddeeeeccccllllaaaarrrraaaattttiiiioooonnnn
ssssuuuubbbb llllooooggggmmmmssssgggg {{{{ pppprrrriiiinnnntttt """"$$$$0000 $$$$$$$$:::: @@@@____ aaaatttt """",,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee,,,, """"\\\\nnnn"""" }}}}
mmmmyyyy $$$$ppppoooorrrrtttt ==== sssshhhhiiiifffftttt |||||||| 2222333344445555;;;;
mmmmyyyy $$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''ttttccccpppp''''))));;;;
ssssoooocccckkkkeeeetttt((((SSSSeeeerrrrvvvveeeerrrr,,,, PPPPFFFF____IIIINNNNEEEETTTT,,,, SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,, $$$$pppprrrroooottttoooo)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
sssseeeettttssssoooocccckkkkoooopppptttt((((SSSSeeeerrrrvvvveeeerrrr,,,, SSSSOOOOLLLL____SSSSOOOOCCCCKKKKEEEETTTT,,,, SSSSOOOO____RRRREEEEUUUUSSSSEEEEAAAADDDDDDDDRRRR,,,,
ppppaaaacccckkkk((((""""llll"""",,,, 1111)))))))) |||||||| ddddiiiieeee """"sssseeeettttssssoooocccckkkkoooopppptttt:::: $$$$!!!!"""";;;;
bbbbiiiinnnndddd((((SSSSeeeerrrrvvvveeeerrrr,,,, ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppoooorrrrtttt,,,, IIIINNNNAAAADDDDDDDDRRRR____AAAANNNNYYYY)))))))) |||||||| ddddiiiieeee """"bbbbiiiinnnndddd:::: $$$$!!!!"""";;;;
lllliiiisssstttteeeennnn((((SSSSeeeerrrrvvvveeeerrrr,,,,SSSSOOOOMMMMAAAAXXXXCCCCOOOONNNNNNNN)))) |||||||| ddddiiiieeee """"lllliiiisssstttteeeennnn:::: $$$$!!!!"""";;;;
llllooooggggmmmmssssgggg """"sssseeeerrrrvvvveeeerrrr ssssttttaaaarrrrtttteeeedddd oooonnnn ppppoooorrrrtttt $$$$ppppoooorrrrtttt"""";;;;
mmmmyyyy $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== 0000;;;;
mmmmyyyy $$$$ppppaaaaddddddddrrrr;;;;
ssssuuuubbbb RRRREEEEAAAAPPPPEEEERRRR {{{{
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;; #### llllooooaaaatttthhhheeee ssssyyyyssssVVVV
$$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== wwwwaaaaiiiitttt;;;;
llllooooggggmmmmssssgggg """"rrrreeeeaaaappppeeeedddd $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd"""" .... (((($$$$???? ???? """" wwwwiiiitttthhhh eeeexxxxiiiitttt $$$$????"""" :::: ''''''''))));;;;
}}}}
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;;
ffffoooorrrr (((( $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== 0000;;;;
(((($$$$ppppaaaaddddddddrrrr ==== aaaacccccccceeeepppptttt((((CCCClllliiiieeeennnntttt,,,,SSSSeeeerrrrvvvveeeerrrr)))))))) |||||||| $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd;;;;
$$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== 0000,,,, cccclllloooosssseeee CCCClllliiiieeeennnntttt))))
{{{{
nnnneeeexxxxtttt iiiiffff $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd;;;;
mmmmyyyy(((($$$$ppppoooorrrrtttt,,,,$$$$iiiiaaaaddddddddrrrr)))) ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppaaaaddddddddrrrr))));;;;
mmmmyyyy $$$$nnnnaaaammmmeeee ==== ggggeeeetttthhhhoooossssttttbbbbyyyyaaaaddddddddrrrr(((($$$$iiiiaaaaddddddddrrrr,,,,AAAAFFFF____IIIINNNNEEEETTTT))));;;;
llllooooggggmmmmssssgggg """"ccccoooonnnnnnnneeeeccccttttiiiioooonnnn ffffrrrroooommmm $$$$nnnnaaaammmmeeee [[[["""",,,,
iiiinnnneeeetttt____nnnnttttooooaaaa(((($$$$iiiiaaaaddddddddrrrr)))),,,, """"]]]]
aaaatttt ppppoooorrrrtttt $$$$ppppoooorrrrtttt"""";;;;
ssssppppaaaawwwwnnnn ssssuuuubbbb {{{{
pppprrrriiiinnnntttt """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee,,,, $$$$nnnnaaaammmmeeee,,,, iiiitttt''''ssss nnnnoooowwww """",,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee,,,, """"\\\\nnnn"""";;;;
eeeexxxxeeeecccc ''''////uuuussssrrrr////ggggaaaammmmeeeessss////ffffoooorrrrttttuuuunnnneeee''''
oooorrrr ccccoooonnnnffffeeeessssssss """"ccccaaaannnn''''tttt eeeexxxxeeeecccc ffffoooorrrrttttuuuunnnneeee:::: $$$$!!!!"""";;;;
}}}};;;;
}}}}
ssssuuuubbbb ssssppppaaaawwwwnnnn {{{{
mmmmyyyy $$$$ccccooooddddeeeerrrreeeeffff ==== sssshhhhiiiifffftttt;;;;
30/Jan/96 perl 5.002 with 11
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
uuuunnnnlllleeeessssssss ((((@@@@____ ======== 0000 &&&&&&&& $$$$ccccooooddddeeeerrrreeeeffff &&&&&&&& rrrreeeeffff(((($$$$ccccooooddddeeeerrrreeeeffff)))) eeeeqqqq ''''CCCCOOOODDDDEEEE'''')))) {{{{
ccccoooonnnnffffeeeessssssss """"uuuussssaaaaggggeeee:::: ssssppppaaaawwwwnnnn CCCCOOOODDDDEEEERRRREEEEFFFF"""";;;;
}}}}
mmmmyyyy $$$$ppppiiiidddd;;;;
iiiiffff ((((!!!!ddddeeeeffffiiiinnnneeeedddd(((($$$$ppppiiiidddd ==== ffffoooorrrrkkkk)))))))) {{{{
llllooooggggmmmmssssgggg """"ccccaaaannnnnnnnooootttt ffffoooorrrrkkkk:::: $$$$!!!!"""";;;;
rrrreeeettttuuuurrrrnnnn;;;;
}}}} eeeellllssssiiiiffff (((($$$$ppppiiiidddd)))) {{{{
llllooooggggmmmmssssgggg """"bbbbeeeeggggaaaatttt $$$$ppppiiiidddd"""";;;;
rrrreeeettttuuuurrrrnnnn;;;; #### iiii''''mmmm tttthhhheeee ppppaaaarrrreeeennnntttt
}}}}
#### eeeellllsssseeee iiii''''mmmm tttthhhheeee cccchhhhiiiilllldddd -------- ggggoooo ssssppppaaaawwwwnnnn
ooooppppeeeennnn((((SSSSTTTTDDDDIIIINNNN,,,, """"<<<<&&&&CCCClllliiiieeeennnntttt"""")))) |||||||| ddddiiiieeee """"ccccaaaannnn''''tttt dddduuuupppp cccclllliiiieeeennnntttt ttttoooo ssssttttddddiiiinnnn"""";;;;
ooooppppeeeennnn((((SSSSTTTTDDDDOOOOUUUUTTTT,,,, """">>>>&&&&CCCClllliiiieeeennnntttt"""")))) |||||||| ddddiiiieeee """"ccccaaaannnn''''tttt dddduuuupppp cccclllliiiieeeennnntttt ttttoooo ssssttttddddoooouuuutttt"""";;;;
######## ooooppppeeeennnn((((SSSSTTTTDDDDEEEERRRRRRRR,,,, """">>>>&&&&SSSSTTTTDDDDOOOOUUUUTTTT"""")))) |||||||| ddddiiiieeee """"ccccaaaannnn''''tttt dddduuuupppp ssssttttddddoooouuuutttt ttttoooo ssssttttddddeeeerrrrrrrr"""";;;;
eeeexxxxiiiitttt &&&&$$$$ccccooooddddeeeerrrreeeeffff(((())));;;;
}}}}
This server takes the trouble to clone off a child version
via _f_o_r_k_(_) for each incoming request. That way it can
handle many requests at once, which you might not always
want. Even if you don't _f_o_r_k_(_), the _l_i_s_t_e_n_(_) will allow
that many pending connections. Forking servers have to be
particularly careful about cleaning up their dead children
(called "zombies" in Unix parlance), because otherwise
you'll quickly fill up your process table.
We suggest that you use the ----TTTT flag to use taint checking
(see the _p_e_r_l_s_e_c manpage) even if we aren't running setuid
or setgid. This is always a good idea for servers and
other programs run on behalf of someone else (like CGI
scripts), because it lessens the chances that people from
the outside will be able to compromise your system.
Let's look at another TCP client. This one connects to
the TCP "time" service on a number of different machines
and shows how far their clocks differ from the system on
which it's being run:
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----wwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
mmmmyyyy $$$$SSSSEEEECCCCSSSS____ooooffff____77770000____YYYYEEEEAAAARRRRSSSS ==== 2222222200008888999988888888888800000000;;;;
ssssuuuubbbb ccccttttiiiimmmmeeee {{{{ ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee((((sssshhhhiiiifffftttt)))) }}}}
mmmmyyyy $$$$iiiiaaaaddddddddrrrr ==== ggggeeeetttthhhhoooossssttttbbbbyyyynnnnaaaammmmeeee((((''''llllooooccccaaaallllhhhhoooosssstttt''''))));;;;
mmmmyyyy $$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''ttttccccpppp''''))));;;;
mmmmyyyy $$$$ppppoooorrrrtttt ==== ggggeeeettttsssseeeerrrrvvvvbbbbyyyynnnnaaaammmmeeee((((''''ttttiiiimmmmeeee'''',,,, ''''ttttccccpppp''''))));;;;
mmmmyyyy $$$$ppppaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn((((0000,,,, $$$$iiiiaaaaddddddddrrrr))));;;;
mmmmyyyy(((($$$$hhhhoooosssstttt))));;;;
30/Jan/96 perl 5.002 with 12
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
$$$$|||| ==== 1111;;;;
pppprrrriiiinnnnttttffff """"%%%%----22224444ssss %%%%8888ssss %%%%ssss\\\\nnnn"""",,,, """"llllooooccccaaaallllhhhhoooosssstttt"""",,,, 0000,,,, ccccttttiiiimmmmeeee((((ttttiiiimmmmeeee(((())))))));;;;
ffffoooorrrreeeeaaaacccchhhh $$$$hhhhoooosssstttt ((((@@@@AAAARRRRGGGGVVVV)))) {{{{
pppprrrriiiinnnnttttffff """"%%%%----22224444ssss """",,,, $$$$hhhhoooosssstttt;;;;
mmmmyyyy $$$$hhhhiiiissssiiiiaaaaddddddddrrrr ==== iiiinnnneeeetttt____aaaattttoooonnnn(((($$$$hhhhoooosssstttt)))) |||||||| ddddiiiieeee """"uuuunnnnkkkknnnnoooowwwwnnnn hhhhoooosssstttt"""";;;;
mmmmyyyy $$$$hhhhiiiissssppppaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppoooorrrrtttt,,,, $$$$hhhhiiiissssiiiiaaaaddddddddrrrr))));;;;
ssssoooocccckkkkeeeetttt((((SSSSOOOOCCCCKKKKEEEETTTT,,,, PPPPFFFF____IIIINNNNEEEETTTT,,,, SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,, $$$$pppprrrroooottttoooo)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
ccccoooonnnnnnnneeeecccctttt((((SSSSOOOOCCCCKKKKEEEETTTT,,,, $$$$hhhhiiiissssppppaaaaddddddddrrrr)))) |||||||| ddddiiiieeee """"bbbbiiiinnnndddd:::: $$$$!!!!"""";;;;
mmmmyyyy $$$$rrrrttttiiiimmmmeeee ==== '''' '''';;;;
rrrreeeeaaaadddd((((SSSSOOOOCCCCKKKKEEEETTTT,,,, $$$$rrrrttttiiiimmmmeeee,,,, 4444))));;;;
cccclllloooosssseeee((((SSSSOOOOCCCCKKKKEEEETTTT))));;;;
mmmmyyyy $$$$hhhhiiiissssttttiiiimmmmeeee ==== uuuunnnnppppaaaacccckkkk((((""""NNNN"""",,,, $$$$rrrrttttiiiimmmmeeee)))) ---- $$$$SSSSEEEECCCCSSSS____ooooffff____77770000____YYYYEEEEAAAARRRRSSSS ;;;;
pppprrrriiiinnnnttttffff """"%%%%8888dddd %%%%ssss\\\\nnnn"""",,,, $$$$hhhhiiiissssttttiiiimmmmeeee ---- ttttiiiimmmmeeee,,,, ccccttttiiiimmmmeeee(((($$$$hhhhiiiissssttttiiiimmmmeeee))));;;;
}}}}
UUUUnnnniiiixxxx----DDDDoooommmmaaaaiiiinnnn TTTTCCCCPPPP CCCClllliiiieeeennnnttttssss aaaannnndddd SSSSeeeerrrrvvvveeeerrrrssss
That's fine for Internet-domain clients and servers, but
what local communications? While you can use the same
setup, sometimes you don't want to. Unix-domain sockets
are local to the current host, and are often used
internally to implement pipes. Unlike Internet domain
sockets, UNIX domain sockets can show up in the file
system with an _l_s(1) listing.
$$$$ llllssss ----llll ////ddddeeeevvvv////lllloooogggg
ssssrrrrwwww----rrrrwwww----rrrrwwww---- 1111 rrrrooooooootttt 0000 OOOOcccctttt 33331111 00007777::::22223333 ////ddddeeeevvvv////lllloooogggg
You can test for these with Perl's ----SSSS file test:
uuuunnnnlllleeeessssssss (((( ----SSSS ''''////ddddeeeevvvv////lllloooogggg'''' )))) {{{{
ddddiiiieeee """"ssssoooommmmeeeetttthhhhiiiinnnngggg''''ssss wwwwiiiicccckkkkeeeedddd wwwwiiiitttthhhh tttthhhheeee pppprrrriiiinnnntttt ssssyyyysssstttteeeemmmm"""";;;;
}}}}
Here's a sample Unix-domain client:
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----wwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
mmmmyyyy (((($$$$rrrreeeennnnddddeeeezzzzvvvvoooouuuussss,,,, $$$$lllliiiinnnneeee))));;;;
$$$$rrrreeeennnnddddeeeezzzzvvvvoooouuuussss ==== sssshhhhiiiifffftttt |||||||| ''''////ttttmmmmpppp////ccccaaaattttssssoooocccckkkk'''';;;;
ssssoooocccckkkkeeeetttt((((SSSSOOOOCCCCKKKK,,,, PPPPFFFF____UUUUNNNNIIIIXXXX,,,, SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,, 0000)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
ccccoooonnnnnnnneeeecccctttt((((SSSSOOOOCCCCKKKK,,,, ssssoooocccckkkkaaaaddddddddrrrr____uuuunnnn(((($$$$rrrreeeemmmmooootttteeee)))))))) |||||||| ddddiiiieeee """"ccccoooonnnnnnnneeeecccctttt:::: $$$$!!!!"""";;;;
wwwwhhhhiiiilllleeee (((($$$$lllliiiinnnneeee ==== <<<<SSSSOOOOCCCCKKKK>>>>)))) {{{{
pppprrrriiiinnnntttt $$$$lllliiiinnnneeee;;;;
}}}}
eeeexxxxiiiitttt;;;;
And here's a corresponding server.
30/Jan/96 perl 5.002 with 13
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----TTTTwwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
uuuusssseeee CCCCaaaarrrrpppp;;;;
BBBBEEEEGGGGIIIINNNN {{{{ $$$$EEEENNNNVVVV{{{{PPPPAAAATTTTHHHH}}}} ==== ''''////uuuussssrrrr////uuuuccccbbbb::::////bbbbiiiinnnn'''' }}}}
mmmmyyyy $$$$NNNNAAAAMMMMEEEE ==== ''''////ttttmmmmpppp////ccccaaaattttssssoooocccckkkk'''';;;;
mmmmyyyy $$$$uuuuaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____uuuunnnn(((($$$$NNNNAAAAMMMMEEEE))));;;;
mmmmyyyy $$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''ttttccccpppp''''))));;;;
ssssoooocccckkkkeeeetttt((((SSSSeeeerrrrvvvveeeerrrr,,,,PPPPFFFF____UUUUNNNNIIIIXXXX,,,,SSSSOOOOCCCCKKKK____SSSSTTTTRRRREEEEAAAAMMMM,,,,0000)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
uuuunnnnlllliiiinnnnkkkk(((($$$$NNNNAAAAMMMMEEEE))));;;;
bbbbiiiinnnndddd ((((SSSSeeeerrrrvvvveeeerrrr,,,, $$$$uuuuaaaaddddddddrrrr)))) |||||||| ddddiiiieeee """"bbbbiiiinnnndddd:::: $$$$!!!!"""";;;;
lllliiiisssstttteeeennnn((((SSSSeeeerrrrvvvveeeerrrr,,,,SSSSOOOOMMMMAAAAXXXXCCCCOOOONNNNNNNN)))) |||||||| ddddiiiieeee """"lllliiiisssstttteeeennnn:::: $$$$!!!!"""";;;;
llllooooggggmmmmssssgggg """"sssseeeerrrrvvvveeeerrrr ssssttttaaaarrrrtttteeeedddd oooonnnn $$$$NNNNAAAAMMMMEEEE"""";;;;
$$$$SSSSIIIIGGGG{{{{CCCCHHHHLLLLDDDD}}}} ==== \\\\&&&&RRRREEEEAAAAPPPPEEEERRRR;;;;
ffffoooorrrr (((( $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== 0000;;;;
aaaacccccccceeeepppptttt((((CCCClllliiiieeeennnntttt,,,,SSSSeeeerrrrvvvveeeerrrr)))) |||||||| $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd;;;;
$$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd ==== 0000,,,, cccclllloooosssseeee CCCClllliiiieeeennnntttt))))
{{{{
nnnneeeexxxxtttt iiiiffff $$$$wwwwaaaaiiiitttteeeeddddppppiiiidddd;;;;
llllooooggggmmmmssssgggg """"ccccoooonnnnnnnneeeeccccttttiiiioooonnnn oooonnnn $$$$NNNNAAAAMMMMEEEE"""";;;;
ssssppppaaaawwwwnnnn ssssuuuubbbb {{{{
pppprrrriiiinnnntttt """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee,,,, iiiitttt''''ssss nnnnoooowwww """",,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee,,,, """"\\\\nnnn"""";;;;
eeeexxxxeeeecccc ''''////uuuussssrrrr////ggggaaaammmmeeeessss////ffffoooorrrrttttuuuunnnneeee'''' oooorrrr ddddiiiieeee """"ccccaaaannnn''''tttt eeeexxxxeeeecccc ffffoooorrrrttttuuuunnnneeee:::: $$$$!!!!"""";;;;
}}}};;;;
}}}}
As you see, it's remarkably similar to the Internet domain
TCP server, so much so, in fact, that we've omitted
several duplicate functions--_s_p_a_w_n_(_), _l_o_g_m_s_g_(_), _c_t_i_m_e_(_),
and _R_E_A_P_E_R_(_)--which are exactly the same as in the other
server.
So why would you ever want to use a Unix domain socket
instead of a simpler named pipe? Because a named pipe
doesn't give you sessions. You can't tell one process's
data from another's. With socket programming, you get a
separate session for each client: that's why _a_c_c_e_p_t_(_)
takes two arguments.
For example, let's say that you have a long running
database server daemon that you want folks from the World
Wide Web to be able to access, but only if they go through
a CGI interface. You'd have a small, simple CGI program
that does whatever checks and logging you feel like, and
then acts as a Unix-domain client and connects to your
private server.
30/Jan/96 perl 5.002 with 14
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
UUUUDDDDPPPP:::: MMMMeeeessssssssaaaaggggeeee PPPPaaaassssssssiiiinnnngggg
Another kind of client-server setup is one that uses not
connections, but messages. UDP communications involve
much lower overhead but also provide less reliability, as
there are no promises that messages will arrive at all,
let alone in order and unmangled. Still, UDP offers some
advantages over TCP, including being able to "broadcast"
or "multicast" to a whole bunch of destination hosts at
once (usually on your local subnet). If you find yourself
overly concerned about reliability and start building
checks into your message system, then you probably should
just use TCP to start with.
Here's a UDP program similar to the sample Internet TCP
client given above. However, instead of checking one host
at a time, the UDP version will check many of them
asynchronously by simulating a multicast and then using
_s_e_l_e_c_t_(_) to do a timed-out wait for I/O. To do something
similar with TCP, you'd have to use a different socket
handle for each host.
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----wwww
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
uuuusssseeee SSSSyyyyssss::::::::HHHHoooossssttttnnnnaaaammmmeeee;;;;
mmmmyyyy (((( $$$$ccccoooouuuunnnntttt,,,, $$$$hhhhiiiissssiiiiaaaaddddddddrrrr,,,, $$$$hhhhiiiissssppppaaaaddddddddrrrr,,,, $$$$hhhhiiiissssttttiiiimmmmeeee,,,,
$$$$hhhhoooosssstttt,,,, $$$$iiiiaaaaddddddddrrrr,,,, $$$$ppppaaaaddddddddrrrr,,,, $$$$ppppoooorrrrtttt,,,, $$$$pppprrrroooottttoooo,,,,
$$$$rrrriiiinnnn,,,, $$$$rrrroooouuuutttt,,,, $$$$rrrrttttiiiimmmmeeee,,,, $$$$SSSSEEEECCCCSSSS____ooooffff____77770000____YYYYEEEEAAAARRRRSSSS))));;;;
$$$$SSSSEEEECCCCSSSS____ooooffff____77770000____YYYYEEEEAAAARRRRSSSS ==== 2222222200008888999988888888888800000000;;;;
$$$$iiiiaaaaddddddddrrrr ==== ggggeeeetttthhhhoooossssttttbbbbyyyynnnnaaaammmmeeee((((hhhhoooossssttttnnnnaaaammmmeeee(((())))))));;;;
$$$$pppprrrroooottttoooo ==== ggggeeeettttpppprrrroooottttoooobbbbyyyynnnnaaaammmmeeee((((''''uuuuddddpppp''''))));;;;
$$$$ppppoooorrrrtttt ==== ggggeeeettttsssseeeerrrrvvvvbbbbyyyynnnnaaaammmmeeee((((''''ttttiiiimmmmeeee'''',,,, ''''uuuuddddpppp''''))));;;;
$$$$ppppaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn((((0000,,,, $$$$iiiiaaaaddddddddrrrr))));;;; #### 0000 mmmmeeeeaaaannnnssss lllleeeetttt kkkkeeeerrrrnnnneeeellll ppppiiiicccckkkk
ssssoooocccckkkkeeeetttt((((SSSSOOOOCCCCKKKKEEEETTTT,,,, PPPPFFFF____IIIINNNNEEEETTTT,,,, SSSSOOOOCCCCKKKK____DDDDGGGGRRRRAAAAMMMM,,,, $$$$pppprrrroooottttoooo)))) |||||||| ddddiiiieeee """"ssssoooocccckkkkeeeetttt:::: $$$$!!!!"""";;;;
bbbbiiiinnnndddd((((SSSSOOOOCCCCKKKKEEEETTTT,,,, $$$$ppppaaaaddddddddrrrr)))) |||||||| ddddiiiieeee """"bbbbiiiinnnndddd:::: $$$$!!!!"""";;;;
$$$$|||| ==== 1111;;;;
pppprrrriiiinnnnttttffff """"%%%%----11112222ssss %%%%8888ssss %%%%ssss\\\\nnnn"""",,,, """"llllooooccccaaaallllhhhhoooosssstttt"""",,,, 0000,,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee ttttiiiimmmmeeee;;;;
$$$$ccccoooouuuunnnntttt ==== 0000;;;;
ffffoooorrrr $$$$hhhhoooosssstttt ((((@@@@AAAARRRRGGGGVVVV)))) {{{{
$$$$ccccoooouuuunnnntttt++++++++;;;;
$$$$hhhhiiiissssiiiiaaaaddddddddrrrr ==== iiiinnnneeeetttt____aaaattttoooonnnn(((($$$$hhhhoooosssstttt)))) |||||||| ddddiiiieeee """"uuuunnnnkkkknnnnoooowwwwnnnn hhhhoooosssstttt"""";;;;
$$$$hhhhiiiissssppppaaaaddddddddrrrr ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$ppppoooorrrrtttt,,,, $$$$hhhhiiiissssiiiiaaaaddddddddrrrr))));;;;
ddddeeeeffffiiiinnnneeeedddd((((sssseeeennnndddd((((SSSSOOOOCCCCKKKKEEEETTTT,,,, 0000,,,, 0000,,,, $$$$hhhhiiiissssppppaaaaddddddddrrrr)))))))) |||||||| ddddiiiieeee """"sssseeeennnndddd $$$$hhhhoooosssstttt:::: $$$$!!!!"""";;;;
}}}}
$$$$rrrriiiinnnn ==== '''''''';;;;
vvvveeeecccc(((($$$$rrrriiiinnnn,,,, ffffiiiilllleeeennnnoooo((((SSSSOOOOCCCCKKKKEEEETTTT)))),,,, 1111)))) ==== 1111;;;;
30/Jan/96 perl 5.002 with 15
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
#### ttttiiiimmmmeeeeoooouuuutttt aaaafffftttteeeerrrr 11110000....0000 sssseeeeccccoooonnnnddddssss
wwwwhhhhiiiilllleeee (((($$$$ccccoooouuuunnnntttt &&&&&&&& sssseeeelllleeeecccctttt(((($$$$rrrroooouuuutttt ==== $$$$rrrriiiinnnn,,,, uuuunnnnddddeeeeffff,,,, uuuunnnnddddeeeeffff,,,, 11110000....0000)))))))) {{{{
$$$$rrrrttttiiiimmmmeeee ==== '''''''';;;;
(((($$$$hhhhiiiissssppppaaaaddddddddrrrr ==== rrrreeeeccccvvvv((((SSSSOOOOCCCCKKKKEEEETTTT,,,, $$$$rrrrttttiiiimmmmeeee,,,, 4444,,,, 0000)))))))) |||||||| ddddiiiieeee """"rrrreeeeccccvvvv:::: $$$$!!!!"""";;;;
(((($$$$ppppoooorrrrtttt,,,, $$$$hhhhiiiissssiiiiaaaaddddddddrrrr)))) ==== ssssoooocccckkkkaaaaddddddddrrrr____iiiinnnn(((($$$$hhhhiiiissssppppaaaaddddddddrrrr))));;;;
$$$$hhhhoooosssstttt ==== ggggeeeetttthhhhoooossssttttbbbbyyyyaaaaddddddddrrrr(((($$$$hhhhiiiissssiiiiaaaaddddddddrrrr,,,, AAAAFFFF____IIIINNNNEEEETTTT))));;;;
$$$$hhhhiiiissssttttiiiimmmmeeee ==== uuuunnnnppppaaaacccckkkk((((""""NNNN"""",,,, $$$$rrrrttttiiiimmmmeeee)))) ---- $$$$SSSSEEEECCCCSSSS____ooooffff____77770000____YYYYEEEEAAAARRRRSSSS ;;;;
pppprrrriiiinnnnttttffff """"%%%%----11112222ssss """",,,, $$$$hhhhoooosssstttt;;;;
pppprrrriiiinnnnttttffff """"%%%%8888dddd %%%%ssss\\\\nnnn"""",,,, $$$$hhhhiiiissssttttiiiimmmmeeee ---- ttttiiiimmmmeeee,,,, ssssccccaaaallllaaaarrrr llllooooccccaaaallllttttiiiimmmmeeee(((($$$$hhhhiiiissssttttiiiimmmmeeee))));;;;
$$$$ccccoooouuuunnnntttt--------;;;;
}}}}
SSSSyyyyssssVVVV IIIIPPPPCCCC
While System V IPC isn't so widely used as sockets, it
still has some interesting uses. You can't, however,
effectively use SysV IPC or Berkeley _m_m_a_p_(_) to have shared
memory so as to share a variable amongst several
processes. That's because Perl would reallocate your
string when you weren't wanting it to.
Here's a small example showing shared memory usage.
$$$$IIIIPPPPCCCC____PPPPRRRRIIIIVVVVAAAATTTTEEEE ==== 0000;;;;
$$$$IIIIPPPPCCCC____RRRRMMMMIIIIDDDD ==== 0000;;;;
$$$$ssssiiiizzzzeeee ==== 2222000000000000;;;;
$$$$kkkkeeeeyyyy ==== sssshhhhmmmmggggeeeetttt(((($$$$IIIIPPPPCCCC____PPPPRRRRIIIIVVVVAAAATTTTEEEE,,,, $$$$ssssiiiizzzzeeee ,,,, 0000777777777777 ))));;;;
ddddiiiieeee uuuunnnnlllleeeessssssss ddddeeeeffffiiiinnnneeeedddd $$$$kkkkeeeeyyyy;;;;
$$$$mmmmeeeessssssssaaaaggggeeee ==== """"MMMMeeeessssssssaaaaggggeeee ####1111"""";;;;
sssshhhhmmmmwwwwrrrriiiitttteeee(((($$$$kkkkeeeeyyyy,,,, $$$$mmmmeeeessssssssaaaaggggeeee,,,, 0000,,,, 66660000 )))) |||||||| ddddiiiieeee """"$$$$!!!!"""";;;;
sssshhhhmmmmrrrreeeeaaaadddd(((($$$$kkkkeeeeyyyy,,,,$$$$bbbbuuuuffffffff,,,,0000,,,,66660000)))) |||||||| ddddiiiieeee """"$$$$!!!!"""";;;;
pppprrrriiiinnnntttt $$$$bbbbuuuuffffffff,,,,""""\\\\nnnn"""";;;;
pppprrrriiiinnnntttt """"ddddeeeelllleeeettttiiiinnnngggg $$$$kkkkeeeeyyyy\\\\nnnn"""";;;;
sssshhhhmmmmccccttttllll(((($$$$kkkkeeeeyyyy ,,,,$$$$IIIIPPPPCCCC____RRRRMMMMIIIIDDDD,,,, 0000)))) |||||||| ddddiiiieeee """"$$$$!!!!"""";;;;
Here's an example of a semaphore:
$$$$IIIIPPPPCCCC____KKKKEEEEYYYY ==== 1111222233334444;;;;
$$$$IIIIPPPPCCCC____RRRRMMMMIIIIDDDD ==== 0000;;;;
$$$$IIIIPPPPCCCC____CCCCRRRREEEEAAAATTTTEEEE ==== 0000000000001111000000000000;;;;
$$$$kkkkeeeeyyyy ==== sssseeeemmmmggggeeeetttt(((($$$$IIIIPPPPCCCC____KKKKEEEEYYYY,,,, $$$$nnnnsssseeeemmmmssss ,,,, 0000666666666666 |||| $$$$IIIIPPPPCCCC____CCCCRRRREEEEAAAATTTTEEEE ))));;;;
ddddiiiieeee iiiiffff !!!!ddddeeeeffffiiiinnnneeeedddd(((($$$$kkkkeeeeyyyy))));;;;
pppprrrriiiinnnntttt """"$$$$kkkkeeeeyyyy\\\\nnnn"""";;;;
Put this code in a separate file to be run in more that
one process Call the file _t_a_k_e:
#### ccccrrrreeeeaaaatttteeee aaaa sssseeeemmmmaaaapppphhhhoooorrrreeee
30/Jan/96 perl 5.002 with 16
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
$$$$IIIIPPPPCCCC____KKKKEEEEYYYY ==== 1111222233334444;;;;
$$$$kkkkeeeeyyyy ==== sssseeeemmmmggggeeeetttt(((($$$$IIIIPPPPCCCC____KKKKEEEEYYYY,,,, 0000 ,,,, 0000 ))));;;;
ddddiiiieeee iiiiffff !!!!ddddeeeeffffiiiinnnneeeedddd(((($$$$kkkkeeeeyyyy))));;;;
$$$$sssseeeemmmmnnnnuuuummmm ==== 0000;;;;
$$$$sssseeeemmmmffffllllaaaagggg ==== 0000;;;;
#### ''''ttttaaaakkkkeeee'''' sssseeeemmmmaaaapppphhhhoooorrrreeee
#### wwwwaaaaiiiitttt ffffoooorrrr sssseeeemmmmaaaapppphhhhoooorrrreeee ttttoooo bbbbeeee zzzzeeeerrrroooo
$$$$sssseeeemmmmoooopppp ==== 0000;;;;
$$$$ooooppppssssttttrrrriiiinnnngggg1111 ==== ppppaaaacccckkkk((((""""ssssssssssss"""",,,, $$$$sssseeeemmmmnnnnuuuummmm,,,, $$$$sssseeeemmmmoooopppp,,,, $$$$sssseeeemmmmffffllllaaaagggg))));;;;
#### IIIInnnnccccrrrreeeemmmmeeeennnntttt tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee ccccoooouuuunnnntttt
$$$$sssseeeemmmmoooopppp ==== 1111;;;;
$$$$ooooppppssssttttrrrriiiinnnngggg2222 ==== ppppaaaacccckkkk((((""""ssssssssssss"""",,,, $$$$sssseeeemmmmnnnnuuuummmm,,,, $$$$sssseeeemmmmoooopppp,,,, $$$$sssseeeemmmmffffllllaaaagggg))));;;;
$$$$ooooppppssssttttrrrriiiinnnngggg ==== $$$$ooooppppssssttttrrrriiiinnnngggg1111 .... $$$$ooooppppssssttttrrrriiiinnnngggg2222;;;;
sssseeeemmmmoooopppp(((($$$$kkkkeeeeyyyy,,,,$$$$ooooppppssssttttrrrriiiinnnngggg)))) |||||||| ddddiiiieeee """"$$$$!!!!"""";;;;
Put this code in a separate file to be run in more that
one process Call this file _g_i_v_e:
#### ''''ggggiiiivvvveeee'''' tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee
#### rrrruuuunnnn tttthhhhiiiissss iiiinnnn tttthhhheeee oooorrrriiiiggggiiiinnnnaaaallll pppprrrroooocccceeeessssssss aaaannnndddd yyyyoooouuuu wwwwiiiillllllll sssseeeeeeee
#### tttthhhhaaaatttt tttthhhheeee sssseeeeccccoooonnnndddd pppprrrroooocccceeeessssssss ccccoooonnnnttttiiiinnnnuuuueeeessss
$$$$IIIIPPPPCCCC____KKKKEEEEYYYY ==== 1111222233334444;;;;
$$$$kkkkeeeeyyyy ==== sssseeeemmmmggggeeeetttt(((($$$$IIIIPPPPCCCC____KKKKEEEEYYYY,,,, 0000,,,, 0000))));;;;
ddddiiiieeee iiiiffff !!!!ddddeeeeffffiiiinnnneeeedddd(((($$$$kkkkeeeeyyyy))));;;;
$$$$sssseeeemmmmnnnnuuuummmm ==== 0000;;;;
$$$$sssseeeemmmmffffllllaaaagggg ==== 0000;;;;
#### DDDDeeeeccccrrrreeeemmmmeeeennnntttt tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee ccccoooouuuunnnntttt
$$$$sssseeeemmmmoooopppp ==== ----1111;;;;
$$$$ooooppppssssttttrrrriiiinnnngggg ==== ppppaaaacccckkkk((((""""ssssssssssss"""",,,, $$$$sssseeeemmmmnnnnuuuummmm,,,, $$$$sssseeeemmmmoooopppp,,,, $$$$sssseeeemmmmffffllllaaaagggg))));;;;
sssseeeemmmmoooopppp(((($$$$kkkkeeeeyyyy,,,,$$$$ooooppppssssttttrrrriiiinnnngggg)))) |||||||| ddddiiiieeee """"$$$$!!!!"""";;;;
WWWWAAAARRRRNNNNIIIINNNNGGGG
The SysV IPC code above was written long ago, and it's
definitely clunky looking. It should at the very least be
made to uuuusssseeee ssssttttrrrriiiicccctttt and rrrreeeeqqqquuuuiiiirrrreeee """"ssssyyyyssss////iiiippppcccc....pppphhhh"""". Better yet,
perhaps someone should create an IIIIPPPPCCCC::::::::SSSSyyyyssssVVVV module the way
we have the SSSSoooocccckkkkeeeetttt module for normal client-server
communications.
(... time passes)
Voila! Check out the IPC::SysV modules written by Jack
Shirazi. You can find them at a CPAN store near you.
30/Jan/96 perl 5.002 with 17
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
NNNNOOOOTTTTEEEESSSS
If you are running under version 5.000 (dubious) or 5.001,
you can still use most of the examples in this document.
You may have to remove the uuuusssseeee ssssttttrrrriiiicccctttt and some of the _m_y_(_)
statements for 5.000, and for both you'll have to load in
version 1.2 of the _S_o_c_k_e_t_._p_m module, which was/is/shall-be
included in _p_e_r_l_5_._0_0_1_o.
Most of these routines quietly but politely return uuuunnnnddddeeeeffff
when they fail instead of causing your program to die
right then and there due to an uncaught exception.
(Actually, some of the new _S_o_c_k_e_t conversion functions
_c_r_o_a_k_(_) on bad arguments.) It is therefore essential that
you should check the return values fo these functions.
Always begin your socket programs this way for optimal
success, and don't forget to add ----TTTT taint checking flag to
the pound-bang line for servers:
####!!!!////uuuussssrrrr////bbbbiiiinnnn////ppppeeeerrrrllll ----wwww
rrrreeeeqqqquuuuiiiirrrreeee 5555....000000002222;;;;
uuuusssseeee ssssttttrrrriiiicccctttt;;;;
uuuusssseeee ssssiiiiggggttttrrrraaaapppp;;;;
uuuusssseeee SSSSoooocccckkkkeeeetttt;;;;
BBBBUUUUGGGGSSSS
All these routines create system-specific portability
problems. As noted elsewhere, Perl is at the mercy of
your C libraries for much of its system behaviour. It's
probably safest to assume broken SysV semantics for
signals and to stick with simple TCP and UDP socket
operations; e.g. don't try to pass open filedescriptors
over a local UDP datagram socket if you want your code to
stand a chance of being portable.
Because few vendors provide C libraries that are safely
re-entrant, the prudent programmer will do little else
within a handler beyond _d_i_e_(_) to raise an exception and
_l_o_n_g_j_m_p(3) out.
AAAAUUUUTTTTHHHHOOOORRRR
Tom Christiansen, with occasional vestiges of Larry Wall's
original version.
SSSSEEEEEEEE AAAALLLLSSSSOOOO
Besides the obvious functions in the _p_e_r_l_f_u_n_c manpage, you
should also check out the _m_o_d_u_l_e_s file at your nearest
CPAN site. (See the _p_e_r_l_m_o_d manpage or best yet, the _P_e_r_l
_F_A_Q for a description of what CPAN is and where to get
it.) Section 5 of the _m_o_d_u_l_e_s file is devoted to
"Networking, Device Control (modems) and Interprocess
Communication", and contains numerous unbundled modules
numerous networking modules, Chat and Expect operations,
CGI programming, DCE, FTP, IPC, NNTP, Proxy, Ptty, RPC,
30/Jan/96 perl 5.002 with 18
PERLIPC(1) User Contributed Perl Documentation PERLIPC(1)
SNMP, SMTP, Telnet, Threads, and ToolTalk--just to name a
few.
30/Jan/96 perl 5.002 with 19