Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <pthread.h> #include <iostream> using namespace std; void *PrintHello(v

ID: 3882266 • Letter: #

Question

#include <pthread.h>
#include <iostream>

using namespace std;

void *PrintHello(void *arg)
{
int actual_arg = *((int*) arg);
cout << "Hello World from thread with arg: " << actual_arg << "! ";
return 0;
}

int main()
{
pthread_t id;
int rc;
cout << "In main: creating thread ";

int t = 23;
rc = pthread_create(&id, NULL, PrintHello, (void*) &t);

if (rc){
cout << "ERROR; return code from pthread_create() is " << rc << std::endl;
return -1;
}

pthread_exit(0);
}

5. Issue the following command to execute the program: /pthread test_3 Part 1: Creating One Thread (2 points) 1. Take the pthread_test_3.cpp program and modify the main function, instead of pre-specifying/hard-coding the integer to be passed to the pthread function PrintHello, ask the user to enter an integer and pass that integer to the pthread function. 2. Build and run your program and make sure that it works correctly. Your output should be similar to the following: In main: creating thread Enter a number: 52 Hello World from thread with arg: 52! Part 2: Creating Multiple Threads (10 points) 1. Make a copy of the program named pthreads_skeleton.cpp, which is

Explanation / Answer

#include <pthread.h>

#include <iostream>

using namespace std;

// passing integer as argument

void *PrintHello(void *threadid)

{

int id = *((int*) threadid);

cout << "Hello World from thread with arg: " << id << "! ";

return 0;

}

int main()

{

pthread_t id;

int rc,t;

cout << "In main: creating thread ";

//user input

cout<< "Enter the Number: ";

//read user input from console

cin>>t;

rc = pthread_create(&id, NULL, PrintHello, (void*) &t);

if (rc){

cout << "ERROR; return code from pthread_create() is " << rc << std::endl;

return -1;

}

pthread_exit(0);

}