Question

In: Computer Science

Using pthread_create(), pthread_join(), and pthread_exit(), write a C or C++ program running on VirtualBox Debian VM...

Using pthread_create(), pthread_join(), and pthread_exit(), write a C or C++ program running on VirtualBox Debian VM shell to sort a list of signed integers in nondecreasing order using the sorting algorithm of your own choice from a text file containing the integers.

The input data should be stored in a file named p2data.txt

Sort a list of signed integers in nondecreasing order using three threads, among which two of them are sorting threads responsible for sorting each of the two sublists, while the third thread (i.e. the merging thread) is responsible for merging the two sorted sublists into a single sorted list. You may assume that the total number of integers in the list is less than or equal to 20.

Solutions

Expert Solution

Hi! :)

Here's the documented code to solve the assignment:

prog.c:

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

// reads in the list from p2data.txt and stores in A and stores the length of the list in the integer pointed to by N
void readList(int* A, int* N)
{
    FILE* file = fopen("p2data.txt", "r");

    int index = 0;

    while(!feof(file))
    {
        int number;
        fscanf(file, "%d ", A + index);
        ++index;
    }

    *N = index;

    fclose(file);
}

// swaps the content of the integers pointed by l and r
void swap(int* l, int* r)
{
    int temp = *l;
    *l = *r;
    *r = temp;
}

// struct for passing a sublist to sortSubList
struct SubList
{
    int* A;
    int l;
    int r;
};

// sorts a sublist using selection sort
void* sortSubList(void* varargp)
{
    // getting the array from the arguments
    struct SubList* subList = (struct SubList*) varargp;
    int* A = subList->A;
    int l = subList->l;
    int r = subList->r;

    // selection sort
    for(int i = l; i <= r; ++i)
    {
        int smallestIndex = i;

        for(int j = i + 1; j <= r; ++j)
        {
            if(A[j] < A[smallestIndex])
            {
                smallestIndex = j;
            }
        }

        swap(A + i, A + smallestIndex);
    }

    return NULL;
}

// merges two sorted sublists
// left sublist: A[0...N/2]
// right sublist: A[N/2-1...N-1]
void merge(int* A, int N)
{
    int temp[N];

    int l = 0;
    int r = N / 2 + 1;
    int k = 0;

    while(l <= N / 2 && r <= N - 1)
    {
        if(A[l] < A[r])
        {
            temp[k++] = A[l++];
        }
        else
        {
            temp[k++] = A[r++];
        }
    }

    while(l <= N / 2)
    {
        temp[k++] = A[l++];
    }

    while(r <= N - 1)
    {
        temp[k++] = A[r++];
    }

    for(k = 0; k < N; ++k)
    {
        A[k] = temp[k];
    }
}

// sorts the list using three threads
void sortList(int* A, int N)
{
    // the sublists
    struct SubList leftSubList = {A: A, l: 0, r: N / 2};
    struct SubList rightSubList = {A: A, l: N / 2 + 1, r: N - 1};

    // these two threads store the thread ids
    pthread_t left_thread_id;
    pthread_t right_thread_id;

    // threads for sorting the sublists
    pthread_create(&left_thread_id, NULL, sortSubList, (void*) &leftSubList);
    pthread_create(&right_thread_id, NULL, sortSubList, (void*) &rightSubList);

    // joining the threads after sorting is done
    pthread_join(left_thread_id, NULL);
    pthread_join(right_thread_id, NULL);

    // the third thread, i.e., the main thread merges the sorted sublists
    merge(A, N);
}

// prints the list
void printList(int* A, int N)
{
    puts("Sorted list:");

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

int main()
{
    int A[20]; // stores the list
    int N; // stores the length of the list

    readList(A, &N);
    sortList(A, N);
    printList(A, N);

    return 0;
}

Here's the screenshot of a demo run:

Hope this helps! :)


Related Solutions

Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
write a c++ program using micro soft visual studio 2010 to write a program and store...
write a c++ program using micro soft visual studio 2010 to write a program and store 36 in variable x and 8 in variable y. add them and store the result in the variable sum. then display the sum on screen with descriptive text. calculate the square root of integer 36 in x. store the result in a variable. calculate the cube root of integer 8 in y. store result in a variable. display the results of square root and...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT