In: Computer Science
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;
}
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