php实现多进程,源文件:
<?php
$pid = pcntl_fork();
if($pid == -1){
die('could not fork');
}else{
if ($pid) {
var_dump('parent:'.$pid);
exit('parent');
}else{
var_dump('child:'.$pid);
exit('child');
}
}
?>
但运行提示出错:
andy@AndyMacBookPro:/usr/local/webdata/andy/php/duojincheng$ php pcntl.php
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20100525/pcntl.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20100525/pcntl.so, 9): image not found in Unknown on line 0
PHP Fatal error: Call to undefined function pcntl_fork() in /usr/local/webdata/andy/php/duojincheng/pcntl.php on line 4
PHP Stack trace:
PHP 1. {main}() /usr/local/webdata/andy/php/duojincheng/pcntl.php:0
andy@AndyMacBookPro:/usr/local/webdata/andy/php/duojincheng$
这是因为我们本机的php环境中没有pcntl.so扩展,所以我们下面来为php安装此扩展:
首先了解下自己本机系统中的php-config在哪里:
Last login: Sun Sep 28 17:04:12 on ttys002
andy@AndyMacBookPro:~$ sudo find / -name php-config
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/bin/php-config
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/Users/andy/Downloads/php-5.4.30/scripts/php-config
/usr/bin/php-config
/usr/local/bin/php-config
/usr/local/Cellar/php54/5.4.30/bin/php-config
andy@AndyMacBookPro:~$ ll /usr/bin/php-config
-rwxr-xr-x 1 root wheel 3672 9 22 09:37 /usr/bin/php-config*
andy@AndyMacBookPro:~$ ll /usr/local/bin/php-config
lrwxr-xr-x 1 andy admin 37 7 1 13:04 /usr/local/bin/php-config@ -> ../Cellar/php54/5.4.30/bin/php-config
andy@AndyMacBookPro:~$
查看/usr/bin/php-config:
1 #! /bin/sh
2
3 SED="/usr/bin/sed"
4 prefix="/usr"
5 datarootdir="/usr/php"
6 exec_prefix="${prefix}"
7 version="5.4.30"
8 vernum="50430"
9 include_dir="${prefix}/include/php"
10 includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_d ir/ext/date/lib"
11 ldflags=" -L/BinaryCache/FreeType/FreeType-18~1619/Root/usr/local/lib"
12 libs="-lresolv -lz -lexslt -ltidy -lresolv -ledit -lncurses -lldap -llber -liconv -liconv -lpng -lz -ljpeg -lcrypto -lssl -lcrypto -lcurl -lbz2 -lz -lpcre -lcrypto -lssl -lcrypto -lm -lxml2 -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_er r -lcurl -lxml2 -lfreetype -lz -lxml2 -lnetsnmp -lcrypto -lxml2 -lxml2 -lxml2 -lxml2 -lxml2 -lxml2 -lxml2 -lxslt"
13 extension_dir='/usr/lib/php/extensions/no-debug-non-zts-20100525'
14 man_dir=`eval echo /usr/share/man`
15 program_prefix=""
16 program_suffix=""
17 exe_extension=""
18 php_cli_binary=NONE
19 php_cgi_binary=NONE
20 configure_options=" '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-trac king' '--sysconfdir=/private/etc' '--with-apxs2=/usr/sbin/apxs' '--enable-cli' '--with-config-file-path=/etc' '--wit h-config-file-scan-dir=/Library/Server/Web/Config/php' '--with-libxml-dir=/usr' '--with-openssl=/usr' '--with-kerber os=/usr' '--with-zlib=/usr' '--enable-bcmath' '--with-bz2=/usr' '--enable-calendar' '--disable-cgi' '--with-curl=/us r' '--enable-dba' '--enable-ndbm=/usr' '--enable-exif' '--enable-fpm' '--enable-ftp' '--with-gd' '--with-freetype-di r=/usr/local' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr/local' '--enable-gd-native-ttf' '--with-icu-dir=/usr ' '--with-ldap=/usr' '--with-ldap-sasl=/usr' '--with-libedit=/usr' '--enable-mbstring' '--enable-mbregex' '--with-my sql=mysqlnd' '--with-mysqli=mysqlnd' '--without-pear' '--with-pdo-mysql=mysqlnd' '--with-mysql-sock=/var/mysql/mysql .sock' '--with-readline=/usr' '--enable-shmop' '--with-snmp=/usr' '--enable-soap' '--enable-sockets' '--enable-sqlit e-utf8' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-tidy' '--enable-wddx' '--with-xmlrpc' '--wi th-iconv-dir=/usr' '--with-xsl=/usr' '--enable-zend-multibyte' '--enable-zip' '--with-pcre-regex=/usr'"
21 php_sapis=" apache2handler cli fpm"
22
23 # Set php_cli_binary and php_cgi_binary if available
24 for sapiin$php_sapis;do
25 case$sapiin
26 cli)
27 php_cli_binary="${exec_prefix}/bin/${program_prefix}php${program_suffix}${exe_extension}"
28 ;;