<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; ">Olá Kleber,<DIV><BR><DIV><DIV>On 2007/04/06, at 14:38, KLEBER wrote:</DIV><BR class="Apple-interchange-newline"><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Olá<SPAN class="Apple-converted-space">  </SPAN>Igor ,</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Agradeço sua gentileza em me responder.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Entendi suas considerações e devo te dizer que não sou um especialista</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">em perl ( conheço com alguma profundidade cobol )</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">O pouco que conheço , aprendi lendo o livro Perl como programar ( deitel ).</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Sobre o tema controle de processo , estava imaginando controlá-lo , não<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">através de arquivo , mas pelo PID.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">( me desculpe se estou falando bobagem ).</DIV></BLOCKQUOTE><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Pelo que me recordo da sua mensagem inicial, você queria utilizar um controle de processos relacionado ao filehandle, isto é, não permitir que dois processos concorrentes modifiquem o mesmo arquivo, ao mesmo tempo. Isto você consegue com flock(), conforme você o fez e eu enviei uma abordagem mais perlish.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Agora, se você quer controlar processos concorrentes através de fork() e wait(), é um pouco diferente.</DIV><BR><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">De acordo com o livro mencionado , o pid está na variável<SPAN class="Apple-converted-space">  </SPAN>$PROCESS_ID</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">e existe uma função chamada WAIT( )<SPAN class="Apple-converted-space">  </SPAN>que aguarda o término de um processo.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">A minha necessidade seria resolvida formulando a seguinte indagação :</DIV></BLOCKQUOTE><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ENQUANTO $PROCESS_ID<SPAN class="Apple-converted-space">  </SPAN>ESTIVER<SPAN class="Apple-converted-space">  </SPAN>ATIVO WAIT( )</DIV></BLOCKQUOTE><DIV><BR class="khtml-block-placeholder"></DIV><DIV>A pergunta que fica é: você que está iniciando estes processos concorrentes, ou o sistema operacional?</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Ah, em relação à variável $PROCESS_ID, você precisa utilizar 'use English;' no início do seu programa (perldoc English para maiores informações).</DIV><BR><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Não sei se é possível este tipo de contole em perl e , se for , como<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">codificá-lo.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Nota - Consta as seguintes informações neste livro :</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">            </SPAN>a) Na variável especial<SPAN class="Apple-converted-space">  </SPAN>$PID <SPAN class="Apple-converted-space">  </SPAN>consta o numero do processo do</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>próprio programa.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">            </SPAN>b) Se quiser esperar que termine a execução de processo<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">específico ,</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>você pode usar a função WAITPID , que recebe um processo</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>como primeiro argumento.Ela espera que aquele processo<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">termine</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>e então devolve o pid daquele processo.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>Se não houver nenhum filho para aquele pid ou se já estiver<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">morto ,</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>a função WAITPID devolve -1.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>O estado de saida do processo filho é armazenada na variável</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>especial<SPAN class="Apple-converted-space">  </SPAN>$?.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">            </SPAN>c) Exemplo de controle de processo no livro ( dois processos ) :</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>my ( $pid, $pid2 );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>s| = 1;</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>if (<SPAN class="Apple-converted-space">  </SPAN>( $pid = fork()<SPAN class="Apple-converted-space">  </SPAN>) &amp;&amp; ( $pid2 = fork() ) ) {</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                      </SPAN>print ( "Preciso esperar meus filhos.\n" );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                      </SPAN>my $straggler = wait();</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                     </SPAN>print ( "Finalmente $straggler terminou.Agora eu posso<SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">ir.\n" );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>}</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                </SPAN>elsif ( $pid &amp;&amp; defined(<SPAN class="Apple-converted-space">  </SPAN>$pid2 ) ) {</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                        </SPAN>sleep( 2 );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                        </SPAN>print( "Filho 2 : O meu tambem....\n" );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                        </SPAN>sleep( 4 );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                        </SPAN>exit ();</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">              </SPAN>}</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">               </SPAN>elsif ( defined( $pid ) ) {</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                        </SPAN>sleep( 1 );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                       </SPAN>print( "Filho 1 : Meu pai tem paciencia......\n" );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                       </SPAN>sleep( 2 );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">              </SPAN>}</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">              </SPAN>else {</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">                  </SPAN>die(<SPAN class="Apple-converted-space">  </SPAN>"Problemas com fork " );</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">              </SPAN>}</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">             </SPAN>Acredito que neste programa exemplo esteja a solução do meu</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">             </SPAN>problema , entretanto , pôr não dominar o perl , não sinto</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-converted-space">             </SPAN>segurança para interpletá-lo.</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Mais uma vez , agradeço a sua atenção ,</DIV></BLOCKQUOTE></DIV><DIV><BR class="khtml-block-placeholder"></DIV>Certo, vamos lá com um exemplo de fork():</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>&lt;code&gt;</DIV><DIV>use strict;</DIV><DIV>use warnings;</DIV><DIV><BR></DIV><DIV>use POSIX qw(:sys_wait_h);</DIV><DIV><BR></DIV><DIV>sub message {</DIV><DIV>    print "[$$] ", @_, $/;</DIV><DIV>}</DIV><DIV><BR></DIV><DIV>sub REAPER {</DIV><DIV>    while ( ( my $child = waitpid( -1, WNOHANG ) ) &gt; 0 ) {</DIV><DIV>        message( "filho $child morreu", $/ );</DIV><DIV>    }</DIV><DIV>    $SIG{CHLD} = \&amp;REAPER;</DIV><DIV>}</DIV><DIV>$SIG{CHLD} = \&amp;REAPER;</DIV><DIV><BR></DIV><DIV>my $pid = fork();</DIV><DIV>unless ( defined $pid ) {</DIV><DIV>    die "problemas ao executar fork(): $!";</DIV><DIV>}</DIV><DIV><BR></DIV><DIV>if ( $pid &gt; 0 ) {    # estamos no processo pai</DIV><DIV>    message( "iniciei o processo $pid", $/ );</DIV><DIV>    sleep 5;</DIV><DIV>    message( "pai esta saindo", $/ );</DIV><DIV>        exit;</DIV><DIV>}</DIV><DIV>else {               # estamos no processo filho</DIV><DIV>    message( "eu sou o filho!", $/ );</DIV><DIV>    exit;</DIV><DIV>}</DIV><DIV><BR>&lt;/code&gt;<BR></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Para maiores informações:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>perldoc perlipc</DIV><DIV>perldoc POSIX</DIV><DIV>perldoc -f fork</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>A propósito: o que é que você quer fazer, afinal? Descreva-nos o seu problema, assim fica mais fácil de ajudá-lo :-)</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Abraço!</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><DIV> <SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Lucida Grande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><DIV>--</DIV><DIV>Igor Sutton</DIV><DIV><A href="mailto:igor.sutton@gmail.com">igor.sutton@gmail.com</A></DIV><DIV><BR class="khtml-block-placeholder"></DIV><BR class="Apple-interchange-newline"></SPAN> </DIV><BR></DIV></BODY></HTML>