I tweaked some things, and completely replaced the client.

With the sleep in the client, you can see it runs $max at a time.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1211329 use strict; use warnings; if( @ARGV ) # if any argument, it's the server, otherwise the client # ( for testing purposes :) { ### tcp server use DBI; use IO::Socket::INET; ### flush after every write $|= 1; # creating object interface of IO::Socket::INET modules which inte +rnally does # socket creation, binding and listening at the specified port add +ress. my $socket = new IO::Socket::INET ( LocalHost=> '127.0.0.1', LocalPort=> '5000', Proto=> 'tcp', Listen=> 5, Reuse=> 1 ) or die "ERROR in Socket creation: $!\n"; print "I am waiting clients to connect on port 5000.\n"; ### infinite loop while(1) { my($client_socket); my($peeraddress, $peerport); my($row, $data); my @processes= ('one', 'two', 'three', 'four', 'five', 'six', 'sev +en', 'eight', 'nine', 'ten'); while($row= shift(@processes)) { $client_socket = $socket->accept(); $peeraddress = $client_socket->peerhost(); $peerport = $client_socket->peerport(); print "Sending $row to $peeraddress:$peerport)... "; # write to the newly accepted client. print $client_socket "$row\n"; # read from the newly accepted client. $data = <$client_socket> // 'nothing'; chomp($data); print "got $data from client.\n"; $client_socket->close(); } #$socket->close(); } } else { ### tcp client my $max = 2; my $active = 0; while( 1 ) { while( $active < $max ) { if( my $pid = fork ) # parent { $active++; } elsif( defined $pid ) # child { my($socket, $data); $socket= new IO::Socket::INET ( PeerHost=> '127.0.0.1', PeerPort=> '5000', Proto=> 'tcp' ) or die "ERROR in Socket creation: $!\n"; $socket->autoflush(1); $data= <$socket>; chomp($data); $socket->close(); print "($$) Got $data from producer.\n"; sleep 1; exit; } else { die "fork failed $!"; } } if( $active == $max ) { wait; $active--; } } }

Note that the server was trying to read back from the client who never wrote anything ???


In reply to Re: fork always 'n' processes. by tybalt89
in thread fork always 'n' processes. by TheMagician

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.