0

I got a question about shared memory and segmentation fault. I thought that it would be fine to use huge size of memory. When I checked Shmmax, i found the huge memory can be allocated.

under data is the result of $ipcs -lm

------ Shared Memory Limits --------

max number of segments = 4096

max seg size (kbytes) = 18014398509465599

max total shared memory (kbytes) = 18014398442373116

min seg size (bytes) = 1

#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h> 
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

#define ARRAY_SIZE 40000000


int main(int argc, char *argv[]){

    int shmid;
    void *shared_memory = (void *)0;
    shmid = shmget((key_t)1234, sizeof(float), IPC_CREAT|0666);
    if (shmid == -1)
    {
        perror("shmget failed : ");
        exit(0);
    }

    shared_memory = (float *)shmat(shmid, NULL, 0);
    if (shared_memory == (void *)-1)
    {
        perror("shmat failed : ");
        exit(0);
    }

    static float *testx;
    testx = (float *)shared_memory;

    int k = 0;
    for(k;k<400;k++){
        testx[k] = 1.12;
    }
    for(k;k<40000000;k++){
        testx[k] = 1.12;
    }
}

the program can run the first for loop which has small amount of size

the problem, however, is the second loop with 40,000,000 size

any suggestion what should i edit to run this code?

2
  • 2
    You are allocating space for 1 float but are trying to write to 40,000,000 floats. Commented Apr 15, 2017 at 12:44
  • To obtain shared memory segments, consider using shm_open, truncate and mmap if your system has them. These are much easier to set up. Commented Apr 15, 2017 at 17:45

1 Answer 1

2

The reason for your SEGFAULT is that you haven't created enough size segment with shmget.

The argument you passed to shmget as size is sizeof(float) which is just enough to store 1 float.

What you need to do is call shmget like this -

shmget((key_t)1234, sizeof(float)*40000000, IPC_CREAT|0666);

Then you can use all the memory correctly.

The reason that the smaller loop of 400 worked is because shmget creates segments that are multiple of PAGE_SIZE.

So even when you passed sizeof(float), it allocated atleast 1 page which was enough to hold 400 floats but not 40000000.

I hope that clears the confusion.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for comment. But, in this case, I got a problem with getting 'shmid' which resulted from shmget function, after editing the code.
So you get -1 from shmget?
For solving this problem, I had to edit shmget function like shmget(IPC_PRIVATE, sizeof(float)*40000000, ...). I guess that there is a trick relationship between input key and the way of memory managing for kernel.
yes, I got -1 from shmget before I change (key_t)1234 to IPC_PRIVATE
@Gabriel722 I was suspicious of the key 1234 to begin with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.