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...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES 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;...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================...
I need the following code completed: #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; // FIXME (1): Internal structure for tree node struct Node {    Bid data; Node *left; Node...
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;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT