OS ( Operating Systems ). Write a pthread program to find the maximum number in
ID: 3683191 • Letter: O
Question
OS (Operating Systems).
Write a pthread program to find the maximum number in a random array of n integers. Your program should split the array into k segments, and each of the k threads will find the maximum number over that segment of the array. The segment max should be stored in a global array called a of type integer and size of k. Input: From the command line the user will provide file (named: input) where the numbers to find the max will be read into an array and the number of threads to create. The first number in the file will be the size of the array you will need to create. Output: Simply output the max number.Explanation / Answer
# include # include # define arrSize 10 struct StructMaxMin { int iMax; int iMin; }; int arr[arrSize]; void *thread_search_min_max(void *); int main() { pthread_t tid; struct StructMaxMin *st_main,*st_th; int FinalMax,FinalMin; st_main=(struct StructMaxMin*)malloc(sizeof(struct StructMaxMin)); int iCount; for(iCount=0;iCountiMax=arr[0]; st_main->iMin=arr[0]; for(iCount=1;iCount st_main->iMax) { st_main->iMax=arr[iCount]; } if(arr[iCount] iMin) { st_main->iMin=arr[iCount]; } } pthread_join(tid,(void**)&st_th); if(st_main->iMax >= st_th->iMax) { FinalMax=st_main->iMax; } else { FinalMax=st_th->iMax; } if(st_main->iMin iMin) { FinalMin=st_main->iMin; } else { FinalMin=st_th->iMin; } printf("Final Max : %d ",FinalMax); printf("Final Min : %d ",FinalMin); return 0; } void *thread_search_min_max(void *para) { struct StructMaxMin *st; st=(struct StructMaxMin*)malloc(sizeof(struct StructMaxMin)); int iCount; st->iMax=arr[arrSize/2]; st->iMin=arr[arrSize/2]; for(iCount=arrSize/2 + 1;iCount st->iMax) { st->iMax=arr[iCount]; } if(arr[iCount] iMin) { st->iMin=arr[iCount]; } } pthread_exit((void*)st); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.