views:

108

answers:

3

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.

+4  A: 

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

Alex Farber
And the error message is incorrect -- there is no socket involved.
Darron
segment_id = -1
Meloun
+2  A: 

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++.

Jens Gustedt
A: 

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

Meloun