问题
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