sem_wait hangs (deadlock)
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > sem_wait hangs (deadlock)




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    sem_wait hangs (deadlock)  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-06 12:21 AM

I'm using named semaphores for interprocess communication. Since I've
deadlock, I'm trying to achieve something very simple (dummy function).
Parent process creates named semaphore with initial count=1:
if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1],
O_CREAT, 0666, 1)) ==
SEM_FAILED) {
perror("sem_open:");
exit(-1);
}

Child process opens this:
if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1], 0))
== SEM_FAILED) {
perror("sem_open:");
exit(-1);
}

(I did a sem_getvalue() and checked names, return values from system
calls to make sure child process is using the right name).

In child process I get deadlock whatever, I do. For e.g. this simple
code hangs on the very 1st iteration of this loop (in the sem_wait()
call as reported by dbx):

for(int in = 0; in < 100; in++) {
if ( sem_wait(filled) < 0 ) {
perror("sem_wait:");
exit(-1);
}
cout << argv[0] << " acqd. lock" << endl;
sem_post(filled);
}

No other process is trying to wait() or post() this semaphore. Can
someone tell what's wrong?
Thanks,






[ Post a follow-up to this message ]



    Re: sem_wait hangs (deadlock)  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-06 12:21 AM


psp wrote:
> I'm using named semaphores for interprocess communication. Since I've
> deadlock, I'm trying to achieve something very simple (dummy function).
> Parent process creates named semaphore with initial count=1:
> if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1],
> O_CREAT, 0666, 1)) ==
>  SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }
>
> Child process opens this:
>     if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1], 0)
)
> == SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }
>
> (I did a sem_getvalue() and checked names, return values from system
> calls to make sure child process is using the right name).
>
> In child process I get deadlock whatever, I do. For e.g. this simple
> code hangs on the very 1st iteration of this loop (in the sem_wait()
> call as reported by dbx):
>
>     for(int in = 0; in < 100; in++) {
>         if ( sem_wait(filled) < 0 ) {
>             perror("sem_wait:");
>             exit(-1);
>         }
>         cout << argv[0] << " acqd. lock" << endl;
>         sem_post(filled);
>     }
>
> No other process is trying to wait() or post() this semaphore. Can
> someone tell what's wrong?
> Thanks,

Upon careful observation I found that out of the 12 semaphores I create
(in parent) and open in the child process, only 1 semaphore (the
"available" sem. below) has the correct initialized value in the child.
I'm posting the code if it helps:
PARENT:
sem_t *daq2lcdx_sem[DAQTOLCDX_COMMSIZE];
sem_t *available, *filled;
// Create named semaphores, so that other processes can connect to
them
for(int i = 0; i < DAQTOLCDX_COMMSIZE; i++) {
if ( (daq2lcdx_sem[i] = sem_open(daq2lcdxSemNames[i],
O_CREAT&O_EXCL, 0666, 1)) =
= SEM_FAILED) {
perror("sem_open:");
exit(-1);
}
}
// Last two are available, filled semaphores
if ( (available = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE],
O_CREAT&O_EXCL, 0666
, DAQTOLCDX_COMMSIZE)) == SEM_FAILED) {
perror("sem_open:");
exit(-1);
}
if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1],
O_CREAT&O_EXCL, 0666,
DAQTOLCDX_COMMSIZE)) == SEM_FAILED) {
perror("sem_open:");
exit(-1);
}

CHILD:
sem_t *daq2lcdx_sem[DAQTOLCDX_COMMSIZE];
sem_t *available, *filled;
for(int i = 0; i < DAQTOLCDX_COMMSIZE; i++) {
if ( (daq2lcdx_sem[i] = sem_open(daq2lcdxSemNames[i], 0)) ==
SEM_FAILED) {
perror("sem_open:");
exit(-1);
}
}
// Last two are available, filled semaphores
if ( (available = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE],
0)) == SEM_FAILED) {
perror("sem_open:");
exit(-1);
}
if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1], 0))
== SEM_FAILED) {
perror("sem_open:");
exit(-1);
}






[ Post a follow-up to this message ]



    Re: sem_wait hangs (deadlock)  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
12-23-06 12:21 AM


psp wrote:
> psp wrote: 
>
> Upon careful observation I found that out of the 12 semaphores I create
> (in parent) and open in the child process, only 1 semaphore (the
> "available" sem. below) has the correct initialized value in the child.
> I'm posting the code if it helps:
> PARENT:
>     sem_t *daq2lcdx_sem[DAQTOLCDX_COMMSIZE];
>     sem_t *available, *filled;
>     // Create named semaphores, so that other processes can connect to
> them
>     for(int i = 0; i < DAQTOLCDX_COMMSIZE; i++) {
>         if ( (daq2lcdx_sem[i] = sem_open(daq2lcdxSemNames[i],
> O_CREAT&O_EXCL, 0666, 1)) =
> = SEM_FAILED) {
>             perror("sem_open:");
>             exit(-1);
>         }
>     }
>     // Last two are available, filled semaphores
>     if ( (available = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE],
> O_CREAT&O_EXCL, 0666
> , DAQTOLCDX_COMMSIZE)) == SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }
>     if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1],
> O_CREAT&O_EXCL, 0666,
>  DAQTOLCDX_COMMSIZE)) == SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }
>
> CHILD:
>     sem_t *daq2lcdx_sem[DAQTOLCDX_COMMSIZE];
>     sem_t *available, *filled;
>     for(int i = 0; i < DAQTOLCDX_COMMSIZE; i++) {
>         if ( (daq2lcdx_sem[i] = sem_open(daq2lcdxSemNames[i], 0)) 
==
> SEM_FAILED) {
>             perror("sem_open:");
>             exit(-1);
>         }
>     }
>     // Last two are available, filled semaphores
>     if ( (available = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE],
> 0)) == SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }
>     if ( (filled = sem_open(daq2lcdxSemNames[DAQTOLCDX_COMMSIZE+1], 0)
)
> == SEM_FAILED) {
>         perror("sem_open:");
>         exit(-1);
>     }

Ok, never mind I figured it out I think. I was creating the semaphores
in the parent with only a sem_open() call. When I initialized them and
declared them to be shared across processes with the sem_init() call I
see the correct values in the child process.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:14 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register