Take the 2-minute tour ×

I am attempting to allocate some amount of shared memory based on a variable shmsz which is always some multiple of 8. In most cases, it's either 8 or 16.

Using the following code:

// allocate shared memory
if ((shmid = shmget(IPC_PRIVATE, shmsz, IPC_CREAT | 0666)) < 0) {
  printf("Could not allocate shared memory.\n");

This used to work like a charm. But now, I am always getting the error message and having my program exit on me.

EDIT: errno() shows "Bus Error: 10"

share|improve this question
How about looking at the value of errno when the call fails, that might give a hint... –  Seg Fault Sep 27 '12 at 17:15
And what does the errno say? –  Nikolai N Fetissov Sep 27 '12 at 17:16
Don't forget #include <errno.h> –  Nikolai N Fetissov Sep 27 '12 at 17:52
Seems you are getting a signal SIGBUS and the program is terminating at that point.Are you sure the signal is due to this line or any subsequent lines? What is the plaform? is there any core files generated? Can you run gdb on that core file to see exactly which line is generating SIGBUS? Here most likely errno may not help. At least, this line with shmz = 8, is not raising any SIGBUS in my Fedora 15. The shm segment is successfully created.Can you pl. paste some more code, before/after this line? –  Tanmoy Sep 27 '12 at 18:05
errno is not a function - you cannot call it with (), it's an integer value for all purposes. –  Nikolai N Fetissov Sep 27 '12 at 18:23

1 Answer 1

Sometimes, when there is an violation of access in that shared memory (such as trying to write in a shared memory where you only have read rights), the process is sent SIGSEGV or SIGBUS.

In your case, as you are creating it with 0666, there is only one possible situation for this scenario: so are you trying to execute code from that shared memory?

share|improve this answer

Your Answer


