I have a program that loads a very large avl tree and then opens a shared memory area and waits for requests (with semaphores to order the queueing). The client program connects to the shared memory area on startup but I would like it to check the status of the shared memory before each request. I have had instances where the client program seems to get stuck (for want of a better word) waiting for a reply. I am using Posix shared memory.
The code for connecting to the shared memory area is:
Code:
/* open and map shared memory that server must create */
FILE_DESC = shm_open(SHM_NAME, O_RDWR, 0666);
if ( FILE_DESC < 0 )
{
/* An error occurred, exit function */
TRACE(("SHM Message: Error occurred opening shared memory exiting\n"));
return(0);
}
SHARED_MEM_PTR = mmap(NULL, sizeof(SHM_STRUCT), PROT_READ | PROT_WRITE, MAP_SHARED, FILE_DESC, 0);
if ( SHARED_MEM_PTR < 0 )
{
/* An error occurred, exit function */
TRACE(("SHM Message: Error mapping shared file exiting\n"));
return(0);
}
/* Shared memory successfully attached to, set global variable to true */
SETTINGS.SHARED_MEM = TRUE;
TRACE(("SHM Message: Shared memory successfully created\n"));
/* Now exit successfully */
return(1);
And the code for issueing request is:
Code:
if ((SETTINGS.CDR_WRITE_ICF || SETTINGS.CDR_SHOW_ICF) && SETTINGS.SHARED_MEM)
{
/* Currently we have to assume that shared memory is available and proceeed
* as normal.
*/
/* UL-CR6572 Need to ensure that 11 characters are passed to the search engine and that
* it is left padded with zeros especially for authcodes
*/
sprintf( ptr->BillNumber, "%011s", ptr->billing_number ); /* UL-CR6572 */
sem_wait(&SHARED_MEM_PTR->ControlSem);
strcpy(SHARED_MEM_PTR->SrchCliNum, ptr->BillNumber);
sem_post(&SHARED_MEM_PTR->RequestSem);
sem_wait(&SHARED_MEM_PTR->AnswerSem);
strcat(ptr->AccountNumber, SHARED_MEM_PTR->RtrnAcctNum);
sem_post(&SHARED_MEM_PTR->ControlSem);
}
Thanks