part3: Write a program( in C )that takes 2 command line arguments as input param
ID: 3667671 • Letter: P
Question
part3: Write a program( in C )that takes 2 command line arguments as input parameters and computes the sum of all the integers form the first one and up to but not including the second. Example if the parameters were 100 and 8000, you would sum the numbers from 100 and up to 7999. YOU must divide the work among 4 concurrently executing threads. Each thread should compute approximately ¼ of the numbers. The main should wait till all the threads are done and then sum the result.
Here are some results I got from running the program in Part3 using 4 threads and doing it using the same computation just in the main. Run1 is from 1 to 1,000,000. Run2 is from 1 to 1,000,000,000 1. Run Multithreaded 249s 055s SingleThreaded 229s 049 user time. 1 systime user time 2 .975s 3.601s sys time 2 .055s .055s Discuss the above results briefly explaining what they mean. Predict what would occur if I went from 1 to 10,000,000,000.Explanation / Answer
# include <stdio.h>
# include <pthread.h>
void * thread_sum(void *);
int TotalSum=0;
pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;
main()
{
int iNumber,iCount;
pthread_t tid;
printf("Enter Number Up to Which You want to Sum :");
scanf("%d",&iNumber);
pthread_create(&tid,NULL,thread_sum,(void *)&iNumber);
for(iCount=1;iCount<=iNumber;iCount=iCount+2)
{
pthread_mutex_lock(&mVar);
TotalSum=TotalSum + iCount;
pthread_mutex_unlock(&mVar);
}
pthread_join(tid,NULL);
printf("Final Sum is : %d ",TotalSum);
// return 0;
}
void *thread_sum(void *no)
{
int *iNumber,iCount;
iNumber=(int*)no;
for(iCount=2;iCount<=*iNumber;iCount=iCount+2)
{
pthread_mutex_lock(&mVar);
TotalSum=TotalSum + iCount;
pthread_mutex_unlock(&mVar);
}
pthread_exit(NULL);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.