Hi All,
I have built an application on C++. It is a multi threaded application. My application spawns 8 threads when it come up. Each thread opens one file and maps it using mmap.
It is a client-server application. Each thread picks up a message from the queue and processes it and finally using mmap stores it in a file.
Writing to the file and moving the file to another folder is fine. After running for 4 hours, application crashes (Segmentation Fault). When I debugged the core I came to know that SIGSEGV has been caught.
Here are some of the highlights of my codes snippets...
#define [B]NORMAL_CDRFILESIZE[/B] 1000000
if((m_hFile = fopen( m_currentFileName, "a+" ))==NULL)
{
cerr << "File could not be opened" << endl;
return false;
}
int fd = fileno(m_hFile);
if(ftruncate(fd,NORMAL_CDRFILESIZE) != 0)
{
perror("trucate error");
}
mvmem =(char*) mmap(0, NORMAL_CDRFILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // mvmem is declared as char * mvmem
fmem = mvmem;
memcpy(fmem,pMsg,strlen(pMsg)); //pMsg is the buffer containing data to be written to the file
The above code snippet has been called by each thread in the application. I am using mutex lock and unlock when each thread executes the above code.
When I tried to debug the core file using dbx, it is at memcpy, the memory getting corrupted.
I have given a highlight of the code but actual code contains lots other computations.
Please let me know the cause for Segmentation Fault.
Thanks you very much in advance. Any clue will be a great help.