Question

3. In the main function, pthread create is called to create four child threads. A data file is divided into four blocks. Each

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lets say the data file contains different numbers and we need to find the sum of all numbers in the file
Lets say The data in the file can divided into four equal parts
Now we'll see how to perform this operation in 4 threads
Assume :
   The data is of length 20
   Each block can contain 5 elements each
Operation :
   The threads have thread-id as 0,1,2,3
   using this we can vary the index of the array as follows
   if th-id = 1 , j = 5*1 = 5 to less than 5+5 = 10
   if thid = 2 , j = 5*2 = 10 to less than 5*2+5 = 15
   To generalize for given thread id th , j = 5*th to j<5*th+5 gives the elements in the array for that data block that is to be processed in that particular thread.

PSEUDOCODE :
Initialize global variable sum to 0
sum is used by all the threads
Read the data in the file into an array a
Define a function that is to be passed to thread
   function *sumT(void *threadid):
       1 . Initialize j
       2 . th = (int)threadid
       3 . Loop from j = 5*th to j < 5*th+5
               update sum with a(j)
       4 . print the sum that is calculated in this thread with thread-id
       5 . pthread_exit(NULL) ,exit the thread

Define Main
   main():
       1 . Initialize the number of threads you want
           pthread_t threads[4];
       2 . Loop from i = 0 to 3
               create a thread
               int j = pthread_create(&threads[i],NULL,sumT,(void*)i);
               if(j)
                   print ERROR and ERROR CODE j
                   exit(-1)
       3 . After all threads are executed we need to join them
       4 . Initialize k
       5 . Loop k = 0 to 3
               pthread_join(k,NULL);
       6 . Print the sum variable
       7 . Exit the main thread , pthread_exit(NULL)


Here sum is used as operation to perform in threads, According to your requirement you can change it
The concept remains the same, we need pthread variable for each different thread and a function to execute by the thread and a parameter for that function.2nd parameter to thread_create mostly stays NULL.

Add a comment
Know the answer?
Add Answer to:
3. In the main function, pthread create is called to create four child threads. A data file is divided into four blocks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT