On Wed, 09 Jan 2008, Lorenzo Fiorini wrote:
> >   But isn't it simpler to use getpid() to determine the pid of a process
> > instead of forking it ?
> Probably yes but I didn't find viable examples.
> My idea is to create a series of HB_RUNxxx functions that controls
> processes, sth like:
> HB_RUN( cCmd )
> HB_RUNKILL( pid )
> HB_RUNLIST( [ cUser|regex] )

Try the code below. It makes what you are looking for.
Compile example with GTCGI for better merge 'ps xa' and program output.

best regrds,
Przemek


proc main()
local aPID:={}, i
for i:=1 to 10
   aadd( aPID, HB_EXECVP( "sleep", ltrim( str( i ) ) ) )
next
__run("ps xa|grep sleep|grep -v grep")
hb_idlesleep(3.5)
?
__run("ps xa|grep sleep|grep -v grep")
for i:=1 to len(aPID)
   if HB_WAITPID( aPID[i], .t. ) > 0
      ? "Process: ", ltrim( str( aPID[i] ) ), "died."
   else
      ? "Killing process:", ltrim( str( aPID[i] ) ), "..."
      ?? iif( HB_KILL(aPID[i],15), " done", "error" )
      HB_WAITPID( aPID[i] )
      ?? ", process terminated"
   endif
next
__run("ps xa|grep sleep|grep -v grep")
?
return

#pragma begindump
#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapierr.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
HB_FUNC( HB_EXECVP )
{
   char * szProgName = hb_parc( 1 );

   if( szProgName )
   {
      char ** argv;
      int argc = 1;
      pid_t pid;
      int iPCount = hb_pcount(), iParam;

      for( iParam = 2; iParam <= iPCount; ++iParam )
      {
         PHB_ITEM pParam = hb_param( iParam, HB_IT_ARRAY | HB_IT_STRING );
         if( pParam )
         {
            if( HB_IS_ARRAY( pParam ) )
            {
               ULONG ul = hb_arrayLen( pParam );
               if( ul ) do
               {
                  if( hb_arrayGetType( pParam, ul ) & HB_IT_STRING )
                     ++argc;
               }
               while( --ul );
            }
            else
               ++argc;
         }
      }

      argv = ( char ** ) hb_xgrab( sizeof( char * ) * ( argc + 1 ) );
      argc = 0;
      argv[ argc++ ] = szProgName;

      for( iParam = 2; iParam <= iPCount; ++iParam )
      {
         PHB_ITEM pParam = hb_param( iParam, HB_IT_ARRAY | HB_IT_STRING );
         if( pParam )
         {
            if( HB_IS_ARRAY( pParam ) )
            {
               ULONG ul = hb_arrayLen( pParam );
               if( ul ) do
               {
                  if( hb_arrayGetType( pParam, ul ) & HB_IT_STRING )
                     argv[ argc++ ] = hb_arrayGetCPtr( pParam, ul );
               }
               while( --ul );
            }
            else
               argv[ argc++ ] = hb_itemGetCPtr( pParam );
         }
      }
      argv[ argc ] = NULL;

      pid = fork();
      if( pid == 0 )
      {
         execvp( szProgName, argv );
         exit( errno );
      }
      hb_xfree( argv );
      hb_retnint( pid );
   }
   else
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, &hb_errFuncName, 
HB_ERR_ARGS_BASEPARAMS );
}

HB_FUNC( HB_KILL )
{
   if( ISNUM( 1 ) )
      hb_retl( kill( hb_parnl( 1 ), hb_parnl( 2 ) ) == 0 );
   else
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, &hb_errFuncName, 
HB_ERR_ARGS_BASEPARAMS );
}

HB_FUNC( HB_WAITPID )
{
   if( ISNUM( 1 ) )
   {
      int status;
      hb_retnint( waitpid( ( pid_t ) hb_parnint( 1 ), &status,
                  ISLOG( 2 ) && hb_parl( 2 ) ? WNOHANG : 0 ) );
   }
   else
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, &hb_errFuncName, 
HB_ERR_ARGS_BASEPARAMS );
}
#pragma enddump
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to