参考:http://www.cnblogs.com/yjf512/p/3217615.html
还是老三样:phpize, ./configure, make && make install
当然是要到你下载的php源码的ext目录中执行。
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ ./configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... i386-apple-darwin13.4.0
checking host system type... i386-apple-darwin13.4.0
checking target system type... i386-apple-darwin13.4.0
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking whether to enable pcntl support... yes, shared
checking for fork... yes
checking for waitpid... yes
checking for sigaction... yes
checking for getpriority... yes
checking for setpriority... yes
checking for wait3... yes
checking for sigprocmask... yes
checking for sigwaitinfo... no
checking for sigtimedwait... no
checking for ld used by cc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 196608
checking command to parse /usr/bin/nm output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking if cc supports -fno-rtti -fno-exceptions... yes
checking for cc option to produce PIC... -fno-common
checking if cc PIC flag -fno-common works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin13.4.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ make && make install
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/pcntl/libtool --mode=install cp ./pcntl.la /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules
cp ./.libs/pcntl.so /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules/pcntl.so
cp ./.libs/pcntl.lai /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules/pcntl.la
----------------------------------------------------------------------
Libraries have been installed in:
/Users/andy/Downloads/php-5.4.30/ext/pcntl/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
during execution
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20100525/
cp: /usr/lib/php/extensions/no-debug-non-zts-20100525/#INST@3041#: Permission denied
make: *** [install-modules] Error 1
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ php -m
[PHP Modules]
andy
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
hash
iconv
json
ldap
libxml
mbstring
mssql
mysql
mysqli
mysqlnd
openssl
pcre
PDO
pdo_dblib
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
snmp
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
Xdebug
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ vim /private/etc/php.ini
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ sudo vim /private/etc/php.ini
Password:
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ php -m
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20100525/pcntl' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20100525/pcntl, 9): image not found in Unknown on line 0
[PHP Modules]
andy
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
hash
iconv
json
ldap
libxml
mbstring
mssql
mysql
mysqli
mysqlnd
openssl
pcre
PDO
pdo_dblib
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
snmp
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
Xdebug
我们看到扩展了还是没有pcntl扩展,可以看到上面有一个cp的error,所以我们sudo一下:
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ sudo make
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/pcntl/libtool --mode=install cp ./pcntl.la /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules
cp ./.libs/pcntl.so /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules/pcntl.so
cp ./.libs/pcntl.lai /Users/andy/Downloads/php-5.4.30/ext/pcntl/modules/pcntl.la
----------------------------------------------------------------------
Libraries have been installed in:
/Users/andy/Downloads/php-5.4.30/ext/pcntl/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
during execution
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/pcntl$ sudo make install
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20100525/
andy@AndyMacBookPro:/usr/lib/php/extensions/no-debug-non-zts-20100525$ php -m
[PHP Modules]
andy
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
hash
iconv
json
ldap
libxml
mbstring
mssql
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
snmp
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tidy
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib
[Zend Modules]
Xdebug
andy@AndyMacBookPro:/usr/lib/php/extensions/no-debug-non-zts-20100525$
好了。
下面可以写个示例试试:
for($x = 1;$x<= 2;$x++){ $pid[$x] = pcntl_fork(); if ($pid[$x] == -1) { die("could not fork"); } elseif ($pid[$x]) { echo "Parent: create ".$pid[$x]."n"; } else { echo "fork ".getmypid()." start:n"; for($i = 0;$i<10;$i++){ echo $x.": ".$i."n"; sleep(1); } exit; } }
输出结果是这样的:
andy@AndyMacBookPro:/usr/local/webdata/andy/php/pcntl$ php pcntl_demo.php
Parent: create 3641
Parent: create 3642
fork 3641 start:
1: 0
fork 3642 start:
2: 0
andy@AndyMacBookPro:/usr/local/webdata/andy/php/pcntl$ 1: 1
2: 1
1: 2
2: 2
1: 3
2: 3
1: 4
2: 4
2: 5
1: 5
1: 6
2: 6
1: 7
2: 7
1: 8
2: 8
1: 9
2: 9
andy@AndyMacBookPro:/usr/local/webdata/andy/php/pcntl$
这个时候我们去ps -ef|grep php的话,可以看到有两个php进程:
如果我们改一下代码,加上19行后面的代码:
<?php for($x = 1;$x<= 2;$x++){ $pid[$x] = pcntl_fork(); if ($pid[$x] == -1) { die("could not fork"); } elseif ($pid[$x]) { echo "Parent: create ".$pid[$x]."\n"; } else { echo "fork ".getmypid()." start:\n"; for($i = 0;$i<10;$i++){ echo $x.": ".$i."\n"; sleep(1); } exit; } } while(count($pid) > 0){ $myId = pcntl_waitpid(-1, $status, WNOHANG); foreach($pid as $k => $v){ if($myId == $v) unset($pid[$k]); } usleep(100); } ?>
可能有人会问WNOHANG是什么意思,W NO HANG,也就是wait no hang,不阻塞的意思。
这个时候我们再执行,可以看到有三个php进程,最后一个php进程其实就是最开始的主进程,本来如果没有加上的后面这段,主进程实例化出来两个子进程之后就退出了,但是加上了这段,就会让主进程等待所有子进程都结束之后,再结束自己。