I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted contents in array x, while the other will use file f2.dat(11999 numbers) and array y. When the threads have finished, I need to merge x and y into array z so that z is also sorted. Also, I am wanting to use one function for both threads, passing the array, its size, and the name of the file in a structure.

Currently, I have a struct set up with the arrays, their sizes, and the name of the two files, a comparison function for qsort, a sort function for the contents of the files, a merge sort for the z array, and of course the creation of the threads.

I think the biggest area I have questions about is how to properly pass a structure to a generalized sort function and then sort data using that structure parameter.

So far:

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

typedef struct Data
{
  int x[10000];
  int size;
  char *filename;
} data_t;


// Comparison function for qsort
int cmpint (int *a, int *b)
{
  if (*a < *b)
     return -1;
  else
     if (*a > *b)
       return 1;
     else
       return 0;
}


// Sort contents of files
// How to generalize this to work with two separate arrays???????
void *sort(void *data)
{
  FILE *fp;
  //long lSize = ftell (fp);
  fp = fopen(filename.x[], "rb");
  fread(x, 10000, size, fp);

  // data_t *my_data = (data_t *) data;

  qsort(x,1000,sizeof(int),cmpint);
  pthread_exit(0);

}


int main()  {

  int i=0,j=0,k=0;
  char z[22000];
  pthread_t px, py;

  pthread_create(&px, NULL, sort, (void *) &data_x);
  pthread_create(&py, NULL, sort, (void *) &data_x);

  pthread_join(px, 0);
  pthread_join (py, 0);

  // Merge x and y into z, sorted
  while (i<1000 || j < 1000)
  {
    if (j == 1000)
       z[k++]=x[i++];

    else
       if (i<1000 && (x[i]<y[j]))
          z[k++]=x[i++];
    else
          z[k++]=y[j++];
  }


}