Question

In: Computer Science

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 #%d farmer can cross the bridge\n", array, North);
Bridge(array, North);
printf("%s Tunbridge #%d farmer has left the bridge\n\n", array, North);
North++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}
//Thread for South farmer
void SouthFarmer(){
pthread_mutex_lock(&mutex1);
char array[5] = "South";
printf("%s Tunbridge #%d farmer can cross the bridge\n", array, South);
Bridge(array, South);
printf("%s Tunbridge #%d farmer has left the bridge\n\n", array, South);
South++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}

//void method for bridge
void Bridge(char array[], int value){
srand(time(NULL));
printf("%s Tunbridge #%d is traveling on the bridge...\n", array, value);
int randomnumber = rand() % 4;
sleep(randomnumber);
}

//main method
int main(){
pthread_t North[THREADS]; // North Thread
pthread_t South[THREADS]; // South Thread
pthread_mutex_init(&mutex1,NULL);

for(int i = 0; i < THREADS; i++){
int CreateFirst = pthread_create(&North[i], NULL, NorthFarmer, NULL);
int CreateSecond = pthread_create(&South[i], NULL, SouthFarmer, NULL);
if(CreateFirst != 0 || CreateSecond != 0){
fprintf(stderr, "Thread Create Failed");
return 1;
}
}

for(int i = 0; i < THREADS; i++){
int JoinFirst = pthread_join(North[i],NULL);
int JoinSecond = pthread_join(South[i],NULL);
if(JoinFirst != 0 || JoinSecond != 0){
fprintf(stderr, "Join Failed");
return 1;
}
}

//destroy the mutex lock
pthread_mutex_destroy(&mutex1);
return 0;
}

Solutions

Expert Solution

The entire sourse code

#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 #%d farmer can cross the bridge\n", array, North);
Bridge(array, North);
printf("%s Tunbridge #%d farmer has left the bridge\n\n", array, North);
North++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}
//Thread for South farmer
void SouthFarmer(){
pthread_mutex_lock(&mutex1);
char array[5] = "South";
printf("%s Tunbridge #%d farmer can cross the bridge\n", array, South);
Bridge(array, South);
printf("%s Tunbridge #%d farmer has left the bridge\n\n", array, South);
South++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}

//void method for bridge
void Bridge(char array[], int value){
srand(time(NULL));
printf("%s Tunbridge #%d is traveling on the bridge...\n", array, value);
int randomnumber = rand() % 4;
sleep(randomnumber);
}

//main method
int main(){
pthread_t North[THREADS]; // North Thread
pthread_t South[THREADS]; // South Thread
pthread_mutex_init(&mutex1,NULL);

for(int i = 0; i < THREADS; i++){
int CreateFirst = pthread_create(&North[i], NULL, NorthFarmer, NULL);
int CreateSecond = pthread_create(&South[i], NULL, SouthFarmer, NULL);
if(CreateFirst != 0 || CreateSecond != 0){
fprintf(stderr, "Thread Create Failed");
return 1;
}
}

for(int i = 0; i < THREADS; i++){
int JoinFirst = pthread_join(North[i],NULL);
int JoinSecond = pthread_join(South[i],NULL);
if(JoinFirst != 0 || JoinSecond != 0){
fprintf(stderr, "Join Failed");
return 1;
}
}

//destroy the mutex lock
pthread_mutex_destroy(&mutex1);
return 0;
}

The output

South Tunbridge #1 farmer can cross the bridge
South Tunbridge #1 is traveling on the bridge...
South Tunbridge #1 farmer has left the bridge

North Tunbridge #1 farmer can cross the bridge
North Tunbridge #1 is traveling on the bridge...
North Tunbridge #1 farmer has left the bridge

South Tunbridge #2 farmer can cross the bridge
South Tunbridge #2 is traveling on the bridge...
South Tunbridge #2 farmer has left the bridge

North Tunbridge #2 farmer can cross the bridge
North Tunbridge #2 is traveling on the bridge...
North Tunbridge #2 farmer has left the bridge

South Tunbridge #3 farmer can cross the bridge
South Tunbridge #3 is traveling on the bridge...
South Tunbridge #3 farmer has left the bridge

North Tunbridge #3 farmer can cross the bridge
North Tunbridge #3 is traveling on the bridge...
North Tunbridge #3 farmer has left the bridge

South Tunbridge #4 farmer can cross the bridge
South Tunbridge #4 is traveling on the bridge...
South Tunbridge #4 farmer has left the bridge

North Tunbridge #4 farmer can cross the bridge
North Tunbridge #4 is traveling on the bridge...
North Tunbridge #4 farmer has left the bridge

North Tunbridge #5 farmer can cross the bridge
North Tunbridge #5 is traveling on the bridge...
North Tunbridge #5 farmer has left the bridge

South Tunbridge #5 farmer can cross the bridge
South Tunbridge #5 is traveling on the bridge...
South Tunbridge #5 farmer has left the bridge

South Tunbridge #6 farmer can cross the bridge
South Tunbridge #6 is traveling on the bridge...
South Tunbridge #6 farmer has left the bridge

North Tunbridge #6 farmer can cross the bridge
North Tunbridge #6 is traveling on the bridge...
North Tunbridge #6 farmer has left the bridge

North Tunbridge #7 farmer can cross the bridge
North Tunbridge #7 is traveling on the bridge...
North Tunbridge #7 farmer has left the bridge

South Tunbridge #7 farmer can cross the bridge
South Tunbridge #7 is traveling on the bridge...
South Tunbridge #7 farmer has left the bridge

South Tunbridge #8 farmer can cross the bridge
South Tunbridge #8 is traveling on the bridge...
South Tunbridge #8 farmer has left the bridge

North Tunbridge #8 farmer can cross the bridge
North Tunbridge #8 is traveling on the bridge...
North Tunbridge #8 farmer has left the bridge

South Tunbridge #9 farmer can cross the bridge
South Tunbridge #9 is traveling on the bridge...
South Tunbridge #9 farmer has left the bridge

North Tunbridge #9 farmer can cross the bridge
North Tunbridge #9 is traveling on the bridge...
North Tunbridge #9 farmer has left the bridge

South Tunbridge #10 farmer can cross the bridge
South Tunbridge #10 is traveling on the bridge...
South Tunbridge #10 farmer has left the bridge

North Tunbridge #10 farmer can cross the bridge
North Tunbridge #10 is traveling on the bridge...
North Tunbridge #10 farmer has left the bridge


Related Solutions

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...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
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...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT