Question: Creating and joining threads #include int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) (

Creating and joining threads #include int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* in */ ) ; int pthread_join( pthread_t thread /*in */, void ** rel_val_p /* out */ ) Tasks: 1. code is given below:- 2. Use an IDE (Visual Studio, or editor in the Ubuntu virtual machine, or editor in school cluster mamba.urc.uncc.edu). Compile the c code: gcc -g -Wall -o c_pthread_hello c_pthread_hello.c -lpthread Run the code: ./c_pthread_hello 3. Explain to your team member(s) about the compiling command and execution command. 4. Execute the code for five times. What do you observe? 2/3/2020 Lab: Shared-memory Parallelism through PThreads (Part 1) https://uncc.instructure.com/courses/119982/assignments/815014 2/2 5. Copy the c_pthread_hello.c to another file. Adapt the code to mimic your team. Main thread displays the class title; each thread displays the team member name (it is okay to hard coding title and member name in the subquestion) 6. Paste code and explanation in text

CODE #include #include #include

int thread_count;

void * hello(void * rank);

int main(int argc, char* argv[]){ long thread; pthread_t* thread_handlers;

thread_count = strtol(argv[1], NULL, 10); thread_handlers = malloc(thread_count * sizeof(pthread_t));

printf("Create pthread_t data structure"); for (thread = 0; thread < thread_count; thread++){ pthread_create(&thread_handlers[thread], NULL, hello, (void *) thread); }

printf("Hello from main thread ");

for (thread = 0; thread < thread_count; thread++){ pthread_join(thread_handlers[thread], NULL); }

free(thread_handlers); thread_handlers = NULL;

return 0; }

void* hello(void* rank){ long my_rank = (long)rank;

printf("Hello from thread %ld of %d ", my_rank, thread_count);

return NULL; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!