Expected Output: Here are several examples of what is expected when you run your
ID: 3604826 • Letter: E
Question
Expected 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
C language is the preferred choice of coding
Objective:
In this assignment, you will be writing a multithreaded program that shares data between threads. You will need to properly protect data that is shared between threads. You will be using mutex locks around critical sections to ensure your program works correctly and does not experience race conditions.
Details:
You will write the program to only use three threads; the main thread, a producer, and a consumer. The three threads will accomplish the following tasks:
Main Thread:
Create the producer and consumer threads
Send each number from the command line to the producer thread via a shared buffer.
This buffer only needs to be shared between the main thread and the producer.
Wait for each thread to complete before terminating the program
Producer Thread:
Wait for numbers to be added to the buffer shared with the main thread
Factor each number
Save numbers to a buffer shared between the producer and consumer threads
Consumer Thread:
Wait for factors to be added to the buffer shared with the producer thread
Display the factors
This program will use two sets of shared data, in the producer-consumer style. Both sets of data need to be protected using pthread_mutex_t variables. The only thread producing any output to the console should be the consumer thread. All other threads should perform their actions silently.
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.