tags:

views:

104

answers:

1

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*) &param[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);
}
+1  A: 

It has been roughly 10 years since I wrote standard C code, so forgive any minor errors. I do think I can help you conceptually.

You are allocating your buffers statically. Instead, determine the sizes involved and dynamically allocate the memory you need. Here's a good reference. Basically determine n as you read in the file, and use malloc to allocate list and param based on that value instead of allocating a fixed array.

What specific problem are you having with the sorting part? Do you get a compiler error, runtime error, incorrect sorting result, ...?

UPDATE:

Here's a discussion of serial and parallel sorting that includes an implementation of parallel odd even transition sorting in C.

Eric J.
I'm not getting any errors with the sorting part, as that function has not been written yet. I was confused about how to use threads to interact with the actual sorting part.
saltinyellow
Updated with a reference that includes an implementation. Suggest you study rather than copy the implementation to maximize learning.
Eric J.
Thank you for the implementation, once I figure how to interact this with the threads. I will possibly have more insight.
saltinyellow