I am trying to implement the odd-even transposition sorting algorithm in c with multi-threading. The program will receive a file with a line of integers that need to be correctly sorted. I have created the array that the program will work with and I am reading in the data correctly. Although, I am not quite sure how to create the threads right. I know that amount of threads I will need will always be N/2. However the N will not always be 20, so I am handling a finite amount right now. That's a limitation though b/c N will not always be 20. It could be higher. My program is only able to handle twenty right now. I'm also having trouble with the sorting part as well even though I have a function named swap already. The start point function is where this all takes place. I am not sure where to begin on this issue as well.
This is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int list[20];
int n = 20;
int param[10];
pthread_t threads[10];
void readLINE(char *filename);
void do_swap(int R1, int R2);
void display();
void *startPoint( void *arg );
void readLINE(char *filename)
{
FILE* file;
int i,j;
file = fopen(filename,"r");
if(file==NULL)
{
printf("Error: can't open file.\n");
}
else
{
printf("File opened successfully.\n");
i = 0;
while(!feof(file))
{
fscanf(file,"%d", &list[i]);
i++;
}
printf("The numbers are: \n");
for(j=0; j< i-1; j++)
{
printf("%d\n", list[j]);
}
fclose(file);
}
n = i-1;
}
//swap positions of arguments in list array
void swap(int R1, int R2)
{
if (list[R1] > list[R1+1])
{
int temp = list[R1];
list[R1] = list[R2];
list[R2] = temp;
}
}
void display()
{
int count;
for (count = 0; count < n; count++)
{
//cout << list[count] << " " ;
printf("%d ",list[count]);
}
//cout << endl;
printf("\n");
}
void *startPoint( void *arg )
{
int R1 = *(int*)arg;
int count;
for (count = 0; count < n/2; count++)
{
}
return 0;
}
int main(int argc, char** argv)
{
pthread_attr_t tattr;
pthread_attr_init (&tattr);
pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
readLINE(argv[1]);
printf("list[] presorted:");
display();
//create n/2 threads to do the sorting algorithm
//the parameter to each thread is an int:
//first thread param is 0
//second thread param is 2
//third thread param is 4 etc....
int count;
for (count = 0; count < n/2; count++)
{
param[count] = count*2;
pthread_create( &threads[ count], NULL, startPoint, (void*) ¶m[count]);
}
//wait for all the reads to finish before exiting the program
//otherwise the process would exit and abort all the threads
for (count = 0; count < n/2; count++)
{
pthread_join(threads[count], NULL);
}
//display the sorted state of the list
printf("list[] after sorting: ");
display();
exit(0);
}