Segment Fault with shared memory set using mmap

This is a discussion on Segment Fault with shared memory set using mmap within the C Programming forums, part of the General Programming Boards category; Hi, My code is segment faulting on line 50 (*ptr = 10) I can't understand why, could someone explain ? ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Segment Fault with shared memory set using mmap

    Hi,

    My code is segment faulting on line 50 (*ptr = 10)

    I can't understand why, could someone explain ?

    Code:
    (Headers here)
    
    int main()
    {
    
        // Close the shared memory we use just to be safe
        shm_unlink("/shared_mem");
        // Set up some shared memory where we can record the total amount of bytes sent by all processes
        int shmfd;
        
        if( (shmfd = shm_open("/shared_mem", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
        {
        	fprintf(stderr, "Error opening shared memory in recordTotalBytes() \n");
    		exit(EXIT_FAILURE);
        }
        
        // Set the size of the shared memory to 1 long
        ftruncate(shmfd, sizeof(long) *1 );
        // Map the shared memory into our address space
        long *ptr;
        ptr = mmap(NULL, sizeof(long) *1, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); 
    
    	// Create a mutex and initalise it
    	pthread_mutex_t * muxlock;
    	pthread_mutex_init(&muxlock, NULL);
    	
    	// Lock the mutex some work is going to happen with shared resources
    	pthread_mutex_lock(&muxlock);
    	
    	
    	
    	
    	// Set ptr to 10
    	ptr* = 10;
    	
    	printf("%ld", *ptr);
    	// Unlock the mutex
    	pthread_mutex_unlock(&muxlock);
    
    	
    
    }

    Thanks !

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep that's going to happen... because ptr is already a pointer so no need for the * and 10 is almost certain to be an OS reserved address.

    Oddly enough you had it right in your question above... *ptr = 10;... will set the value at the address in ptr to 10.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Well, for one thing, I guess you should be checking the return value of mmap() to see if it's NULL. My guess it the call is failing. I'm also guessing that "ptr* = 10" is just a copy/paste error.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Hi

    Thanks for the replies

    I changed it to

    *ptr = 10

    and it still segfaults


    Also I added a check to see if ptr is null and its not

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Did you check ptr against MAP_FAILED?
    Upon successful completion, the mmap() function shall return the address at which the mapping was placed ( pa); otherwise, it shall return a value of MAP_FAILED and set errno to indicate the error. The symbol MAP_FAILED is defined in the <sys/mman.h> header. No successful return from mmap() shall return the value MAP_FAILED.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Just checked it against MAP_FAILED

    And it IS failing.

    Any idea why ?


    Thanks

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Well, I know one way you can find out. Read what I quoted previously again:
    otherwise, it shall return a value of MAP_FAILED and set errno to indicate the error.
    Looks like you can check errno to find out.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    The errno is 0

    Is that the first entry under errors on this page?

    mmap

    [EACCES]
    The fildes argument is not open for read, regardless of the protection specified, or fildes is not open for write and PROT_WRITE was specified for a MAP_SHARED type mapping.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    errno 0 means "no error," you probably aren't checking it in the right place.

    EDIT: Also, doesn't the length parameter have to be a multiple of the page size? You can't map a single long.
    Last edited by brewbuck; 11-18-2011 at 12:58 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    I have

    Code:
    	
    if( ptr = MAP_FAILED )
    {
    	fprintf(stderr, "Error setting shared memory in recordTotalBytes() error is %d \n ", errno);
    	exit(EXIT_FAILURE);
    }
    Is this the right way to do it ?


    Thanks

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Ugh. You are assigning instead of comparing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Sorry, stupid mistake.

    mmap isn't returning MAP_FAILED

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Fixed it !

    I wasn't allocating any memory for the mutex lock and was passing in &muxlock when it should have just been muxlock


    Not sure why it waited until line 50 to segfault though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-05-2011, 05:11 PM
  2. simple shared memory throwing segmentation fault
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 10-03-2010, 06:53 AM
  3. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  4. BSD mmap for shared memory - am I right?
    By sean in forum Linux Programming
    Replies: 21
    Last Post: 03-09-2009, 01:57 PM
  5. Destroy a shared memory segment
    By g_p in forum C Programming
    Replies: 4
    Last Post: 12-03-2006, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21