Question
Redo the programming assignment on Inter-Process Communication using a maximum of 8 threads. The number of trapezoids will be input by the user. using the
Redo the programming assignment on Inter-Process Communication using a maximum of 8 threads. The number of trapezoids will be input by the user.
using the integral
by using N processes to sum up n trapezoids (n>N).
here is the half done code:
#include
#include
#include
#include
using namespace std;
#define MAX_NUM_THREADS 8
char *messages[MAX_NUM_THREADS];
struct thread_data
{
int thread_id;
char *message;
};
struct thread_data thread_data_array[MAX_NUM_THREADS];
void *PrintHello(void *threadarg)
{
int taskid;
char *hello_msg;
struct thread_data *my_data;
sleep(1);
my_data = (struct thread_data *) threadarg;
taskid = my_data->thread_id;
hello_msg = my_data->message;
printf("Thread %d: %s ", taskid, hello_msg);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[MAX_NUM_THREADS];
int rc, t;
size_t malloc_size = 100;
for (int i = 0; i
{
messages[i] = (char*)malloc(malloc_size);
printf("Enter messages:%d ",i+1); // as indicated in question input is taken from user
scanf("%[^ ]%*c", messages[i]);
}
for(t = 0; t
thread_data_array[t].thread_id = t;
thread_data_array[t].message = messages[t];
printf("Creating thread %d ", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)
&thread_data_array[t]);
if (rc) {
printf("ERROR; return code from pthread_create() is %d ", rc);
exit(-1);
}
}
/// wait all threads by joining them
for (int i = 0; i
pthread_join(threads[i], NULL);
}// detach thread
pthread_detach(pthread_self());
// pthread_exit(NULL);
return EXIT_SUCCESS;
}
N should vary in the range of 1 to 8, while n will be input by the user.
rcos (2) daStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started