Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ignore d and e since they're more so statements rather than actual questions. Here's the program written in C that the assignment is based off

image text in transcribed

Ignore d and e since they're more so statements rather than actual questions. Here's the program written in C that the assignment is based off of:

image text in transcribed

4. Answer the following questions: a. In Pthreads programs, global variables are shared among all threads; each thread has a private copy of local variables and function arguments. What variables, if any, are shared in pth_hello? b. What do you think strtolo) does? c. Why do you have to call malloc function in the program? d. Now look at the loop that creates the threads. The first argument is a pointer to a pthread_t object. This is an opaque object that is system dependent. The second argument has to do with thread attributes and we will pass NULL. The third argument is the target function that the thread will execute. There is some trickery going on with the fourth argument. First, we are giving the thread a rank because we can't use pthread_t to identify threads (although the operating system can). We are passing an integer but telling the function that it's a pointer; the create function wants a pointer because it expects a list of arguments. In the function, we cast the argument to a long int. This could cause a problem if the pointer type is a different size than the type of the argument-that's why we use long. On some systems and int is 32 bits but a pointer is 64 bits. A long int should be 64 bits on those systems. e. Note that we don't explicitly have to start the threadsthe create function does that. f. The value of my rank for thread 3 is g. What do you think pthread_join() does? h. What do you think free() does? We should have been calling free() in our previous programs that use malloc but I've gotten so used to languages that do garbage collection that I often forget. #include #include #include /* Global variable: accessible to all threads */ int thr_count; void* Hello(void* rank); //Thread function int main(int argc, chart argv[]) { long thr; //Use long in case of 64 bit system pthread_t* thr_handles; // Get number of threads from cmd line thr_count = strtol (argv[1], NULL, 10); thr_handles = malloc(thr_count * sizeof(pthread_t)); for (thr = 0; thr #include #include /* Global variable: accessible to all threads */ int thr_count; void* Hello(void* rank); //Thread function int main(int argc, chart argv[]) { long thr; //Use long in case of 64 bit system pthread_t* thr_handles; // Get number of threads from cmd line thr_count = strtol (argv[1], NULL, 10); thr_handles = malloc(thr_count * sizeof(pthread_t)); for (thr = 0; thr

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

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

Calculate reaction and bending forces on this beam,

Answered: 1 week ago