Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Bartleby Related Questions Icon
Related questions
Question
thumb_up100%
Need help completing C++ program
#include
#include
#include
#include
#include
static pthread_mutex_t bsem;
static pthread_cond_t waitTurn = PTHREAD_COND_INITIALIZER;
static int turn;
static int nthreads;
void *print_in_reverse_order_odd_then_even(void *void_ptr_argv)
{
// std::cout << "I am Thread " << /*variable identifier*/ << std::endl;
return NULL;
}
int main()
{
std::cin >> nthreads;
pthread_mutex_init(&bsem, NULL); // Initialize access to 1
pthread_t *tid= new pthread_t[nthreads];
int *threadNumber=new int[nthreads];
//HINT: this code determines the starting thread (thread with the highest odd number).
//You can erase this if statement if your solution does not need to know the starting child thread number.
if ((nthreads-1)%2!=0)
turn = nthreads -1;
else
turn = nthreads -2;
for(int i=0;i
Transcribed Image Text:4:32
Done
Scanned Documents
Write a C++ program that creates n child threads, where each child thread prints a message into
STDOUT with the assigned number by the parent thread. The range of the integer values assigned
by the main thread to the child threads goes from 0 until n - 1. Your program must use
synchronization mechanisms to guarantee that the child threads print the message in the
opposite order they were created, starting with child threads with odd numbers and finishing with
child threads with even numbers. Your program will receive from STDIN the number of child
threads (nchildthreads).
For nchildthreads = 5, the expected output is:
I am Thread 3
I am Thread 1
I am Thread 4
I am Thread 2
I am Thread e
NOTES
1. You must create n child threads (where n is the value that your program receives from
STDIN).
2. The integer number assigned to each child thread has a value from 0 to n-1.
3. Zero is considered an even number.
4. You can add as many global variables as needed.
5. You can only use POSIX semaphores, pthreads mutex semaphore, or pthreads
condition variables to achieve synchronization (A penalty of 100% will be applied to
solutions using mechanisms other than the synchronization mechanisms listed
before).
Transcribed Image Text:main.cpp
#include <pthread.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
1
2.
static pthread_mutex_t bsem;
static pthread_cond_t waitTurn = PTHREAD_COND_INITIALIZER;
static int turn;
static int nthreads;
7
10
11
void *print_in_reverse_order_odd_then_even(void *void_ptr_argv)
13 {
12
// std::cout << "I am Thread "
return NULL;
14
<</*variable identifier*/ << std::endl;
15
16
17
int main()
19 {
18
std::cin >> nthreads;
pthread_mutex_init(&bsem, NULL); // Initialize access to 1
pthread t *tid= new pthread_t[nthreads];
int threadNumber=new int[nthreads]
20
21
22
23
24
//HINT: this code determines the starting thread (thread with the highest odd number).
//You can erase this if statement if your solution does not need to know the starting
25
26
27
if ((nthreads-1)%2!=0)
turn = nthreads -1;
else
28
29
30
31
turn = nthreads -2;
32
33
for(int i=0;i<nthreads; i++)
34 -
35
36
// wait for the other threads to finish.
for (int i = 0; i < nthreads; i++)
pthread join(tid[i], NULL);
37
38
39
delete [] threadNumber;
delete [] tid;
return e;
40
41
42
43
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
This is a popular solution
bartleby
Trending nowThis is a popular solution!
bartleby
Step by stepSolved in 2 steps with 1 images
Knowledge Booster
Background pattern image
Similar questions
- /* Interface File: Movie.h Declaration of Movie Class (Variables - "Data Members" or "Attributes" AND Functions - "Member Functions" or "Methods") */ #ifndef MOVIE_H // Include Guard or Header Guard -If already defined ignore rest of code #define MOVIE_H // Otherwise, define MOVIE_H #include<string> // Note: Not "using namespace std;" or even "using std::string" class Movie { private: std::string title = ""; // Explict scope used --> std::string int year = 0; public: Movie(std::string title = "", int year = 1888); // Declaring a Default Constructor ~Movie(); // A Destructor used for freeing up resources void set_title(std::string title_param); std::string get_title() const; // "const" safeguards class variable changes within function std::string get_title_upper() const; void set_year(int year_param); int get_year() const; }; //...arrow_forward#include <iostream> #include <iomanip> #include <string> #include <vector> using namespace std; class Movie { private: string title = ""; int year = 0; public: void set_title(string title_param); string get_title() const; // "const" safeguards class variable changes within function string get_title_upper() const; void set_year(int year_param); int get_year() const; }; // NOTICE: Class declaration ends with semicolon! void Movie::set_title(string title_param) { title = title_param; } string Movie::get_title() const { return title; } string Movie::get_title_upper() const { string title_upper; for (char c : title) { title_upper.push_back(toupper(c)); } return title_upper; } void Movie::set_year(int year_param) { year = year_param; } int Movie::get_year() const { return year; } int main() { cout << "The Movie List program\n\n"...arrow_forward// Assume all libraries are included 3 void func (int a, int &b, int &c); // int main () { int i = 5, j = 4, k = 33; 4 7 %3D 8. func (i, j, k); func (j, i, k); cout << j « k << i <« endl; 10 11 12 13 return 0; 14 } // 15 16 void func (int a, int &b, int &c) { 17 18 = 2*c + b; 19 b a; 20 C = a + 3*b; } // 21 22 I|||arrow_forward
- Not C++ Only Carrow_forwardIn C++ struct applianceType { string supplier; string modelNo; double cost; }; applianceType myNewAppliance; applianceType applianceList[25]; Write the C++ statements that prints out all the appliance info from applianceList whose modelNo is B1234.arrow_forwardC++ code. Please describe what EACH line means/does. Ignore code that is commented out.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY