简体   繁体   English

PHP / C ++:共享内存时出现shm_open()错误

[英]PHP / C++: shm_open() error when sharing memory

I've been all over the internet looking for an answer to my problem. 我一直在互联网上寻找问题的答案。 Here is the setup, I am running embedded Linux (created with Yocto) which is running the Lighttpd web server with PHP5. 这是设置,我正在运行嵌入式Linux(使用Yocto创建),该Linux使用PHP5运行Lighttpd Web服务器。 In my C++ code I have the following: 在我的C ++代码中,我具有以下内容:

shared = shm_open(SHARED_FILE_NAME, O_RDWR | O_CREAT | O_TRUNC, 0666);
ftruncate(shared, FILE_SIZE);
map = mmap(...);
// shm_unlink() isn't called until my C++ thread ends.

Everything works well and I do not get any errors and other C++ processes and threads are also able to access the shared memory and map without any problems (I have one writer thread and all other threads and processes do a read only on the memory). 一切正常,我没有收到任何错误,其他C ++进程和线程也能够访问共享内存并映射而没有任何问题(我有一个writer线程,所有其他线程和进程都对内存进行只读操作)。 The memory is used as a ring buffer where the writing thread is updating data very quickly. 内存用作环形缓冲区,其中写入线程可以非常快速地更新数据。 The problems start to occur when trying to access that same memory in PHP. 当尝试在PHP中访问相同的内存时,问题开始出现。 In PHP I do (need read only): 在PHP中,我这样做(需要只读):

<?php
$shm_key = ftok("/dev/shm/shared_file.shm", 'c');
$shm_id = shm_open($shm_key, "a", 0, 0);
...
?>

When looking at the value from ftok() it returns a non -1 number which means it did not fail. 当查看ftok()的值时,它返回一个非-1数字,这表示它没有失败。 I do get a fail on the PHP's shm_open() call which reads: 我确实在PHP的shm_open()调用上失败,该调用显示为:

Warning: shmop_open(): unable to attach or create shared memory segment in /www/pages/shared.php on line 9

I've changed the permission of the file with chmod 777 /dev/shm/shared.shm just to rule out any file permission issues. 我已经使用chmod 777 /dev/shm/shared.shm更改了文件的权限,只是为了排除任何文件权限问题。 Also when I run ipcs -m I do not get any listings for shared memory segments, yet my C++ code is running just fine. 同样,当我运行ipcs -m我没有得到共享内存段的任何清单,但是我的C ++代码运行得很好。 I've also looked for SELinux and tried entering setenforce 0 but I get a response of -sh: setenforce: command not found so I figure this isn't an issue. 我还寻找了SELinux,并尝试输入setenforce 0但是得到了-sh: setenforce: command not found的响应,所以我-sh: setenforce: command not found这不是问题。 I've also tried running wget <local ip address>/shared.php to see if running locally would return the correct data but when looking at the file which was returned it had the same error messages. 我也尝试过运行wget <local ip address>/shared.php以查看是否在本地运行将返回正确的数据,但是当查看返回的文件时,它具有相同的错误消息。

I am looking to be able to have a web page on my embedded system read this shared memory and stream back chunks of binary to feed a graph when a request comes in (not interested in web sockets at the time). 我希望能够让我的嵌入式系统上的网页读取此共享内存,并在请求进入时(当时对Web套接字不感兴趣)流回二进制数据块以提供图形。 I am able to get named pipes to work across PHP and C++ just fine but I need shared memory for this application and the shared memory access seems to be troublesome. 我能够使命名管道在PHP和C ++上正常工作,但是我需要此应用程序的共享内存,并且共享内存访问似乎很麻烦。 Any help is appreciated. 任何帮助表示赞赏。

I'm developing PHP functions that need to use C Shared Memory. 我正在开发需要使用C共享内存的PHP函数。 As your code, my C functions use shm_open, mmap, etc.. and I guess to use PHP ftok(), shmop_open() to access the C's shared memory but this PHP functions don't work. 作为您的代码,我的C函数使用shm_open,mmap等。我猜想使用PHP ftok(),shmop_open()来访问C的共享内存,但是此PHP函数不起作用。
The two area are not compatible. 这两个区域不兼容。 I found different properties of the two areas in this documents http://menehune.opt.wfu.edu/Kokua/More_SGI/007-2478-008/sgi_html/ch03.html : 我在此文档http://menehune.opt.wfu.edu/Kokua/More_SGI/007-2478-008/sgi_html/ch03.html中找到了这两个区域的不同属性:

  • C (with shm_open, mmap, like the Straton source code) use “POSIX Shared Memory” C(带有shm_open,mmap,类似于Straton源代码)使用“ POSIX共享内存”
  • PHP (with shmop_* functions) use “System V Shared Memory” PHP(具有shmop_ *函数)使用“ System V共享内存”

I suggest you to try with Sync http://php.net/manual/en/book.sync.php : you need the PECL sync extension. 我建议您尝试使用Sync http://php.net/manual/en/book.sync.php :您需要PECL同步扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM