All I have to do is take an array of n integers and run p threads to sort equally divided p blocks of the same and finally merge them. There is occuring some problem in my code. If I run the threads one by one, by using pthread_join's before next thread, its working all good. This is some trivial error but now I cant give it more. Please help.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

void *func_sort(void *ptr);

typedef struct{
    int *arr;
    int n, p, start;
}arg;

main()
{
    pthread_t* th;
    int n, p, *arr, i, j, *fin, *count;
    arg* arg1;
    printf("n=");
    scanf("%d", &n);
    printf("p=");
    scanf("%d", &p);
    arr = (int*) malloc(n*sizeof(int));
    arg1 = (arg*) malloc(sizeof(arg));
    fin = (int*) malloc(n*sizeof(int));
    count = (int*) malloc(p*sizeof(int));
    th = (pthread_t*) malloc(sizeof(pthread_t)*p);

    for(i=0; i<n; i++)
    {
        arr[i] = rand()%1000;
    }
    printf("\nRandom Numbers Generated = %d\n",n);

    for(i=0; i<n; i++)
    {
        printf("%d:%d\n",i,arr[i]);
    }

    arg1->n=n;
    arg1->p=p;
    for(i=0; i<p; i++)
    {   
        count[i] = 0;
        arg1->arr = arr;
        arg1->start = i*(n/p);
        pthread_create(&th[i], NULL, func_sort, (void*)arg1);
    }       

    for(j=0; j<p; j++);
        pthread_join(th[j], NULL);

    printf("\nThreads completed:\n");       

    for(i=0; i<n; i++)
    {
        printf("%d:%d\n",(i/(n/p)),arr[i]);
        if((i+1)%(n/p)==0)
            printf("\n");
    }

//MERGER PART

    int k, min;
    j=0;

    while(j<n)
    {
        for(i=0; i<p; i++)
            if(count[i]<(n/p))
            {
                min = arr[i*(n/p)+count[i]];
                break;
            }

        for(i=0; i<p; i++)
        {
            if(arr[i*(n/p)+count[i]]<=min && count[i]<(n/p))
            {
                min = arr[i*(n/p)+count[i]];
                k = i;
            }
        }
        count[k]++;
        fin[j++] = min;
    }

    printf("Merging completed:\n"); 

    for(i=0; i<n; i++)
    {
        printf("%d\t", fin[i]);
    }
    printf("\n");
    exit(0);
}

void *func_sort(void *ptr)
{
    arg* arg1 = (arg*) ptr;
    int i, j, min, tj;
    for(i=arg1->start; i<(arg1->start + (arg1->n/arg1->p)); i++)
    {
        min = arg1->arr[i];
        tj = i;
        for(j=i; j<(arg1->start+(arg1->n)/(arg1->p)); j++)
        {
            if(arg1->arr[j]<=min)
            {
                min = arg1->arr[j];     
                tj = j;
            }
        }

        arg1->arr[tj] = arg1->arr[i];
        arg1->arr[i] = min;     
    }
}
pthread_create(&th[i], NULL, func_sort, (void*)arg1);

You pass the same argument to all threads. Remember, that threads are running in the common address space, so a single copy of arg1 is shared among them. You need to create distinct individual arguments for each thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.