Question: Concerning program thread_creation2.cpp (see below) a) If we switch the order of line #53 and line #54 whats the output of the program? Why? b)

Concerning program thread_creation2.cpp (see below)

a) If we switch the order of line #53 and line #54

whats the output of the program? Why?

b) If we comment line #54, whats the output? Why?

program thread_creation2.cpp :

/******************************************************************** * program taken from page 310 @ APUE * *****************************************************************/

#include //header for UNIX standard library #include #include //header for POSIX thread #include //header for C standard library

pthread_t ntid;

void printids(const char *s) /***************************************************** * print ID of the calling process * print ID of the calling thread * **************************************************/ { pid_t pid; pthread_t tid;

pid = getpid(); //returns the ID of the calling process tid = pthread_self(); //returns the ID of the calling thread printf("%s pid %lu tid %lu (0x%lx) ", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid); }

void * thr_fn(void *arg) /***************************************************** * print information of the new thread * **************************************************/ { printids("new thread: "); return((void *)0); }

int main(void) /*********************************************************** * print process ID, initial thread ID, and child thread ID * *********************************************************/ { int err;

err = pthread_create(&ntid, NULL, thr_fn, NULL); //starts a new thread in the calling process if (err != 0) { printf("Failure. Cannot create thread. "); exit(-1); }

//line 53// printids("main thread:"); //line 54// sleep(1); //pause for 1 second exit(0); }

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!