作者tonini (LPI Course Ongoing)
看板Perl
标题[问题] 请问一下Server-Client的问题
时间Mon Apr 13 19:42:19 2009
其实我是完完全全的新手
底下只要看的到的程式都是改编自版主的XD (版主歹是 喔 囧)
话说我想要做的是这样
Server提供一个可写入的阵列(整数~size=10)
Client可以读也可以写
但是一次只能有一个写入~读取可以有多个
其实我也不知道我在写啥XD
但是东拼西凑出来的东西如下
但是似乎就是没有办法去确认Server是不是正确的读取了Client的东西
还是只是client print自己的array :(
#!/usr/bin/perl
#Server
use threads;
use IO::Socket;
my $server = IO::Socket::INET->new(
LocalPort => 8088,
Type => SOCK_STREAM,
Listen => 32,
Reuse =>1
);
my @array = ();
while(my $client = $server->accept() ) {
threads->create( "client_handler", $client);
}
sub client_handler {
my $client = shift;
while(<$client>) {
push( @array, $_ );
}
print $client foreach( @array );
$client->shutdown(1);
$client->close();
}
#!/usr/bin/perl
#Client
use IO::Socket;
my $client = IO::Socket::INET->new(
PeerAddr => "127.0.0.1",
PeerPort => 8088);
my @array = ();
my $element = 0;
while( $element != 10 ){
for ( 1 .. 10 ) {
push( @array, $element ); }
$element++;
}
print "$_\n" foreach( @array );
print $client foreach( @array );
while(<$client>) { print;}
$client->close();
因为完全不会写程式
所以这段话有点不懂
Each read/write thread should perform a busy loop
incrementing a local variable from 0 to 2,000,000
before actually doing the reading or the writing of the
shared array. Make sure that you put this loop inside
the critical section of the thread. This simulates longer
service and will therefore introduce more contention for
the resource.
busy loop 是啥米??
好像是很重要的一段话
但是怎麽也看不懂:(
要储存离开又不自觉的按下esc 然後 : 然後 wq! 了
那ㄟ安ㄋㄟ?!
--
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.72.195.107
1F:推 LiloHuang:busy loop就是那个回圈会一直跑 每次跑得过程不间隔 04/13 19:46
2F:推 LiloHuang:这样会造成那个thread耗用cpu 100% 你可能要做sleep动作 04/13 19:46
3F:推 LiloHuang:不过原本read/write其实是block-I/O 所以你不用管他 04/13 19:47
4F:推 LiloHuang:理论上你资料有送过去 不过目前我还没时间帮你跑一下 04/13 19:48
5F:推 LiloHuang:等其他板友看看呗^^ 要不然就等明天我比较有空哩 04/13 19:48
6F:推 LiloHuang:另外他是在讲threads要做临界区间的保护避免资源竞争 04/13 19:49
7F:推 LiloHuang:你应该把 my @array = (); 改 my @array : shared = (); 04/13 19:51
8F:推 LiloHuang:这样基本上这个阵列就可以同时多个client thread存取了 04/13 19:51
9F:推 LiloHuang:记得 use threads::shared; 这样才可以使用喔 :) 04/13 19:52
10F:→ tonini:谢版主隆恩~~~~ ^_^ 04/13 19:59