Thread: simple shared memory throwing segmentation fault

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    simple shared memory throwing segmentation fault

    this simple code is throwing segmentation fault at run time, Any help will be precious for me...
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <stdlib.h>
    #include <sys/types.h>
    main()
    {
    
            int shmWid, *written;
    
            shmWid = (IPC_PRIVATE, sizeof(int), 0777 | IPC_CREAT);
    
            if (fork() == 0) {
                    written = (int *)shmat(shmWid, 0,0);
                    sleep(1);
    
            }
            else {
                    written = (int *)shmat(shmWid, 0,0);
                    *written = 0;
            }
            wait();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > shmWid = (IPC_PRIVATE, sizeof(int), 0777 | IPC_CREAT);
    Did you actually want to call some shm... function here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    You might want to shmget( ) the shared memory before attaching to it.

    -- p

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    134
    Thank you pete and salem, I just found that, I was really dancing barefoot! I forgot to use "shmget"
    Sorry for my carelessness. And thank you for correcting my stupid mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  2. Help on Segmentation Fault!!!
    By SweeLeen in forum Linux Programming
    Replies: 4
    Last Post: 02-19-2006, 11:33 AM
  3. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 02:29 AM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM