Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: In this assignment, you will be writing a multithreaded program. You

ID: 3873150 • Letter: O

Question

Objective:

In this assignment, you will be writing a multithreaded program. You will demonstrate your knowledge of the POSIX Pthreads library and how to create, destroy, and wait for threads. You will also demonstrate how to pass data between parent and child threads.

Details:

You will create a multithreaded program that takes one or more numeric arguments from the command line and displays the prime factors (Linkof each number. Each child thread will be passed a number from the parent thread that should be factored by that thread. Each child thread will return a sorted integer array of prime factors for that number. The parent thread will wait for each thread to terminate. The parent thread will display the original number provided and all factors returned by the child thread to the console.

The child thread must not display any data. Your only output should be provided by the main thread, and only after waiting for the thread to complete.

Output:

Here are several examples of what is expected when you run your program:

# ./assn3
Usage:./assn3 <number to factor>...

# ./assn3 12
12: 2 2 3

# ./assn3 7 12 25
7: 7
12: 2 2 3
25: 5 5

# ./assn3 {32..48}
32: 2 2 2 2 2
33: 3 11
34: 2 17
35: 5 7
36: 2 2 3 3
37: 37
38: 2 19
39: 3 13
40: 2 2 2 5
41: 41
42: 2 3 7
43: 43
44: 2 2 11
45: 3 3 5
46: 2 23
47: 47
48: 2 2 2 2 3

Explanation / Answer

# this is c++ code for the above function

#include <bits/stdc++.h>
#include <pthread.h>
using namespace std;
int prime[25] = {2,3,5,7,11,
                  13,17,19,23,29,
                  31,37,41,43,47,
                  53,59,61,67,71,
                  73,79,83,89,97};
/* create thread argument struct for thr_func() which stores
number corresponding to thread and list of prime factors of the thread */
typedef struct _thread_data_t
{
    int tid,num;
    double stuff;
    list<int> ans;
}thread_data_t;


/* thread function */
void *thr_func(void *arg)
{
    thread_data_t *data = (thread_data_t *)arg;
    int val = data->num,index=0;
  
    /* storing prime factors in the list */
    while(val != 1)
    {
        if(val % prime[index] == 0)
        {
          val = val / prime[index];
          data->ans.push_back(prime[index]);
        }
        else
          index++;
    }

    /* exiting the thread */
    pthread_exit(NULL);
}
int main(int argc, char **argv)
{
    int k,l;
    int arr[argc-1];
    for(k=1;k<argc;k++)
    {
      int len = strlen(argv[k]);
      int dec=0;
      for(l=0; l<len; l++)
          dec = dec * 10 + ( argv[k][l] - '0' );
      arr[k-1] = dec;
    }
  
    pthread_t thr[argc-1];
    int i, rc;
    /* create a thread_data_t argument array */
    thread_data_t thr_data[argc-1];

    /* create threads */
    for (i = 0; i < argc-1; ++i)
    {
      thr_data[i].tid = i;
      thr_data[i].num = arr[i];
      if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i])))
      {
        fprintf(stderr, "error: pthread_create, rc: %d ", rc);
        return EXIT_FAILURE;
      }
    }
    /* block until all threads complete */
    for (i = 0; i < argc-1; ++i)
    {
      pthread_join(thr[i], NULL);
    }

    /* printing aal the prime factors of the numbers */
    list<int> :: iterator it;
    for (i = 0; i < argc-1; ++i)
    {
      cout<<arr[i]<<": ";
      for(it = thr_data[i].ans.begin(); it!= thr_data[i].ans.end(); it++)
      {
        cout<<*it<<" ";
      }
      cout<<endl;
    }
    return EXIT_SUCCESS;
}