- 论坛徽章:
- 0
|
以前win下也就简单用用 fork,感觉还好,没遇到什么问题。
今天用 POE 实现个 tcp server, 因为考虑到对 server 端而言,每个 session 本身也会有比较复杂的状态机,有些操作,甚至会有非常耗时的block操作,
因此考虑父进程就用POE::Wheel::ListenAccept() 处理每个socket连接,而对收到的每个 client 连接,就 fork 一个进程,在 子进程 中 create 一个POE session
代码稍微改了下,感觉没啥错误了,win下一执行,结果发现每个 client 都不能正常工作, cpu一会儿就到 100%,一开始还以为是 有什么逻辑错误,实在查不出来后,直接脚本 copy 到 linux 中,运行,毫无问题,残念……浪费了我一上午时间!
fork 的代码就这段:
- sub accept_client {
- my ($kernel, $heap, $remote_sock, $remote_addr, $wheel_id) = @_[KERNEL, HEAP, ARG0..ARG2];
- my ($peerport, $packed_ip) = sockaddr_in($remote_addr);
- my $client_ip = inet_ntoa($packed_ip);
- print "SERVER: recieved a connection from $client_ip:$peerport\n";
- my $pid;
- if( $pid = fork() ) {
- print "SERVER: Create a sub process $pid for $client_ip:$peerport, and return!\n";
- return 0;
- }elsif(defined $pid) {
- delete $_[HEAP]->{listener}; # delete wheel which copy from main process
- $|++;
- POE::Session->create(
- inline_states => {
- _start => \&start_child,
- socket_input => \&socket_input,
- socket_failed => \&socket_failed,
- check_shutdown => \&check_shutdown,
- interrupt => \&process_interrupt,
- },
- args => [ $remote_sock, $client_ip, $peerport ],
- );
- }else {
- print "return undef, can't fork, $!\n";
- return;
- }
- }
复制代码
请教下大家,win 下 fork 会有哪些问题? 或者还是上面代码确实有问题? |
|