0
votes

I have n files and my program needs to merge the contents of all into an one file using threads and temp files (have to use tmpfile()). When creating a thread it has to merge 2 files into a temp file (temp1) then another thread will merge the next 2 files into another temp file (temp2) and so on, then in the next level another thread should merge temp1 with temp2 into another temp file.

alt text

I was thinking in creating an array of file names passing it to pthread_create as argument, and the function should return the array modified too but i can't figure how to get the tempfile name. Will be something like this:

int main(int argc, char *argv[]){

int n = argc -1;

char *files_arr[n];

pthread_t threads[n-1];

}

    for (int i=0; i < argc; i++)

    {
       pthread_create (&threads[i], NULL, temp_merge, (void *) &files_arr);
    }
}//end main

void *temp_merge (void *arg){

    char *myarray[];
    myarray = (char *) arg;
    FILE *f1, *f2, *tf;
    tf = tmpfile();

    //code to merge f1 and f2 into tf, f1 and f2 could be temp files created before

     pthread_exit((void*) myarray); //Do I lose the temp file using pthread_exit? 
}

The question is: How can I access to a temp file openned before with tmp() in a previous thread to generate new temporary files?

2
"i can't figure how to get the tempfile name." - you can't, in general That's the point of tmpfile - The Archetypal Paul
That's right Paul, so how do I "store" these temp files to read them later? - g0d1anier
create an array of saved names. after each step, merge those files into the new files (saving those names as you go). the word 'recursive' comes to mind here ;) - KevinDTimm
if you mean by "later" another run of this or another program: you cannot, because tmpfile() deletes the file after completion; - Peter Miehle
@Kevin the problem is i can't "save" the names of the temp files using tmpfile() and i thought in recursion but i think if i use it its gonna do all in one thread (i need a thread for each merging). @Peter when i say "later" i mean in a next thread at the same program - g0d1anier

2 Answers

1
votes

Could you use tmpnam() instead of tmpfile(), and manually open/close/delete files as necessary?

0
votes

You have the list of files to begin with (I assume), so you'll open all of them (returning FILE *).

In your mergesort function (MSF), tmpfile() returns a FILE * which is what your MSF should return. So, your return value from the MSF becomes one of the two inputs your next invocation of MSF. This continues until you have only one FILE * to act upon.