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

I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
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++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h>...
C++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h> using namespace std; void GenerateData() //DO NOT TOUCH CODE IN THIS METHOD { JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine) JNIEnv *env; // Pointer to native interface //================== prepare loading of Java VM ============================ JavaVMInitArgs vm_args; // Initialization arguments JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options options[0].optionString = (char*) "-Djava.class.path="; // where to find java .class vm_args.version = JNI_VERSION_1_6;...
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 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....
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
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]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT