[Système] pcntl_fork() : duplication de processus ?
Bonjour,
Je voudrais creer des processus pour executer des scripts php en parallele.
Pour cela j'utilise la fonction pcntl_fork().
voila mon code :
check_ack_thread.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#!/usr/bin/php -q
<?php
$d = true;
for ($i = 0 ; $i < 10 ; $i++){
echo "loop [".$i."]\n\n";
$i_child_pid = pcntl_fork();
if ($d) echo "FORK PID: $i_pid\n";
if ($i_child_pid == -1) {
echo "Could not fork(). Exiting.\n";
exit;
}
if($i_child_pid){ // parent
if ($d) echo "Parent: child's pid is: ".$i_child_pid."\n";
if ($d) echo "A_Pids:\n";
# pcntl_wait($status);
sleep(1); // sleep to try and give each one a little breathing room
}
else
{ // child
system("./check_ack_test.php ".$i);
}
}
?> |
Dans mon script "check_ack_test.php" je fais different sleep pour voir si mes thread fonctionne bien.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
#!/usr/bin/php -q
<?php
if ($_SERVER['argc'] > 1){
$i = $_SERVER['argv'][1];
echo "TIME script [".$i."] : ".date("H:i:s")."\n";
sleep($i/2);
echo "SELECT FROM table WHERE id = ".$i."\n";
echo "TIME END [".$i."] : ".date("H:i:s")."\n";
}
?> |
cependant si je regarde les processus sur mon server voila ce que j'obtiens :
Citation:
-bash-3.2$ ps -ef | grep php
501 2949 2857 1 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2950 2949 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2952 2950 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2954 2952 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2956 2949 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2958 2950 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 2
501 2960 2956 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2961 2960 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 2
501 2962 2952 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2963 2962 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 3
501 2964 2954 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2965 2964 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 3
501 2967 2907 0 18:13 pts/1 00:00:00 grep php
Autrement dit il me creer plusieurs processus avec le meme parametre:
./check_ack_test.php 2
on le voit 2 fois.
Ce que je veux c'est creer 10 processus avec 10 parametres differents.
Je devrais avoir cela dans mes processus:
Citation:
-bash-3.2$ ps -ef | grep php
501 2958 2950 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_thread.php
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 0
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 1
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 2
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 3
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 4
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 5
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 6
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 7
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 8
501 2959 2958 0 18:13 pts/0 00:00:00 /usr/bin/php -q ./check_ack_test.php 9
Je suis un peu perdu. J'ai commencer a lire de la doc sur fork, mais je ne vois toujours pas pourquoi mon script ne fonctionne pas comme je le voudrais.
Merci