simple shared memory throwing segmentation fault

This is a discussion on simple shared memory throwing segmentation fault within the C Programming forums, part of the General Programming Boards category; this simple code is throwing segmentation fault at run time, Any help will be precious for me... Code: #include <stdio.h> ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:33 AM
  3. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 01: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

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