Question
//compile and link with -pthread #include #include #include void *pthread_function( void *ptr ) { char *message; message = (char *) ptr; printf(%s , message); sleep(1);
//compile and link with -pthread
#include
#include
#include
void *pthread_function( void *ptr ) {
char *message;
message = (char *) ptr;
printf("%s ", message);
sleep(1);
}
main() {
pthread_t thread1, thread2;
char *message1 = "Hello from Thread 1";
char *message2 = "Hi from Thread 2";
int iret1, iret2;
/* Create independent threads*/
iret1 = pthread_create( &thread1, NULL, pthread_function, (void*) message1);
sleep(1);
iret2 = pthread_create( &thread2, NULL, pthread_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d ",iret1);
printf("Thread 2 returns: %d ",iret2);
exit(0);
}
What is the output of this program? What happens in what order?
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