Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please complete all tasks for a thumbs up :-) TASK A: #include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid;
Please complete all tasks for a thumbs up :-)
TASK A:
#include#include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld! ", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { // create an array of thread struct instances with appropriate length long t; for(t=0; t TASK B:
#include#include #include void *print_message_function( void *ptr ); main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; /* Create independent threads each of which will execute function */ iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create(&thread2, NULL, print_message_function, (void*) message2); // use thread join function to wait for the thread thread1 to terminate // do the same for thread2 // print the return value of each thread return(0); } void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); } TASK C:
#includeasks Read the man page of the following functions: pthread_join pthread-mutex-init pthread_mutex_lock pthread mutex_trylock pthread_mutex_unlock pthread_mutex_destroy For each task, fullill the requirements provided in the comments, or fill the blank Compile the code and make sure it is executable What is the output of the code? What does the code do#include #include void *functionC(); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; main() { int rc1, rc2; pthread_t thread1, thread2; /* Create independent threads each of which will execute functionC */ if((rc1=pthread_create( &thread1, NULL,__________ , NULL))) { printf("Thread creation failed: %d ", rc1); } if((rc2=pthread_create( &thread2, NULL,__________ , NULL))) { printf("Thread creation failed: %d ", rc2); } pthread_join(__________ , NULL); pthread_join(__________ , NULL); return(0); } void *functionC() { pthread_mutex_lock(__________ ); counter++; printf("Counter value: %d ",counter); pthread_mutex_unlock(__________ ); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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