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

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!