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