Have the main function in your program first print out a line identifying itself
ID: 3890411 • Letter: H
Question
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 <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include<pthread.h>
#include<errno.h>
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);
}
Explanation / Answer
//u can find my code under comment //Added by Chegg EA
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include<pthread.h>
#include<errno.h>
//Added by chegg EA, fnction declaration to pass arguments to thread
void *myThreadFun(void *arg);
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;
// Added by Chegg EA,char to be passed to second thread
char arg= 'B';
/*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,myThreadFun, (void*)&arg);
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);
}
// Added by chegg EA, fnction defintion to take arguments to thread
void *myThreadFun(void *arg)
{
// Store the value argument passed to this thread
char *myid = (char *)arg;
// Print the argument passed to this thread
printf("I received a '%c' when I was created ", *myid);
}
-----------------------------------------------------------------------------------
//output
I am main program
Process ID : 3
Thread ID : 139823621904192
Second thread TID : 139823613507328
I received a 'B' when I was created
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.