Need a short report containing the implementation details of this code please. Will give an upvote :) Code: #include

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Need a short report containing the implementation details of this code please. Will give an upvote :) Code: #include

Post by answerhappygod »

Need a short report containing the implementation details ofthis code please. Will give an upvote :)
Code:
#include <pthread.h>#include <stdio.h>#include <stdlib.h>
#define SIZE 10#define NUMBER_OF_THREADS 3
void *sorter(void *params); void *merger(void *params);
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];
// First sorting thread established //parameters *data = (parameters *) malloc(sizeof(parameters));data->from_index = 0;data->to_index = (SIZE/2) - 1;pthread_create(&workers[0], 0, sorter, data);
// Second sorting thread established //data = (parameters *) malloc (sizeof(parameters));data->from_index = (SIZE/2);data->to_index = SIZE - 1;pthread_create(&workers[1], 0, sorter, data);
// 2 sorting threads are waiting to be finished //for (i = 0; i < NUMBER_OF_THREADS - 1; i++)pthread_join(workers, NULL);
data = (parameters *) malloc(sizeof(parameters));data->from_index = 0;data->to_index = (SIZE/2);pthread_create(&workers[2], 0, merger, data);
// Merge thread waiting to be finished //pthread_join(workers[2], NULL);
return 0;}
void *sorter(void *params){parameters* p = (parameters *)params;
//SORTint begin = p->from_index;int end = p->to_index+1;int z;
for(z = begin; z < end; z++){printf("The array recieved is: %d\n", list[z]);}
printf("\n");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\n", list[k]);}
int x;
for(x=begin; x<end; x++){result[x]=list[x] ;}
printf("\n");pthread_exit(0);}
void *merger(void *params){parameters* p = (parameters *)params;
//MERGEint 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\n", result[d]);}
pthread_exit(0);}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply