I’m using Kubuntu 12.04, gcc 4.6.3.
If I create a pthread, use fopen64 and then fgets – it segfaults.
Same code replacing fopen64 with fopen – it succeeds.
Without creating pthread – it succeeds.
So why the failure? Here’s the code:
#include <stdio.h>
#include <pthread.h>
typedef struct threadArgs
{
char* argsList;
int argc;
} threadArgs;
void
threadRun(void *pArg);
int
main(int argc, char* argv[])
{
int err = 0;
threadArgs thrArgs;
pthread_t thrd;
if (argc > 1)
{
printf("creating thread \n");
err = pthread_create (&thrd, NULL, (void *) &threadRun, (void *) &thrArgs);
printf("pthread_create returned: %d \n", err);
pthread_join(thrd, NULL);
}
else
{
printf("no thread - just calling func \n");
threadRun((void*)&thrArgs);
}
printf("Exiting main() \n");
return err;
}
void
threadRun(void *pArg)
{
printf("IN the Thread \n");
char* pStr;
FILE *pFile = NULL;
pFile = (FILE*)fopen64("test.txt","r");
//pFile = (FILE*)fopen("test.txt","r");
if (pFile==NULL)
{
printf("pFile is NULL \n");
}
else
{
printf("pFile is NOT null \n");
char line[256];
pStr = fgets(line, sizeof(line),pFile);
if (pStr)
{
printf("line retrieved: %s \n", line);
}
else
{
printf("no line retrieved \n");
}
}
printf("End of pthread run func \n");
return;
}
pthread_create()expectsvoid * (*)(void *)as thread function, but you are passingvoid (*)(void *).Update:
You are missing a prototype for
fopen64(), so the compiler assumesintwhich is not the same asFILE*.Update 1:
To have this prototype available (and with this fix you initial problem) just add:
as first line in your source file.
Additional edit: To be exact:
_LARGEFILE64_SOURCEneeds to#defineed before#includeingstdio.hUpdate 2:
Following the sources I used to make the suxxer work (
main.c):build by:
(no other errors or warnings)
enviroment:
The output (using
main.c‘s source without\ns astest.txt):