Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do it in LINUX ONLY Read the man page of the following functions pthread_create,

ID: 3594710 • Letter: D

Question

Do it in LINUX ONLY

Read the man page of the following functions pthread_create, pthread_join. Read the man page of the following functions pthread_mutex_init, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, pthread_mutex_destroy. For each task, fullfill 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?

Thank You

#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*/ message1); pthread_create (&thread2;, NULL, print_ message_ function, (void*) message2); int message function,(void) iret1 pthread_create (&thread1;, NULL, pr ret2 // 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 ) ;

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//Prototype of function print_message_function
void *print_message_function(void *ptr);
//Main function definition
int main()
{
//Creates two thread
pthread_t thread1, thread2;
//Creates two message and stores data
char *message1 = "Thread 1";
char *message2 = "Thread 2";
//To store the return value
int iret1, iret2;
//Create interdependent 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);
//Thread join function to wait for the thread 1 to terminate
pthread_join( thread1, NULL);
//Thread join function to wait for the thread 2 to terminate
pthread_join( thread2, NULL);
//Displays the return value
printf(" Return value of Thread 1: %d", iret1);
printf(" Return value of Thread 2: %d", iret2);
}//End of main
//Function to print the message for each thread
void *print_message_function(void *ptr)
{
//Declares character pointer
char *message;
//stores the message passed to the function in message converting it to char*
message = (char*)ptr;
//Displays the message
printf("%s ", message);
}//End of function

Sample Run:

Thread 1
Thread 2

Return value of Thread 1: 0
Return value of Thread 2: 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote