Question

In: Computer Science

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 #%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

CODE FOR C++ IUSING SEMAPHORES

#include <iostream>
#include <thread>
#include <unistd.h>
#include <string>
#include <pthread.h>
using namespace std;

#define THREADS 10 // Number of Thread
//bridge declared with array of character and integer value
void Bridge(string 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(void* arg){
pthread_mutex_lock(&mutex1);
string array = "North"; // North
cout<<array<<" Tunbridge #"<<North<<" farmer can cross the bridge\n";
Bridge(array, North);
cout<<array<<" Tunbridge #"<<North<<" farmer has left the bridge\n\n";
North++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}
//Thread for South farmer
void* SouthFarmer(void* arg){
pthread_mutex_lock(&mutex1);
string array = "South";
cout<<array<<" Tunbridge #"<<South<<" farmer can cross the bridge\n";
Bridge(array, South);
cout<<array<<" Tunbridge #"<<South<<" farmer has left the bridge\n\n";
South++;
pthread_mutex_unlock(&mutex1);
pthread_exit(0);
}
//void method for bridge
void Bridge(string array, int value){
srand(time(NULL));
cout<<array<<"Tunbridge #"<<value<<" is traveling on the bridge...\n";
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;
}


Output:

up to 10.

I hope u like my effort...... Thank u


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 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...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT