Thread: segmentation faults..Please HELP

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    segmentation faults..Please HELP

    ​We have a target system where we are trying to add shared memory using pthread mutex functions. We created a separate library that constructs and destructs memory and a set of functions defined which accesses shared memory. These functions are called from a daemon and other programs. Here is the following code

    Code:
    MemMan::MemMan()
    {
            lock();
    
    
            key_t key = ftok("/lxi/pil/shmem.file", 133);
    
    
            _created = false;
            _size = sizeof(struct mydata);
            mem_id = shmget(key, _size, 0666);                              
    // Attempt to open existing shared memory
    
                                                                            
    // with size to hold my structure
                                                                            
    // and r/w access for all
            if (mem_id != -1)
            {
                     if (errno == ENOENT)
                    {
                            mem_id = shmget(key, _size, IPC_CREAT | 0666);                     
                            // Attempt to create shared memory
    
    
                            if (mem_id == -1) 
                            {
                                    printf("Failed to get memory\n");
                                    return;
                            }
    
    
                    }
                    else
                    {
                            printf("Error creating shared mem\n");
                            return ;
                    }
                    _created = true;
            }
           _mydata = (struct mydata*)shmat(mem_id, NULL, 0);                    
            // Attach it
            mutex = &_mydata->mutex;
            file_mutex = &_mydata->file_mutex;
             pthread_mutex_init(mutex, NULL);                        // Only init content if this is first instance
                    pthread_mutex_init(file_mutex, NULL);                   // Only init content if this is first instance
                    _mydata->Flag = 0;
            }
            _created = true;
            unlock();
    }
    
    
    MemMan::~MemMan()
    {
    
    
            struct shmid_ds stats;
            shmctl(mem_id, IPC_STAT, &stats);
    
    
            shmdt((void*)_mydata);
            if( stats.shm_nattch <= 1)
            {
                    shmctl(mem_id, IPC_RMID, NULL);
            }
            
    }
    
    
    
            // Note: IPC_RMID on every destruct was causing problems, new app created new share !
            // The above should do the IPC_RMID only on last destruct
    }
    
    
    int MemMan::stats(struct shmid_ds *stats)
    {
            return shmctl(mem_id, IPC_STAT, stats);
    }
    
    
    int MemMan::lock()
    {
            return pthread_mutex_lock(mutex);
    }
    
    
    int MemMan::unlock()
    {
            int result = pthread_mutex_unlock(mutex);
            usleep(10000);
            return result;                                                  // This stops mutex hogging by any one app
    }
    int MemMan::file_lock()
    {
            pthread_mutex_lock(file_mutex);
    }
    
    
    int MemMan::file_unlock()
    {
            pthread_mutex_unlock(file_mutex);
            usleep(10000);                                                  // This stops mutex hogging by any one app
    }
    
    
    int MemMan::NumberOfClients()
    {
            struct shmid_ds stats;
            shmctl(mem_id, IPC_STAT, &stats);
            return stats.shm_nattch;
    }



    The functions are then defined like the following

    Code:
       int UTIL_GetFlag()      {
                    if (mymem.Valid())
                    {
                            mymem.lock();
                            struct mydata *data = (struct mydata*)mymem.GetAddress();
                            mymem.unlock();
                            return data->Flag;
                    }
                    else
                    {
                            return -1;
                    }
            }
    The function (similar to one above) are accessing shared memory using daemon and other programs. The problem that we are experiencing are generally segmentation faults and target machine locking up. Since there is no debug capabilities on the target machine , its really difficult to step through the code. Its my first time with shared memory so I am not sure where I am going wrong. Can you please help?
    Last edited by Noman Hussain; 10-15-2012 at 06:44 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    On a very quick look only, it strikes me you are assuming that all operations succeed, but you really need to check rather than assuming - there are plenty of potential causes of those operations failing.

    For example, you are assuming - and not checking - that shmat() returns a valid pointer. IIRC, however, shmat() returns (void *)(-1) if it fails (which is not a NULL pointer).

    Your code where the "function are then defined" is implicitly assuming that mymem.Valid() being true guarantees that mymem.GetAddress() is a valid pointer. If that is not true ....

    It is often not a good idea to initialise a mutex in shared memory. The whole point of pthread functions is to provide a defined interface to manage mutexes and other system-wide objects. By putting such objects in shared memory, you are allowing code to access memory associated with such objects while bypassing that interface. The information you've given is insufficient to conclude you've done that but, equally, is insufficient to exclude the possibility.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    A few comments:
    1. To debug segfaults, run the application through valgrind. Simply install it, and prepend "valgrind" to the command, and voila, you know where it crashed and why.
    2. The usleep doesn't make any sense...
    3. Locking as you are doing there is dangerous. Return a class that automatically releases the mutex when it's destructed. Imagine an exception occurs somewhere between lock() and unlock() - the lock won't ever be released. Unless you create a mess of try-catch blocks of course...

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    To debug segfaults, run the application through valgrind. Simply install it, and prepend "valgrind" to the command, and voila, you know where it crashed and why.
    Did you mean the debugger (probably gdb), rather than valgrind? That would usually be the first choice.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Thanks for the reply, We are using a stripped version of Linux which doesnt have a gdb, valgrind and voila.
    Last edited by Noman Hussain; 10-17-2012 at 07:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation faults
    By C_Enthuaist in forum C Programming
    Replies: 4
    Last Post: 06-17-2010, 06:47 AM
  2. segmentation faults
    By movie55 in forum C Programming
    Replies: 8
    Last Post: 09-22-2009, 05:31 AM
  3. Segmentation faults out the ****
    By Ubber_C_Noob in forum C Programming
    Replies: 26
    Last Post: 09-11-2005, 10:25 AM
  4. segmentation faults?
    By salsa in forum C Programming
    Replies: 4
    Last Post: 10-08-2004, 09:11 AM
  5. Segmentation faults!
    By Chris Gat in forum C Programming
    Replies: 5
    Last Post: 07-15-2002, 04:54 PM