Question

Please complete Part 1. The code is also below:

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

#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 << "!\n";
return 0;
}

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

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);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program code

#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 << "!\n";
return 0;
}

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

cout << "Enter a number ";
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);
}

//Sample output

In main: creating thread                                                                                                      

Enter a number  52                                                                                                            

Hello World from thread with arg: 52!  

Screen shot output

Screen Shot of the program

Add a comment
Know the answer?
Add Answer to:
Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By...

    Please finish the following program /** * Basic POSIX Thread (pthread) Usage 1. * * By walking through this example you’ll learn: * - How to use pthread_create(). * - How to use pthread_exit(). * - How to use pthread_join(). * */ #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sys/wait.h> void* ninja(void* arg){ printf("Who’s there?"); fflush(stdout); pthread_exit("ninja"); } // // Expected output: // // Knock knock. // Who's there? - from ninja // Knuc...kles. // int main(int argc, char* argv[]){...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • MOdify the program below to do 3x3 matrix mutiplications: #include <iostream> #include <cstdlib> #include <pthread.h> using...

    MOdify the program below to do 3x3 matrix mutiplications: #include <iostream> #include <cstdlib> #include <pthread.h> using namespace std; int A[3] [3] = {{1,2},{3,4}}; int B[3] [3] = {{5,6},{7,8}}; int C[3] [3]; struct Position{     int row;     int col; }; void *CalculateElement(void *pos){     Position *p = (Position *)pos;     C[p->row][p->col] = 0;     for(int i = 0; i < 3; i++){         C[p->row][p->col] += A[p->row][i]*B[i][p->col];     }     pthread_exit(NULL); } const int NUM_THREADS = 4; int main() {    ...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Pleas help I need to create and array to add to my code something like this...

    Pleas help I need to create and array to add to my code something like this for (i = 0; i < NUM_THREADS; ++i) { thr_data[i].tid = i; if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) { fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return EXIT_FAILURE; ::::::::: CODE ::::::::::::: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>    int sharedVar = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* thread handler */ void *threadHandler(void *vargp) {    int i = 0;    pthread_mutex_lock(&mutex);   ...

  • Convert the below code into if else selection: #include <iostream> using namespace std; int main() {...

    Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }

  • Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function...

    Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function prototype declaration int main() for (int i=-10 ; i(z10; 1++) {//for each # cout<<"("<< ǐ<<") ^5. "<<PowerFive(1)くくendl;// calling the defined Function int PowerFive (int a) //Function definition return a*a*a*a*a;

  • #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...

    #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT