Question

In: Computer Science

Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...

Hi, i need flowchart for this code (C++) please, THANX

#include <iostream>
#include <thread>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
using namespace std;
#define NRO 6 // Número de coches
//Puente declarado con matriz y valor entero
void Puente(string array, int value);
// Variable global
int Norte = 1;
int Sur = 1;
sem_t mutex1;
//Coche al norte
void* NorteC(void* arg){
sem_wait(&mutex1);
string array = "En el lado Norte "; // Norte
cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el puente\n";
Puente(array, Norte);
cout<<array<<"del puente, el coche #"<<Norte<<" ha dejado el puente\n\n";
Norte++;
sem_post(&mutex1);
pthread_exit(0);
}
//Coche al sur
void* SurC(void* arg){
sem_wait(&mutex1);
string array = "En el lado Sur ";
cout<<array<<"del puente, el coche #"<<Sur<<" puede cruzar el puente\n";
Puente(array, Sur);
cout<<array<<"del puente, el coche #"<<Sur<<" ha dejado el puente\n\n";
Sur++;
sem_post(&mutex1);
pthread_exit(0);

}
//Método void para el puente
void Puente(string array, int value){
cout<<array<<"del puente, el coche #"<<value<<" esta viajando por el puente...\n";
this_thread::sleep_for(chrono::milliseconds(600));
}
//Programa principal
int main(){
pthread_t Sur[NRO];
pthread_t Norte[NRO];
sem_init(&mutex1,0,1);
for(int i = 0; i < NRO; i++){
int C1 = pthread_create(&Norte[i],NULL,&NorteC,NULL);
int C2 = pthread_create(&Sur[i],NULL,SurC,NULL);
if(C1 != 0 || C2 != 0){
return 1;
}
}
for(int i = 0; i < NRO; i++){
int E1 = pthread_join(Norte[i],NULL);
int E2 = pthread_join(Sur[i],NULL);
if(E1 != 0 || E2 != 0){
return 1;
}
}
return 0;
}

Solutions

Expert Solution

First Function

Second Function:

Third Function:

Main Function:

Please like, I have tried to explained in more detailed way.


Related Solutions

In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word,...
c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word, phrase, number, or sequence of words that reads the same backward as forward. Punctuation, capitalization, and spaces between the words or lettering are allowed. Here are some examples of word and phrase palindromes. Words: Civic Kayak Level Madam Mom Noon Racecar Radar Refer Rotor Phrases: Don't nod. I did, did I? My gym Step on no pets Top spot Never odd or even Eva,...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
I get the following errors when I type the code #include <iostream> #include <string.h> #include <bitset>...
I get the following errors when I type the code #include <iostream> #include <string.h> #include <bitset> #include <math.h> #define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0 using namespace std; template<class T> std::string inttobits1(T byte, IS_INTEGRAL(T)) { std::bitset<sizeof(T)* CHAR_BIT> bs(byte); return bs.to_string(); } int bitstoint1(string bits) { int value; for (int x = 0, y = bits.length() - 1;x < 8 && x < bits.length();x++) { int val = atoi(bits.substr(y, 1).c_str()); value += (int)val * pow(2, x); } return value; }...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT