I have this code. pthread_create does not through an error message, however it does not execute it's argument function. See comments in code for better description
void * moveFiles (void * arg);
int main (int argc, char **argv)
{
pthread_t * tid;
int rc = 0;
/*variables count and the files array are correctly populated from other parts of the program */
tid = malloc (count);
for (i = 0; i < count; i++) {
/* Function moveFile is never executed */
rc = pthread_create (&tid[i], NULL, moveFile, (void *)files[i]);
if (rc != 0) {
/* This error message is also never executed */
fprintf (stderr, "Could not create thread!\n");
exit (0);
}
}
for (i = 0; i < count; i++) {
/* This causes segfault when it tries to join non-existant threads */
pthread_join (tid[i], NULL);
}
}
void *moveFile (void * arg)
{
char *file = (char *)arg;
int in, out; /* FDs for input and output file */
char buffer [INT_MAX]; /* Being a little liberal with memory here, please don't mark off! */
char backup[512];
strcat(backup, ".backup/");
strcat (backup, file);
strcat (backup, ".bak");
printf ("In thread!!!\n");
printf ("File name is %s\n", file);
printf ("Backup file is %s\n", backup);
if ((in = open(file, O_RDONLY)) < 0) {
printf("Cannot open inputfile!\n");
exit(0);
}
if ((out = open(backup, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IRGRP|S_IROTH)) < 0) {
printf("Cannot open outfile\n");
exit(1);
}
read (in, buffer, INT_MAX);
write (out, buffer, INT_MAX);
close (in);
close (out);
return NULL;
}
Thanks for any help can provide.