Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

image text in transcribed

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) da

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions