I am developing a c program which connects to a mysql database reads data from a a table of 4 million of data and writes its data to another 100 tables in another database after some calculations. To make it efficient I tried to use 100 threads to write data for 100 tables and 1 thread to read data from the database and write those in to a buffer. So that the 100 threads would read from the buffers.
But the problem is when I'm making the buffers. I used malloc
to make the buffers. char *** queue;
is declared in the header file so that it is global
int i = 0, j = 0;
queue = (char ***) malloc(100);
int threadRet;
for (i; i < 100; i++) {
queue[i] = (char **) malloc(2000);
for (j; j < 2000; j++) {
queue[i][j] = (char *) malloc(180);
}
}
and My buffer writing thread as I mentioned before exicutes the function void * thrededChunkPicker(void * parr)
sprintf(queue[tableNo][marker[tableNo]], "INSERT INTO usage_summary%s(mobile,`0`,ddate) VALUES('%s',%d,'%s') ON DUPLICATE KEY UPDATE `0` = VALUES(`0`), ddate=VALUES(ddate)", row[0] + 7, row[0], sumOfUsage, row[2]);
(marker[tableNo])++;
This is how I write to the buffer . As I've found out the segmentation fault occurs here.
I needed 100 buffers in each of which contains 2000 string arrays of 180. this code compiled successfully.but when runs it gives a segmentation fault.