IMPORTANT: Per accedir als fitxer de subversion: http://acacha.org/svn (sense password). Poc a poc s'aniran migrant els enllaços. Encara però funciona el subversion de la farga però no se sap fins quan... (usuari: prova i la paraula de pas 123456)

Control de processos amb PHP (Process Control Extensions)


Program execution Functions

Recursos:

Funcions:

  • escapeshellarg — Escape a string to be used as a shell argument
  • escapeshellcmd — Escape shell metacharacters
  • exec — Execute an external program
  • passthru — Execute an external program and display raw output
  • proc_close — Close a process opened by proc_open and return the exit code of that process.
  • proc_get_status — Get information about a process opened by proc_open
  • proc_nice — Change the priority of the current process
  • proc_open — Execute a command and open file pointers for input/output
  • proc_terminate — Kills a process opened by proc_open
  • shell_exec — Execute command via shell and return the complete output as a string
  • system — Execute an external program and display the output

PCNTL

Extret de http://es2.php.net/manual/en/intro.pcntl.php

Process Control support in PHP implements the Unix style of process creation, program execution, signal handling and process termination. Process 
Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web 
server environment.

This documentation is intended to explain the general usage of each of the Process Control functions. For detailed information about Unix process 
control you are encouraged to consult your systems documentation including fork(2), waitpid(2) and signal(2) or a comprehensive reference such as 
Advanced Programming in the UNIX Environment by W. Richard Stevens (Addison-Wesley).

PCNTL now uses ticks as the signal handle callback mechanism, which is much faster than the previous mechanism. This change follows the same semantics 
as using "user ticks". You use the declare() statement to specify the locations in your program where callbacks are allowed to occur. This allows you to 
minimize the overhead of handling asynchronous events. In the past, compiling PHP with pcntl enabled would always incur this overhead, whether or not 
your script actually used pcntl. 

There is one adjustment that all pcntl scripts prior to PHP 4.3.0 must make for them to work which is to either to use declare() on a section where you 
wish to allow callbacks or to just enable it across the entire script using the new global syntax of declare(). 

El suport de control de processos de PHP no està activat per defecte. Per activar-lo, tens que compilar la versió CGI o CLI del PHP amb l'opció:

Funcions

  • pcntl_alarm — Set an alarm clock for delivery of a signal
  • pcntl_exec — Executes specified program in current process space
  • pcntl_fork — Forks the currently running process
  • pcntl_getpriority — Get the priority of any process
  • pcntl_setpriority — Change the priority of any process
  • pcntl_signal_dispatch — Calls signal handlers for pending signals
  • pcntl_signal — Installs a signal handler
  • pcntl_sigprocmask — Sets and retrieves blocked signals
  • pcntl_sigtimedwait — Waits for signals, with a timeout
  • pcntl_sigwaitinfo — Waits for signals
  • pcntl_wait — Waits on or returns the status of a forked child
  • pcntl_waitpid — Waits on or returns the status of a forked child
  • pcntl_wexitstatus — Returns the return code of a terminated child
  • pcntl_wifexited — Checks if status code represents a normal exit
  • pcntl_wifsignaled — Checks whether the status code represents a termination due to a signal
  • pcntl_wifstopped — Checks whether the child process is currently stopped
  • pcntl_wstopsig — Returns the signal which caused the child to stop
  • pcntl_wtermsig — Returns the signal which caused the child to terminate

Recursos

pcntl_fork()

pcntl_fork — Forks the currently running process

pcntl_exec()

Similar a les funcions equivalents de C, la funció pcntl_exec() permet executar una aplicació externa. A diferencia del que succeïx en C, amb PHP només hi ha una funció

Sintaxi:

 void pcntl_exec  ( string $path  [, array $args  [, array $envs  ]] )

Els paràmetre són:

  • path: Camí de l'executable que volem executar o guió de Shell amb la primera línia indicant quin és l'executable a utilitzar (per exemple: #!/usr/local/bin/perl si es un script fet en perl)
  • args: un vector de cadenes de caràcters (String) amb els paràmetres a passar a l'executable indicar al paràmetre anterior (path)
  • envs: un vector de cadenes de caràcters (String) que es passen a l'entorn d'execució del programa. El vector esta en format nom => valor, on nom és el nom de la variable d'entorn i valor és el valor de la variable d'entorn

Exemple de codi:

<?php
//El primer paràmetre és el nom d'usuari

$username = $_SERVER['argv'][1];

echo $username;

$user = posix_getpwnam($username);
posix_setuid($user['uid']);
posix_setgid($user['gid']);
pcntl_exec('/sbin/ifconfig'); 

?>


Vegeu també:

pcntl_wait(),pcntl_wait() i pcntl_wexitstatus()

  • pcntl_wait — Waits on or returns the status of a forked child
  • pcntl_waitpid — Waits on or returns the status of a forked child
  • pcntl_wexitstatus — Returns the return code of a terminated child

pcntl_getpriority() i pcntl_setpriority()

  • pcntl_setpriority — Change the priority of any process
  • pcntl_getpriority — Get the priority of any process


--enable-pcntl

POSIX

PHP té suport per a POSIX amb les funcions de POSIX

http://es2.php.net/manual/en/book.posix.php

Interprocess Comunication (IPC)

Semàfors:

Memòria Compartida:

Exemples

<?php
declare(ticks=1);

$pid = pcntl_fork();
if ($pid == -1) {
     die("could not fork"); 
} else if ($pid) {
     exit(); // we are the parent 
} else {
     // we are the child
}

// detatch from the controlling terminal
if (posix_setsid() == -1) {
    die("could not detach from terminal");
} 
 
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");

// loop forever performing tasks
while (1) {  

    // do something interesting here 

}

function sig_handler($signo) 
{   

     switch ($signo) {
         case SIGTERM:
             // handle shutdown tasks
             exit;
             break;
         case SIGHUP:
             // handle restart tasks
             break;
         default:
             // handle all other signals
     } 

}

?>

'Recursos:

Pear

System Daemon Package

Permet convertir scripts de PHP en dimonis Unix.

Recursos: