Unix Programming - sem_wait hangs (deadlock)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2006 > sem_wait hangs (deadlock)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sem_wait hangs (deadlock)
psp

2006-12-22, 7:21 pm

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,

psp

2006-12-22, 7:21 pm


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);
}

psp

2006-12-22, 7:21 pm


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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com