Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, When I copy the code below that is copyable and post this into comiplers

ID: 3592134 • Letter: H

Question

Hello, When I copy the code below that is copyable and post this into comiplers these errors come up.

$gcc -o main *.c /tmp/ccT7JzVP.o: In function `main': main.c:(.text+0x4b): undefined reference to `pthread_create' main.c:(.text+0x8c): undefined reference to `pthread_create' main.c:(.text+0xac): undefined reference to `pthread_join' main.c:(.text+0xf7): undefined reference to `pthread_create' main.c:(.text+0x108): undefined reference to `pthread_join' collect2: error: ld returned 1 exit status

Is there a way you can take the copy-able code and compile it and fix these errors and post the working solution to this question?

https://www.chegg.com/homework-help/questions-and-answers/write-multithreaded-sorting-program-c-works-follows-list-integers-divided-two-smaller-list-q5125010

Copyable code:

Code:

//Sort a list of numbers using two separate threads

//by sorting half of each list separately then

//recombining the lists

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#define SIZE 10

#define NUMBER_OF_THREADS 3

void *sorter(void *params);    /* thread that performs basic sorting algorithm */

void *merger(void *params);    /* thread that performs merging of results */

int list[SIZE] = {7,1,8,3,4,2,6,9,12,15};

int result[SIZE];

typedef struct

{

    int from_index;

    int to_index;

} parameters;

int main (int argc, const char * argv[])

{

    int i;

    pthread_t workers[NUMBER_OF_THREADS];

    /* establish the first sorting thread */

    parameters *data = (parameters *) malloc (sizeof(parameters));

    data->from_index = 0;

    data->to_index = (SIZE/2) - 1;

    pthread_create(&workers[0], 0, sorter, data);

    /* establish the second sorting thread */

    data = (parameters *) malloc (sizeof(parameters));

    data->from_index = (SIZE/2);

    data->to_index = SIZE - 1;

    pthread_create(&workers[1], 0, sorter, data);

    /* now wait for the 2 sorting threads to finish */

    for (i = 0; i < NUMBER_OF_THREADS - 1; i++)

        pthread_join(workers[i], NULL);

    /* establish the merge thread */

    data = (parameters *) malloc(sizeof(parameters));

    data->from_index = 0;

    data->to_index = (SIZE/2);

    pthread_create(&workers[2], 0, merger, data);

    /* wait for the merge thread to finish */

    pthread_join(workers[2], NULL);

    /* output the sorted array */

    return 0;

}

void *sorter(void *params)

{

    parameters* p = (parameters *)params;

    //SORT

    int begin = p->from_index;

    int end = p->to_index+1;

    int z;

    for(z = begin; z < end; z++)

    {

        printf("The array recieved is: %d ", list[z]);

    }

    printf(" ");

    int i,j,t,k;

    for(i=begin; i< end; i++)

    {

        for(j=begin; j< end-i-1; j++)

        {

            if(list[j] > list[j+1])

            {

                t = list[j];

                list[j] = list[j+1];

                list[j+1] = t;

            }

        }

    }

    for(k = begin; k< end; k++){

            printf("The sorted array: %d ", list[k]);

    }

    int x;

    for(x=begin; x<end; x++)

    {

            result[x]=list[x] ;

    }

    printf(" ");

    pthread_exit(0);

}

void *merger(void *params)

{

    parameters* p = (parameters *)params;

   //MERGE

    int begin = p->from_index;

    int end = p->to_index+1;

    int i,j,t;

   

    for(i=begin; i< end; i++)

    {

        for(j=begin; j< end-i; j++)

        {

            if(result[j] > result[j+1])

            {

                t = result[j];

                result[j] = result[j+1];

                result[j+1] = t;

            }

        }

    }

    int d;

    for(d=0; d<SIZE; d++)

    {

        printf("The final resulting array is: %d ", result[d]);

    }

    pthread_exit(0);

}

Explanation / Answer

For me it is not showing any error, just u need to include an extra library while compilation as below:

// Assuming that your file name is sort.c

$ gcc sort.c -lpthread

// if you get any pthread library linking error, just install pthread library and tyr again

// TELL me if you still having issues

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote