C shared memory

夙愿已清 提交于 2021-01-28 19:17:08

问题


I am trying to implement shared memory on embedded device with uClinux.

My C source

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <errno.h>

//using namespace std;

int main() {
       int segment_id;

       segment_id = shmget(04,  getpagesize(), IPC_CREAT | 0666);

       printf("Page size - %d\n",getpagesize());
       printf("Error in socket - %d\n",errno);
}

I get an error

Page size - 4096
Error in socket - 38

Can anyone help me? Thanks.


回答1:


You need to test segment_id value, and use errno only if segment_id == -1.




回答2:


Your key 04 looks completely bogus. You should obtain a key_t with ftok, I guess.

Also, if you have the choice, it might be better to choose the shm_open / mmap facilities for such a task.

And since I am at it, use perror to print errors, and also please remove C++ from your question title, has nothing to do with C++.




回答3:


The errno 38 corresponds to ENOSYS which means function not implemented. I missed a kernel config. I have to enable CONFIG_SYSVIPC.



来源:https://stackoverflow.com/questions/3760552/c-shared-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!