problem using semaphores

From: "Raja Agrawal" <raja(dot)agrawal(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: problem using semaphores
Date: 2007-05-08 19:37:29
Message-ID: 14006f560705081237g2362ce43s99a73cc24749c00e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

We are using the following piece of code for updating a list
synchronously i.e. no two threads would update the list at a time. But
the postgres hangs for some reason after a few calls to UpdateList
function. The program (i.e. postgres) does not even react to Ctrl+C or
any other interrupt. We had to kill force fully (i.e. with -9 option)
and restart the postgres.

Later, we found out a similar coding used in backend/port/pg_sema.c
which provides utilities for using semaphores. But these utilities are
not used anywhere. (We have not tried using these utilities but our
code looks similar).

Is the following way of using semaphores not correct in the postgres
environment?

Are we missing something or is there any hack around it?

-regards
Raja Agrawal
Postgraduate Student
Department of Computer Science & Engineering
Indian Institute of Technology Bombay
===================================================
List * UpdateList(List *q, void *req, int isAdd, int semid)
{
struct sembuf operation;
int ret;

/* Acquire the lock with semid */
operation.sem_num = 0; // Which semaphore in the semaphore array
operation.sem_op = -1; // Operation: subtract 1 from semaphore value
operation.sem_flg = 0; // Flag =0 means we will wait
ret = semop(semid, &operation, 1);

if(ret == 0)
{
elog(NOTICE, "Successful P-operation\n");
elog(NOTICE, "Process id is %d\n", getpid());
}
else
{
elog(NOTICE, "semb: P-operation did not succeed.\n");
}

if (isAdd == 1)
{
q = lcons(req, q);
}
else
{
q = list_delete_ptr(q, req);
}

/* Release the lock on ready queue */
operation.sem_num = 0; // Which semaphore in the semaphore array
operation.sem_op = 1; // Operation: add 1 from semaphore value
operation.sem_flg = 0; // Flag =0 means we will wait

/* So do the operation! */
ret = semop(semid, &operation, 1);
if(ret == 0)
{
elog(NOTICE, "Successful V-operation by program sema.\n");
}
else
{
elog(NOTICE, "sema: V-operation did not succeed.\n");
perror("REASON");
}
}
===================================================

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Neil Conway 2007-05-08 19:58:04 Re: problem using semaphores
Previous Message Lukas Kahwe Smith 2007-05-08 19:32:33 Re: Managing the community information stream