步骤:
1.运行命令:./ext_skel --extname=sharemem
2.运行命令:./configure --with-php-config=/usr/local/lnmp/php/bin/php-config
3.make clean
make
make install
/usr/local/lnmp/php/sbin/php-fpm restart
/usr/local/lnmp/php/bin/php-cgi /data0/htdocs/blog/sharemem.php
代码如下:
1.config.m4
PHP_ARG_ENABLE(sharemem, whether to enable sharemem support,
Make sure that the comment is aligned:
[ --enable-sharemem Enable sharemem support])
if test "$PHP_SHAREMEM" != "no"; then
PHP_NEW_EXTENSION(sharemem, sharemem.c, $ext_shared)
fi
2.php_sharemem.h
#ifndef PHP_SHAREMEM_H
#define PHP_SHAREMEM_H
extern zend_module_entry sharemem_module_entry;
#define phpext_sharemem_ptr &sharemem_module_entry
#ifdef PHP_WIN32
#define PHP_SHAREMEM_API __declspec(dllexport)
#else
#define PHP_SHAREMEM_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
/*hanhhh*/
#include "sys/mman.h"
#include "fcntl.h"
#include "semaphore.h"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/*hanhhh*/
typedef struct php_shared_mm {
sem_t mutex; /* the mutex: a Posix memory-based semaphore */
int count; /* and the counter */
} php_shared_mm;
PHP_MINFO_FUNCTION(sharemem);
PHP_FUNCTION(say8_count_add); /* For testing, remove later. */
PHP_FUNCTION(say8_count_dec); /* For testing, remove later. */
#endif /* PHP_SHAREMEM_H */
3.sharemem.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sharemem.h"
zend_function_entry sharemem_functions[] = {
PHP_FE(say8_count_add, NULL)
PHP_FE(say8_count_dec, NULL)
{NULL, NULL, NULL} /* Must be the last line in sharemem_functions[] */
};
/* }}} */
/* {{{ sharemem_module_entry
*/
zend_module_entry sharemem_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sharemem",
sharemem_functions,
NULL,
NULL,
NULL, /* Replace with NULL if there's nothing to do at request start */
NULL, /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(sharemem),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SHAREMEM
ZEND_GET_MODULE(sharemem)
#endif
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(sharemem)
{
php_info_print_table_start();
php_info_print_table_header(2, "sharemem support", "enabled");
php_info_print_table_end();
}
/* }}} */
PHP_FUNCTION(say8_count_add){
int fd;
struct php_shared_mm *ptr;
fd = open("/tmp/xx", O_RDWR, FILE_MODE);
if(fd < 0)
{
fd = open("/tmp/xx", O_RDWR|O_CREAT, FILE_MODE);
assert(fd > 0);
ftruncate(fd, sizeof(struct php_shared_mm));
//write(fd, &php_shared_mm, sizeof(struct php_shared_mm));
}
ptr =(struct php_shared_mm *) mmap(NULL, sizeof(struct php_shared_mm), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
assert(sem_init(&ptr->mutex, 1, 1) == 0);
setbuf(stdout, NULL);
assert(sem_wait(&ptr->mutex) >=0 );
ptr->count++;
// printf ( "%d/n", ptr->count );
assert(sem_post(&ptr->mutex) >=0 );
// usleep(90000);
RETURN_LONG(ptr->count);
RETURN_TRUE;
}
PHP_FUNCTION(say8_count_dec){
int fd;
struct php_shared_mm *ptr;
fd = open("/tmp/xx", O_RDWR, FILE_MODE);
if(fd < 0)
{
fd = open("/tmp/xx", O_RDWR|O_CREAT, FILE_MODE);
assert(fd > 0);
ftruncate(fd, sizeof(struct php_shared_mm));
//write(fd, &php_shared_mm, sizeof(struct php_shared_mm));
}
ptr =(struct php_shared_mm *) mmap(NULL, sizeof(struct php_shared_mm), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
assert(sem_init(&ptr->mutex, 1, 1) == 0);
setbuf(stdout, NULL);
assert(sem_wait(&ptr->mutex) >=0 );
ptr->count--;
// printf ( "%d/n", ptr->count );
assert(sem_post(&ptr->mutex) >=0 );
// usleep(90000);
RETURN_LONG(ptr->count);
RETURN_TRUE;
}