Question: ************************************************************ main.c ************************************************************ #include #include #include #include functions.h int MIN = 9999; /*Used when you implement the thread function */ int main(int argc, char *argv[])

************************************************************

main.c

************************************************************

#include

#include

#include

#include "functions.h"

int MIN = 9999; /*Used when you implement the thread function */

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

{

int *array;

struct ArrayPackage onePackage;

int size = 0;

int num_threads;

int increments;

int min = 9999;

pthread_t *thread_ids;

void *p_status;

if (pthread_mutex_init(&min_lock, NULL) != 0)

{

perror("Could not create mutex for MIN: ");

return 1;

}

printf("How many threads? ");

scanf("%d", &num_threads);

thread_ids = (pthread_t *)malloc(sizeof(pthread_t *) * num_threads);

do

{

printf("How big is the array (100 elements or more)?");

scanf("%i", &size);

} while (size

array = (int *)malloc(sizeof(int) * size);

increments = size / num_threads;

if (initialize(array, size) != 0)

{

printf("initialization error ");

exit(1);

}

printArray(array, size);

for (int i = 0; i

{

struct ArrayPackage *onePack =

(struct ArrayPackage *)malloc(sizeof(struct ArrayPackage));

/*-------------------------------------------------------------------*/

/* Store the array, the start, and the end into the onePack instance */

/*---------------------------------------------------------------------*/

/* Call the pthread_create */

}

for (int i = 0; i

{

/*---------------------------------------------------------------------*/

/*Call the join */

}

/*-----------------------------------------------------------------------*/

/* The following call to findMin will be taken out when you implement the thread function*/

if (findMin(array, size, &min) != 0)

{

printf("someFunction error ");

exit(1);

}

/*-----------------------------------------------------------------------*/

/* The following line will have to be changed */

printf("min value in array is: %d ", min);

free(array);

free(thread_ids);

return 0;

}

************************************************************

functions.h

************************************************************

#ifndef FUNCTIONS_H

#define FUNCTIONS_H

#include

#include

#include

#include

struct ArrayPackage

{

int *array;

int start;

int end;

};

extern int MIN;

pthread_mutex_t min_lock;

int initialize(int *array, int length);

int findMin(int *array, int length, int *min);

int printArray(int *array, int length);

#endif

************************************************************

functions.c

************************************************************

/* FUNCTION: initialize

* This function should initialize the array

* to random values between 1 and 500

*

* The arguments are:

* array: pointer to an array of integer values

* length: size of array

*

* It returns:

* 0: on success

* non-zero: on an error

*/

int initialize(int *array, int length)

{

srand(getpid());

int i;

for (i = 0; i

{

array[i] = rand() % 500 + 1;

}

return 0;

}

/* FUNCTION: findMin

* This function should find the smallest element in the array and

* return it through the argument min.

*

* The arguments are:

* array: pointer to an array of integer values

* length: size of array

* min: set to the smallest value in array

*

* It returns:

* 0: on success

* non-zero: on an error

*/

int findMin(int *array, int length, int *min)

{

int i;

*min = array[0];

for (i = 1; i

{

if (*min > array[i])

{

*min = array[i];

}

}int findMin(int *array, int length, int *min)

{

int i;

*min = array[0];

for (i = 1; i

{

if (*min > array[i])

{

*min = array[i];

}

}

return 0;

}

/* FUNCTION: printArray

* This function should print all the elements of the array

*

* The arguments are:

* array: pointer to an array of integer values

* length: size of array

*

* It returns:

* 0: on success

* non-zero: on an error

*/

int printArray(int *array, int length)

{

int i;

for (i = 0; i

{

printf("%d ", array[i]);

// add a newline character for readability

if ((i + 1) % 15 == 0)

{

printf(" ");

}

}

printf(" ");

return 0;

}

return 0;

}

/* FUNCTION: printArray

* This function should print all the elements of the array

*

* The arguments are:

* array: pointer to an array of integer values

* length: size of array

*

* It returns:

* 0: on success

* non-zero: on an error

*/

int printArray(int *array, int length)

{

int i;

for (i = 0; i

{

printf("%d ", array[i]);

// add a newline character for readability

if ((i + 1) % 15 == 0)

{

printf(" ");

}

}

printf(" ");

return 0;

}

Change main

load the onePack instance with the array, the start and end of indices that you want to work within the array

for the last thread, the end index will be the size of the array

add the call to the pthread_create

add the call to the pthread_join

remove the original call to the findMin function

modify the variable that is printed to display the global minimum

Change findMin

turn findMin into a thread function (hint: two void *)

create a pointer to a struct ArrayPackage

you might want to store the pieces of ArrayPackage into variables

print the "start" and the "end" that is sent to the thread function using the ArrayPackage

loop from "start" to "end" to find the minimum of the array

note: anytime you print or anytime you work with the global MIN, you will want to have a mutex_lock and mutex_unlock around that code

Although there are different approaches, the approach we are expecting is that this function returns NULL.

Please ensure that you are cleaning up memory for the arguments allocated in main

************************************************************ main.c ************************************************************ #include #include #include #include "functions.h" int MIN = 9999;

Sample runs

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!