Shared Memory with Mutex

This is a discussion on Shared Memory with Mutex within the C++ Programming forums, part of the General Programming Boards category; Hi, I am happy to be with this forum for the first time and so let me say "hi" to ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    Shared Memory with Mutex

    Hi,

    I am happy to be with this forum for the first time and so let me say "hi" to every body.Trying the Mutex between shared process is new to me.

    Now i am trying to write simple socket IPC mechanism on Linux using server and client.Server will create shared memory with mutex and buffer and same mutex initialized with flag PTHERAD_SHARED_PROCESS and different clients will access this shared memory and update their message.

    When i run server it is able to create the shared memory and mutex initialization everything is cool but when i run different clients they try to attach this shared memory and acquire the lock, all of them were able to quire the lock.

    so it looks like mutex is not shared among process.

    Please suggest me what am I missing.

    Code:
    Code Snippet On Server Side Done:
      
     key_t key = 5678;
    
        if ((shmid = shmget(key, 27, IPC_CREAT | 0666)) < 0) 
        {
            std::cout<< "Failed to create shared Memory::"<<strerror( errno )<<std::endl;
            exit(1);
        }
        if ((lockptr = (shLock*)shmat(shmid, NULL, 0)) == (shLock *) -1) 
        {
            std::cout<< "Failed to attach shared Memory::"<<strerror( errno )<<std::endl;
            exit(1);
        }
        
        memset( lockptr->buffer,0,sizeof( lockptr->buffer) );
        char d[] = "REDYYYYYYYYYYYYYYYYYYYYYYYYYY";
        memcpy( lockptr->buffer,d,strlen( d ) );
       
        if( pthread_mutexattr_init( &lockptr->mutexAttr ) == 0 )
            std::cout<< "shared mutex attr intialized"<<std::endl;
    
    
        if( pthread_mutexattr_setpshared( &lockptr->mutexAttr,PTHREAD_PROCESS_SHARED) == 0 )
            std::cout<< "shared mutexattribute set"<<std::endl;
    
    
        if( pthread_mutex_init( &lockptr->mutex, &lockptr->mutexAttr) == 0 )
            std::cout<< "shared mutex intialized"<<std::endl;
    
    Clent SIde Code:
    
      key_t key = 5678;
    
    
        if ((shmid = shmget(key, 27, 0666)) < 0) 
        {
            std::cout<< "Failed to create shared Memory::"<<strerror( errno )<<std::endl;
            exit(1);
        }
        
        if ((lockptr = (shLock*)shmat(shmid, NULL, 0)) == (shLock *) -1) 
        {
            std::cout<< "Failed to attach shared Memory::"<<strerror( errno )<<std::endl;
            exit(1);
        }  
        
         pthread_mutex_lock( &lockptr->mutex );
            std::cout<< "Enter the data to send "<<std::endl;
    
    // NOTE: i am not releasing the lock for test purpose only to make sure it is working correctly when different clients try to acquire same

    Thanks In Advance
    Brahma

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Add error checking to pthread_mutex_lock call, and anywhere else that doesn't have it.

    Instead of "27", use something like "sizeof(shLock)".

    gg

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What does shLock look like?

    You don't need to memset() the shared memory to 0; it comes that way.

    REDYYYYYYYYYYYYYYYYYYYYYYYYYY
    is 29 bytes long (not counting the nul) but you've only asked for 27 in shmget().
    Try
    Code:
    strncpy(lockptr->buffer, "READYYYYYYYYY", sizeof lockptr->buffer);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by Codeplug View Post
    Add error checking to pthread_mutex_lock call, and anywhere else that doesn't have it.

    Instead of "27", use something like "sizeof(shLock)".

    gg
    Hi All,

    Thanks for your quick reply.

    I was under assumtion that when shared meory size mentioned as 27 will be multiple of PAGE_SIZEi .e 27 * PAGE_SIZE. or it may be rounded to PAGE_SIZE.

    Now if I consider that my shmget() call creates only 27 bytes of ahared memory and I added data of more than 27 i.e 71 bytes bytes into my shared buffer on server side tha same data client is able to see with any data loss i.e 71 bytes.Why am i getting this much data on client side ?

    I updated the memory segment in bytes from 27 to 1024 on server side and then on client side as zero .As you know that if you want to refer to existing shared memory we can put this size as zero,this provides plenty of memory thanks for suggestions

    I have added error check for mutex lock but for every lock i am getting success only i.e Zero

    NO success on shared locking,still the problem exists

    Code:
     
    typedef struct shLock
    {
        pthread_mutex_t mutex;
        pthread_mutexattr_t mutexAttr;
        char buffer[100];
    }shLock;
    
     On Server Side:
    if ((shmid = shmget(key, 1024, IPC_CREAT | 0666)) < 0) 
    
    On Client Side:
    
    if( pthread_mutex_lock( &lockptr->mutex ) == 0 )
    std::cout<< "Lock Aquired "<<std::endl;
    
     if( pthread_mutex_lock( &lockptr->mutex ) == 0 )
    std::cout<< "Lock Aquired "<<std::endl;
    Thanks In Advance
    Brahmam

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Hi All,

    Thanks.I am able to find out why shared locking is not working with different client,because of lpthread loading missing with clients( assumed handled in make file but missed i stupid).
    But still i am looking for other memory issues as we are discussing in above threads.

    Thanks in advance
    Brahmam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. shared memory & mutex
    By lollobrigido in forum Linux Programming
    Replies: 3
    Last Post: 02-22-2008, 03:34 PM
  3. ICP shared memory
    By cavemandave in forum C Programming
    Replies: 1
    Last Post: 11-20-2007, 05:08 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. using shared memory in c
    By flawildcat in forum C Programming
    Replies: 1
    Last Post: 04-09-2002, 12:25 PM

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