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-operationby program sema.\n"); } else { elog(NOTICE, "sema: V-operation did not succeed.\n");
perror("REASON"); }
}
===================================================