Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, I have two processes read and write using shared memory.I have to synchronize both these processes so that one occurs after the other after write read occurs and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    7

    Shared memory in Linux


    Hi all,

    I have two processes read and write using shared memory.I have to synchronize both these processes so that one occurs after the other after write read occurs and both are using the common shared memory.

    main()
    {

    Create a semaphore id.
    }

    read()
    {
    sem lock(using sembuf structure)
    Some critical code section
    sem unlock
    }



    write()
    {
    sem lock
    Some critical code section
    sem unlock
    }

    I want to know whether the above approach works fine or it may happen in this approach that one process keeps running and other process remains locked.

    Is there any other way of doing this i.e making one process happen after another and both are using the common shared memory.

    Regards,
    Mayank Agarwal

  2. #2
    Just Joined!
    Join Date
    Jul 2008
    Posts
    3
    I'm no expert but I think it'd be rather easy to have a 'flag' in your shared memory that you can set, signifying the write is complete. So you won't have to worry about synchronizing. After you get your mutex, just make sure the flag is set. If this is going to happen more than once, remember to clear the flag once you've read the data. Something like:

    main()
    {

    Create a semaphore id.
    }

    read()
    {
    sem lock(using sembuf structure)
    Check if 'flag' is set in shared mem

    Some critical code section

    Clear 'flag' in shared mem
    sem unlock
    }



    write()
    {
    sem lock
    Check if 'flag' is clear in shared mem

    Some critical code section

    Set the flag in shared mem
    sem unlock
    }

    Of course you'll have to release the mutex before exiting your function, even if it fails.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    2
    Quote Originally Posted by amayank77 View Post
    Hi all,

    I have two processes read and write using shared memory.I have to synchronize both these processes so that one occurs after the other after write read occurs and both are using the common shared memory.

    main()
    {

    Create a semaphore id.
    }

    read()
    {
    sem lock(using sembuf structure)
    Some critical code section
    sem unlock
    }



    write()
    {
    sem lock
    Some critical code section
    sem unlock
    }

    I want to know whether the above approach works fine or it may happen in this approach that one process keeps running and other process remains locked.

    Is there any other way of doing this i.e making one process happen after another and both are using the common shared memory.

    Regards,
    Mayank Agarwal

    Your approach is fine. Just make sure that you create the semaphores in the proper way. Because unless you create it with proper options, there are possibilities that how many times you use the same semaphores from any number of processes, they will run independently. Better first try out your semaphore with two simple processes. Then you can club it with your shm processes!

    -Giri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •