¾È³çÇϼ¼¿ä. ÇØÇÇCGI À¥¸¶½ºÅÍ À±¿µ¿õÀÔ´Ï´Ù. ÆÞ¸¶´Ï¾Æ¿¡¼ ¹ßÃéµÈ ±ÛÀÔ´Ï´Ù.
fork¸¦ »ç¿ëÇÏ´Â ÀÌÀ¯´Â »õ·Î¿î ÄÁ³Ø¼ÆÀº ¸Î±â À§ÇÑ°ÅÁÒ. °£´ÜÇÑ »ç¿ë¹ýÀº ´ÙÀ½°ú °°½À´Ï´Ù.
if(!defined($ChildPid=fork()) { die "cannot fork\n"; } else { if($ChildPid) { # Parent Process
} else { # Child Process
} }
fork·Î »õ·Î¿î ÇÁ·Î¼¼½º°¡ »ý¼ºµÇ¸é ¿ø ÇÁ·Î¼¼½º(ºÎ¸ð ÇÁ·Î¼¼½º)ÀÇ fork´Â child processÀÇ pid¸¦ ¹ÝȯÇÏ°í, »ý¼ºµÈ(ÀÚ½Ä ÇÁ·Î¼¼½º)ÀÇ fork´Â 0À» ¹ÝȯÇÕ´Ï´Ù. ¹ÝȯµÈ PID¸¦ °¡Áö°í¼ ÀÚ½ÅÀÌ ºÎ¸ð ÇÁ·Î¼¼½ºÀÎÁö, ÀÚ½Ä ÇÁ·Î¼¼½º ÀÎÁö ±¸º°ÇÏÁÒ.
¼ÒÄÏÀÇ °æ¿ì ºÎº¸ ÇÁ·Î¼¼½º´Â ´ÙÀ½ ÄÁ³Ø¼ÇÀ» ±â´Ù¸®°í, ÀÚ½Ä ÇÁ·Î¼¼½º´Â ÄÁ³Ø¼ÇÀ» ¸Î¾î¼ µ¥ÀÌŸ¸¦ ÁÖ°í ¹Þ°í.. ÇÒÀÏ ÇÏÁÒ.
Á»ºñ ÇÁ·Î¼¼½º´Â ÇÁ·Î¼¼½º°¡ Á¾·á됬´Âµ¥, ±× ÇÁ·Î¼¼½º¸¦ »ý¼º½ÃŲ ÇÁ·Î¼¼½º°¡ ±× µÞÀÏÀ» ó¸®ÇÏÁö ¾ÊÀ¸¸é »ý±é´Ï´Ù. ÀÚ½Ä ÇÁ·Î¼¼½º°¡ ÀÚ±â ÇÒ ÀÏ ´Ù ³¡³»°í Á¾·áÇߴµ¥, ºÎ¸ð ÇÁ·Î¼¼½º°¡ ³ª¸ô¶óÇÏ¸é ±×³ðÀÌ Á»ºñ°¡ µÇ¼ ¸®¼Ò½º ¹Ýȯ ¾ÈÇÏ°í ¹®Á¦°¡ µÇ±âµµ ÇÕ´Ï´Ù.
´ÙÀ½Àº °£´ÜÇÑ ¼¹ö ¼Ò½ºÀÔ´Ï´Ù.
ÈÀÏ À̸§ : server.pl
#!/usr/bin/perl
$|=1;
use IO::Socket;
$Local = IO::Socket::INET->new(Proto=>"tcp", LocalPort=>"1234", Listen=>1) or die "Can't open socket\n"; while($Remote = $Local->accept) { $Remote->autoflush(1); print "Connected\n"; if(!defined($ChildPid=fork())) { die "cannot fork\n"; } elsif($ChildPid) { # Parent Process while(`ps axw|grep server|grep Z|grep -v grep|wc`/1 > 0) { wait; } } else { # Child Process $Input=<$Remote>; print "INCOMING : $Input\n"; print $Remote "HI! I am Server. my pid is $ChildPid\n";
print "Connection Closed\n"; close ($Remote); exit; } }
close $Local;
exit;
»ç½Ç Àúµµ Á»ºñ ¶§¹®¿¡ °í¹ÎÇؼ ´ÙÀ½°ú °°ÀÌ Ã¼Å© Çؼ ó¸®Çß½À´Ï´Ù. Á¦´ë·Î ó¸®ÇÑ °ÍÀº ¾Æ´Ñµ¥.. ÇÏ¿©°£¿¡ Á»ºñ¸¦ ¾ø¾Ö±â´Â ÇØ¿ä.
while(`ps axw|grep server.pl |grep Z|grep -v grep|wc`/1 > 0) { wait; }
ÇÁ·Î¼¼½º Áß¿¡ server.pl(ÇÁ·Î±×·¥À̸§ÀÌÁÒ)ÀÌ µé¾î°£°Í ´Ù ã°í, ±× Áß¿¡ Á»ºñ(Z·Î Ç¥½ÃµÇÁÒ)¸¦ ã¾Æ¼ ±× °¹¼ö°¡ 1°³ ÀÌ»óÀ̸é, wait·Î Á»ºñ¸¦ ó¸®ÇÏ°í. ±×·±µ¥ ÀÌ·¸°Ô Çϸé ÀÚ½Ä ÇÁ·Î¼¼½º°¡ Á¾·áµÇÀÚ ¸¶ÀÚ Ã³¸®ÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó, ´ÙÀ½ ÄÁ³Ø¼Ç¶§ ó¸®ÇÏ´Â °Å¶ó¼ Á¦´ë·Î µÈ ¹æ¹ýÀÌ ¾Æ´ÏÁÒ. ¿µ ÂóÂóÇϱä Çѵ¥.
°ü·ÃµÈ ¹®¼°¡ ÀÖ±ä Çѵ¥, Á¤¸®°¡ ¾ÈµÈ°Å¶ó ÀÚ·á½Ç¿¡µµ ¿Ã¸®Áö ¸øÇÏ°í... ¸ÞÀÏ ¾îµå·¹½º ÁÖ½Ã¸é ¸ÞÀÏ·Î º¸³»µå¸±²²¿ä.
ÁñÆÞ!
|