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 ]
|