The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help me out with my threaded c++ mudbase - c++, pthread_cond_wait sonicx High Level Programming 4 08-25-2007 02:47 AM
multi threaded program is hanging hikrishn AIX 0 12-15-2006 05:39 AM
multi-threaded server, pthreads, sleep Parahat Melayev High Level Programming 0 03-16-2005 09:38 AM
Threaded 'find' utility mariuca High Level Programming 1 10-27-2004 02:57 PM
Threaded Discussions for Webpages evertk UNIX for Dummies Questions & Answers 4 03-08-2002 07:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-30-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
threaded merge sort help

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 y[12000];
  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++];
  }


}

Last edited by AusTex; 04-30-2005 at 06:07 PM.
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0