From: Vandana on
Hello All,

I am using fork () command to create 2 processes and using shared
memory to communicate between the processes. I have couple of
questions.

1. When either of the process access the shared memory, do I need to
have a mutex around the whole shared memory? If yes, then how do I
create a mutex that can be seen by both process, do I have to declare
it in the shared memory?

for ex:
struct shared {

int data;
char *something;
pthread_spinlock_t shared_lock; }

can i use this spinlock, so that each process acquires the lock then
writes . reads to the shared memory? Is this correct?

2. How do I make the child process wait on a condition that is set by
the parent. ie, I want the child process to do nothing until the
condition set by the parent is reset again by the parent.

Thanks,
Vadana
From: David Schwartz on
On Feb 26, 1:06 pm, Vandana <nair...(a)gmail.com> wrote:

>     I am using fork () command to create 2 processes and using shared
> memory to communicate between the processes. I have couple of
> questions.

> 1. When either of the process access the shared memory, do I need to
> have a mutex around the whole shared memory?

You probably need a mutex if one thread might modify data while
another thread is reading or modifying it.

> If yes, then how do I
> create a mutex that can be seen by both process, do I have to declare
> it in the shared memory?

Yes. See 'pthread_mutexattr_setpshared' too.

> for ex:
> struct shared {
>
>    int data;
>    char *something;
>    pthread_spinlock_t shared_lock;   }
>
> can i use this spinlock, so that each process acquires the lock then
> writes . reads to the shared memory? Is this correct?

I don't think you can create a process shared spinlock. I believe you
have to use a mutex.

> 2. How do I make the child process wait on a condition that is set by
> the parent. ie, I want the child process to do nothing until the
> condition set by the parent is reset again by the parent.

The same way you would do it with two threads. Acquire a mutex, check
a shared variable. If it's not set to allow forward progress, call
pthread_cond_wait. Then check the variable again. Once it's set to
allow forward progress, release the mutex, if appropriate.

DS
From: ~Glynne on
On Feb 26, 2:06 pm, Vandana <nair...(a)gmail.com> wrote:
> Hello All,
>
>     I am using fork () command to create 2 processes and using shared
> memory to communicate between the processes. I have couple of
> questions.
>
> 1. When either of the process access the shared memory, do I need to
> have a mutex around the whole shared memory? If yes, then how do I
> create a mutex that can be seen by both process, do I have to declare
> it in the shared memory?
>
> for ex:
> struct shared {
>
>    int data;
>    char *something;
>    pthread_spinlock_t shared_lock;   }
>
> can i use this spinlock, so that each process acquires the lock then
> writes . reads to the shared memory? Is this correct?
>
> 2. How do I make the child process wait on a condition that is set by
> the parent. ie, I want the child process to do nothing until the
> condition set by the parent is reset again by the parent.
>
> Thanks,
> Vadana


Since you're talking about separate *processes* (not threads), you
could use something much simpler:

// create lock file
int h= fileno( tmpfile() );
lseek( h, 0, SEEK_SET );


// usage
lockf( h, F_LOCK, 0 );
//
// do shared memory updates here
//
lockf( h, F_ULOCK, 0 );

From: ~Glynne on
On Feb 26, 7:01 pm, "~Glynne" <glynnec2...(a)yahoo.com> wrote:
> On Feb 26, 2:06 pm, Vandana <nair...(a)gmail.com> wrote:
>
>
>
> > Hello All,
>
> >     I am using fork () command to create 2 processes and using shared
> > memory to communicate between the processes. I have couple of
> > questions.
>
> > 1. When either of the process access the shared memory, do I need to
> > have a mutex around the whole shared memory? If yes, then how do I
> > create a mutex that can be seen by both process, do I have to declare
> > it in the shared memory?
>
> > for ex:
> > struct shared {
>
> >    int data;
> >    char *something;
> >    pthread_spinlock_t shared_lock;   }
>
> > can i use this spinlock, so that each process acquires the lock then
> > writes . reads to the shared memory? Is this correct?
>
> > 2. How do I make the child process wait on a condition that is set by
> > the parent. ie, I want the child process to do nothing until the
> > condition set by the parent is reset again by the parent.
>
> > Thanks,
> > Vadana
>
> Since you're talking about separate *processes*  (not threads),  you
> could use something much simpler:
>
> // create lock file
>    int h= fileno( tmpfile() );
>    lseek( h, 0, SEEK_SET );
>
> // usage
>    lockf( h,  F_LOCK, 0 );
>       //
>       // do shared memory updates here
>       //
>    lockf( h, F_ULOCK, 0 );

Obviously you should create the lock file *before* forking ;-)

From: David Schwartz on
On Feb 26, 6:01 pm, "~Glynne" <glynnec2...(a)yahoo.com> wrote:

> Since you're talking about separate *processes*  (not threads),  you
> could use something much simpler:
>
> // create lock file
>    int h= fileno( tmpfile() );
>    lseek( h, 0, SEEK_SET );
>
> // usage
>    lockf( h,  F_LOCK, 0 );
>       //
>       // do shared memory updates here
>       //
>    lockf( h, F_ULOCK, 0 );

What keeps the memory accesses from migrating around the 'lockf'
calls? Suppose his CPU has a write posting buffer and that posting
buffer holds a write just before the unlock operation and doesn't
commit it to memory/cache until after the unlock completes?

DS