[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming.threads

pthread_cond_broadcast with nptl

mailmrg

7/20/2004 7:37:00 PM

i wrote a prog that uses pthread cond broadcast across process
(unrelated), which has cond variables in shared memory. it works in
solaris and i came to know that it should work with NPTL (/lib/tls
library in RHL9.0 in i686 box) in linux. it is not working as
expected.

this is code snipped from prog (i am not pasting the whole code here).
can someone please let me know whats wrong with this code?

##### proc1.c ########
>char* mAddr = (char*)mmap(NULL, 200, PROT_WRITE|PROT_READ,
MAP_SHARED, fd,0);
> mut1 = (pthread_mutex_t*)mAddr;
> cond1 = (pthread_cond_t*)(mAddr+10);
> pthread_mutexattr_init(&attr);
> pthread_condattr_init(&condAttr);
>
> pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
> pthread_condattr_setpshared(&condAttr, PTHREAD_PROCESS_SHARED);
>
> pthread_mutex_init(mut1, &attr);
> pthread_cond_init(cond1, &condAttr);
>
> pthread_mutexattr_destroy(&attr);
> pthread_condattr_destroy(&condAttr);
>
>
> pid_t childPid;
> childPid = vfork();
> if( childPid == 0 )
> {
> if( 0 != execl("proc2", NULL) )
> {
> printf("exec failed\b");
> return 3;
> }
> exit(0);
> }
> sleep(15);
>
> pthread_mutex_lock(mut1);
> pthread_cond_broadcast(cond1);
> printf("proc1 sent event\n");
>
> pthread_mutex_unlock(mut1);

### proc2.c ######

>char* mAddr = (char*)mmap(NULL, 200, PROT_WRITE|PROT_READ,
MAP_SHARED, fd,0);
> pthread_mutex_t *mut1;
> pthread_cond_t *cond1;
>
> mut1 = (pthread_mutex_t*)mAddr;
> cond1 = (pthread_cond_t*)(mAddr+10);
>
> pthread_mutex_lock(mut1);
>
> printf("proc2 waiting on event\n");
> pthread_cond_wait(cond1,mut1);
>
> pthread_mutex_unlock(mut1);
> printf("condition reaached\n");


as u can see, proc1 sends broadcast to proc2 but proc2 doesnt seem to
get the signal at all. so the whole code waits for ever.

thanks,
Ganesh.M.Ramaswamy
3 Answers

Joe Seigh

7/20/2004 8:08:00 PM

0



Ganesh Ramaswamy wrote:
(snip)
> as u can see, proc1 sends broadcast to proc2 but proc2 doesnt seem to
> get the signal at all. so the whole code waits for ever.
>

No, it got the signal, just not when you thought it would. You assumed
the wait would occur before the signal. What happens if the signal is
sent before the wait? You assumed incorrectly as your program correctly
demonstrated. You need to get a good book on thread programming and learn
the techniques for using condition variables.

Joe Seigh

ptjm

7/21/2004 4:51:00 AM

0

In article <9c3ceb1e.0407201136.25ce0e06@posting.google.com>,
Ganesh Ramaswamy <mailmrg@yahoo.com> wrote:

% i wrote a prog that uses pthread cond broadcast across process
% (unrelated), which has cond variables in shared memory. it works in
% solaris and i came to know that it should work with NPTL (/lib/tls

It's only by chance that your program works anywhere. Condition variables
are purely a signalling mechanism. They don't maintain state, so if you
don't happen to be in a condition wait at the time the condition is
signalled, the broadcast won't necessarily have any effect. The other
thing you should know is that CVs are allowed to return from a wait
without being signalled.

% >char* mAddr = (char*)mmap(NULL, 200, PROT_WRITE|PROT_READ,
% MAP_SHARED, fd,0);
% > mut1 = (pthread_mutex_t*)mAddr;
% > cond1 = (pthread_cond_t*)(mAddr+10);

The other thing you should know is that it's not safe to set up pointers
like this. Try creating a structure:

struct mysem {
pthread_mutex_t mux;
pthread_cond_t cond;
int signalstate;
} * mysem = (struct mysem *)mmap(NULL, sizeof(*mysem)+whatever, ...);

% > pthread_mutexattr_init(&attr);
% > pthread_condattr_init(&condAttr);
% >
%
% > pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
% > pthread_condattr_setpshared(&condAttr, PTHREAD_PROCESS_SHARED);
% >
%
pthread_mutex_init(&mysem->mux, &attr);
pthread_cond_init(&mysem->cond, &condAttr);
mysem->signalstate = 0; /* not signalled */

[...]

pthread_mutex_lock(mysem->mux);
/* you must do something here */
mysem->signalstate = 1;
pthread_cond_broadcast(mysem->cond);
/* point of style -- don't do expensive things like calling
printf while holding a mutex, unless you must, which in this
case you needn't */
% > printf("proc1 sent event\n");
% >
%
pthread_mutex_unlock(mysem->mux);

[...]

/* use the same structure definition to set mysem */

pthread_mutex_lock(mysem->mux);
% >
%
/* now test the signalstate until it changes to the value you crave */
while (!mysem->signalstate) {
% > printf("proc2 waiting on event\n");
pthread_cond_wait(mysem->cond,mysem->mux);
/* it doesn't hurt to check the return code here, either */
}
% >
%
pthread_mutex_unlock(mysem->mux);
% > printf("condition reaached\n");

--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com

David Eng

7/22/2004 4:40:00 PM

0


"Ganesh Ramaswamy" <mailmrg@yahoo.com> wrote in message
news:9c3ceb1e.0407201136.25ce0e06@posting.google.com...
> i wrote a prog that uses pthread cond broadcast across process
> (unrelated), which has cond variables in shared memory. it works in
> solaris and i came to know that it should work with NPTL (/lib/tls
> library in RHL9.0 in i686 box) in linux. it is not working as
> expected.
>
> this is code snipped from prog (i am not pasting the whole code here).
> can someone please let me know whats wrong with this code?
>
> ##### proc1.c ########
> >char* mAddr = (char*)mmap(NULL, 200, PROT_WRITE|PROT_READ,
> MAP_SHARED, fd,0);
> > mut1 = (pthread_mutex_t*)mAddr;
> > cond1 = (pthread_cond_t*)(mAddr+10);
> > pthread_mutexattr_init(&attr);
> > pthread_condattr_init(&condAttr);
> >
> > pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
> > pthread_condattr_setpshared(&condAttr, PTHREAD_PROCESS_SHARED);
> >
> > pthread_mutex_init(mut1, &attr);
> > pthread_cond_init(cond1, &condAttr);
> >
> > pthread_mutexattr_destroy(&attr);
> > pthread_condattr_destroy(&condAttr);
> >
> >
> > pid_t childPid;
> > childPid = vfork();
> > if( childPid == 0 )
> > {
> > if( 0 != execl("proc2", NULL) )
> > {
> > printf("exec failed\b");
> > return 3;
> > }
> > exit(0);
> > }
> > sleep(15);
> >
> > pthread_mutex_lock(mut1);
> > pthread_cond_broadcast(cond1);
> > printf("proc1 sent event\n");
> >
> > pthread_mutex_unlock(mut1);
>
> ### proc2.c ######
>
> >char* mAddr = (char*)mmap(NULL, 200, PROT_WRITE|PROT_READ,
> MAP_SHARED, fd,0);
> > pthread_mutex_t *mut1;
> > pthread_cond_t *cond1;
> >
> > mut1 = (pthread_mutex_t*)mAddr;
> > cond1 = (pthread_cond_t*)(mAddr+10);
> >
> > pthread_mutex_lock(mut1);
> >
> > printf("proc2 waiting on event\n");
> > pthread_cond_wait(cond1,mut1);
> >
> > pthread_mutex_unlock(mut1);
> > printf("condition reaached\n");
>
>
> as u can see, proc1 sends broadcast to proc2 but proc2 doesnt seem to
> get the signal at all. so the whole code waits for ever.
>
> thanks,
> Ganesh.M.Ramaswamy