Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Have the main function in your program first print out a line identifying itself as main and giving the PID of the process and the

Have the main function in your program first print out a line identifying itself as main and giving the PID of the process and the TID (thread identifier) of the first thread, the only thread at this point. (A thread can use pthread_self to get its TID.) Then have your main function create a second thread. The function that you start the second thread in (see note [2], below) needs to have only a single line of code that just prints out a simple "Hi, I'm the second thread" message with its PID and TID (PID will be the same of course, you won't have created a new process, just a new thread). Have the main thread check for success in creating the second thread, and, if successful, have main print out the thread identifier (TID) of the new (second) thread. Use a pthread_join call. Putting in a call to pthread_join(...) after the second printout from main should do the trick:

main thread here; pid is 12369, tid is 47974190738160

main: successfully created a new thread with TID of 1098160448

second thread here, my tid is 1098160448 but I'm still in process 12369

main: second thread has terminated, main will now exit

Add a second function to your program for your second thread to start executing in. The second function of your program, where your new thread will start, is required by pthread_create to be defined as returning a void * value (we won't bother with returning a value this time) and having a single parameter of type void *. Demonstrating that you can pass an argument into a thread will be the third version of this program. Pass in a single character and modify the code of your second function so that its printout is: second thread here, my tid is 1098160448 but I'm still in process 12369, I received a 'B' when I was created

Here is my code so far:

#include

#include

#include

#include

#include

void PrintHello ()

{

pid_t pid;

pthread_t tid;

/*Get Self Process PID */

pid = getpid ();

/*Get Self thread TID */

tid = pthread_self ();

printf ("Hi, I'm the second thread PID : %lu TID : %lu ", pid, tid);

}

int main (int argc, char *argv[])

{

pid_t pid;

pthread_t tid;

int returnCode;

/*Get Self thread TID */

tid = pthread_self ();

/*Get Self Process PID */

pid = getpid ();

pthread_t thread;

/*Display Result */

printf ("I am main program ");

printf ("Process ID : %lu ", pid);

printf ("Thread ID : %lu ", tid);

returnCode = pthread_create(&thread, NULL,PrintHello, NULL);

if (returnCode)

{

printf ("ERROR; return code from pthread_create() is %d ", returnCode);

exit (-1);

}

else /*Display second thread TID*/

{

printf ("Second thread TID : %lu ", thread);

}

/* Last thing that main() should do */

pthread_exit (NULL);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

Write down an Overview of Digital Printing Systems

Answered: 1 week ago

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago