Hello mate,
I'm learning C in Linux and just encountered some issue which I could not solve myself. Here is part my code
Code:
char *wordlist[500000];
int main()
{
FILE *file;
long i=0;
char *tmp = malloc(100);
char *convert = malloc(20);
long count=0;
wordlist[0]=calloc(500000,30*sizeof(char));
for (i=1;i<500000;i++)
wordlist[i]=wordlist[i-1]+30;
strcpy(name,"/grid/grid21dec07/ProcessOutput/out");
for (i=0;i<=1000;i++)
{
strcpy(tmp,name);
sprintf(convert,"%d",i);
strcat(tmp,convert);
strcat(tmp,"/SuccessList.txt");
file = fopen(tmp,"r");
if (file == (FILE *)0)
{
printf("File %s cannot be openned. Program terminated.",tmp);
return -1;
}
while (fgets(wordlist[count],100,file)!=(char *)NULL)
{
count++;
}
fclose(file);
}
free(wordlist[0]);
free(line);
free(tmp);
free(convert);
free(name);
} Basically, my program just read a lot of files (around 50000), each of which contains several lines. The program will read these lines into the string array
wordlist[].
I'm currently using FC7, and gcc as compiler. The problem is when I started running the program, it was so slow that I had to stop it in the middle, review the code to see if there is any infinite loop, then run again. In the second running, I realized that all the parts that it has ran through in the previous time was done in no time, and then slow again.
After several times, the program now run in alomost no time. But if I wait for few hours, or restart the machine, then the slow performance comes back. I look at the
top program all the time, and see that my program only occupy 0.5% of mem and 1% of CPU, and my linux does not run anything heavy at all.
Could anyone suggest me the reason, and possible solution?
Thank you for reading.
SG.