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.

Most likely, it is dying at line 32. You have to allocate writable memory for the whole backup filename.

It is dying not in that function; I before had a print statement which was at the very top of that function and it did not execute; the function is never called.

I should also mention I have had print statements after the call pthread_create which execute.

But, in the interest of good faith (and because it would need to be addressed) I fixed the memory problem.

> a print statement did not execute means practically nothing. Load it in gdb, set a breakpoint at the moveFile, and run. This is the only way to know for sure what is going on.
I would do it, but I am away from my linux box.

> a print statement did not execute means practically nothing. Load it in gdb, set a breakpoint at the moveFile, and run. This is the only way to know for sure what is going on.
I would do it, but I am away from my linux box.

In addition to what nezachem stated, it looks like you are also simply exploding the stack with your buffer declaration

char buffer [INT_MAX];

> a print statement did not execute means practically nothing. Load it in gdb, set a breakpoint at the moveFile, and run. This is the only way to know for sure what is going on.
I would do it, but I am away from my linux box.

In this instance it does. And yes, I HAVE run it through gdb. The function moveFile is NEVER executed. The seg fault IS at the pthread_join command.

I do appreciate that you are trying to help, but I have no time to check things I know are not wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.