Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment involves creating a child thread that generates the Collatz sequence, writing it to global data it shares with the parent. When the child

This assignment involves creating a child thread that generates the Collatz sequence, writing it to global data it shares with the parent. When the child thread terminates, the parent will output the sequence. Your program will run similarly to the previous assignments where you will pass the original number on the command line, such as

./lab3 13

You have some interesting choices for a data structure to store the sequence. One approach is to simply use an array of integer values to store the numbers in the sequence. For example, you may create a global array of the following capacity:

const int SIZE = 25; int sequence[SIZE];

The obvious issue with this approach is that static allocation may be either too large (i.e. you have created an array much larger than necessary) or too small (i.e. you have created an array that is too small to store the sequence.) A better strategy is to instead use a linked list. construct a multithreaded program that has the child thread construct and populate the list using the Collatz sequence. When the child thread has terminated, the parent will output the sequence by traversing the list.

Use the following program as a starting point

#include  #include  #include  int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ if (argc != 2) { fprintf(stderr,"usage: a.out  "); /*exit(1);*/ return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative ",atoi(argv[1])); /*exit(1);*/ return -1; } /* get the default attributes */ pthread_attr_init(&attr); /* create the thread */ pthread_create(&tid,&attr,runner,argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL); printf("sum = %d ",sum); } /** * The thread will begin control in this function */ void *runner(void *param) { int i, upper = atoi(param); sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Optimizing Data Collection In Warzones

Authors: Aaget Aamber

1st Edition

B0CQRRFP5F, 979-8869065902

More Books

Students also viewed these Databases questions

Question

4. Similarity (representativeness).

Answered: 1 week ago

Question

=+/ Does this objective indicate a time frame?

Answered: 1 week ago

Question

Would Zaras model work for other retailers? Why or why not?

Answered: 1 week ago