3

I am trying to achieve IPC between two process (Process1 and Process2) using shared memory that too without wasting cpu while waiting for its turn. It's basically use of condition variable in multiple process scenario.

Although I am able to do it in parent-child scenario, but not able to do two separate process (process1 & process2) scenario.

Here is what I have done in parent-child scenario.

****Created mptr (shared mutex) and cvptr ( condition variable) using PTHREAD_PROCESS_SHARED flag,****

pthread_cond_t *cvptr;    //Condition Variable Pointer
pthread_condattr_t cattr; //Condition Variable Attribute
pthread_mutex_t    *mptr; //Mutex Pointer
pthread_mutexattr_t matr; //Mutex Attribute

int   shared_mem_id;      //shared memory Id
int   *mp_shared_mem_ptr; //shared memory ptr -- pointing to mutex
int   *cv_shared_mem_ptr; //shared memory ptr -- pointing to condition variable



   // Setup Mutex
rtn = pthread_mutexattr_init(&matr);
rtn = pthread_mutexattr_setpshared(&matr,PTHREAD_PROCESS_SHARED);
rtn = pthread_mutex_init(mptr, &matr);


 //Setup Condition Variable
    rtn = pthread_condattr_init(&cattr);
    pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED));
    pthread_cond_init(cvptr, &cattr);

send signal from parent process

sleep(2);
    pthread_cond_signal( cvptr ); // send signal to child process

and waiting for signal in child process.

pthread_cond_wait( cvptr, mptr );

as per property of condition variable while child waiting for its turn , it is not wasting any CPU cycle.


To make sure, parent must send signal only after child starts waiting for signal, sleep(2) is added before pthread_cond_signal( cvptr );


While I am using same concept in two process scenario , process2 is not getting any signal from process1.

Process2 is waiting infinitely. In seems signal is lost.

I first run process2, then run process1 to make sure process1 signal must be delivered only after when process2 starts waiting.

Where I am making mistake ?

I am using ubuntu12.04, and gcc. Any help to achieve the same will highly appreciated. Thanks in advance.

Note : I am able to do IPC among multiple process using simple shared memory concept, where initially shared_variable value =0 and process1 makes it to 1 to notify process2 to starts its work and after that process2 changes the shared_variable =0 and this continues. This solution actually waste cpu while waiting for updated value of shared variable to continue its working. like while (shared_stuff->written_by_you == 1) {do nothing;}

0

1 Answer 1

1

As it's not obvious from the code you show let me state this:

You ought to place the mutex and the condition variable inside the shared memory, to make them available to anybody via the shared memory. Placing a pointer to them in shared memory is not enough.

Sign up to request clarification or add additional context in comments.

1 Comment

"Placing a pointer to them in shared memory is not enough", most likely not even valid as the processes have different address spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.