implement in c This is a OS concept Implement the Ballroom D

implement in c++. This is a OS concept

Implement the Ballroom Dancers simulation discussed on slides 26-28 in the Semaphores powerpoint. Input: .about n m output.txt where n is the number of \"leaders\" and m is the number of \"followers\" and output.txt is the output file generated by your program. Output: leader dances with follower for all threads... Note, the number of leaders and followers may be different. We want to make sure all dancers dance at least once. After all have dance, the program ends.

Solution

I have created the code with the information you have provided:

#include <unistd.h> //Provides API for POSIX(or UNIX) OS for system calls
#include <stdio.h>
#include <iostream>
#include <stdlib.h> //For exit() and rand()
#include <pthread.h> //Threading APIs
#include <semaphore.h> //Semaphore APIs
#define DANCE_TIME 1 //Dance Time 1 second
#define NUM_LEAD 2 //No. of leaders (input value can be set here for leaders)
#define MAX_FOLL 30 //Maximum no. of followers for simulation (input value can be set here for followers)
sem_t followers; //Semaphore
sem_t leaders; //Semaphore
sem_t mutex; //Semaphore for providing mutially exclusive access
int MAX_LEADERS = NUM_LEAD;
int danceWithMe = 0; //Index for next legitimate couple
int serveMeNext = 0; //Index to choose a candidate for dancing
int numberOfFreeLeaders = MAX_LEADERS;//Counter for Vacant seats in waiting room
int seatPocket[MAX_LEADERS]; //To exchange pid between customer and barber
static int count = 0; //Counter of No. of followers
void leaderThread(void *tmp); //Thread Function
void followerThread(void *tmp); //Thread Function
void wait(); //Randomized delay function

int main()
{   
pthread_t leader[NUM_LEAD],follower[MAX_FOLL]; //Thread declaration
int i,status=0;
/*Semaphore initialization*/
sem_init(&followers,0,0);
sem_init(&leaders,0,0);
sem_init(&mutex,0,1);   
/*leader thread initialization*/
for(i=0;i<NUM_LEAD;i++) //Creation of 2 leader Threads
{   
status=pthread_create(&leader[i],NULL,(void *)leaderThread,(void*)&i);
sleep(1);
if(status!=0)
perror(\"No leader Present... Sorry!!\ \");
}
/*follower thread initialization*/
for(i=0;i<MAX_FOLL;i++) //Creation of follower Threads
{   
status=pthread_create(&follower[i],NULL,(void *)followerThread,(void*)&i);
wait(); //Create followers in random interval
if(status!=0)
perror(\"No followers Yet!!!\ \");
}   
for(i=0;i<MAX_FOLL;i++) //Waiting till all followers are dealt with
pthread_join(follower[i],NULL);
printf(\"!!All Dancers dances!!\ \");
exit(EXIT_SUCCESS); //Exit abandoning infinite loop of leader thread
}

void followerThread(void *tmp) /*follower Process*/
{   
int myPartner, B;
sem_wait(&mutex); //Lock mutex to protect seat changes
count++; //Arrival of follower
printf(\"follower-%d[Id:%d] Entered Shop. \",count,pthread_self());
if(numberOfFreeLeader > 0)
{
--numberOfFreeLeaders;   
printf(\"follower-%d waiting for leaders.\ \",count);
danceWithMe = (++danceWithMe) % MAX_LEADERS;
myPartner = danceWithMe;
seatPocket[myPartner] = count;
sem_post(&mutex);
sem_post(&leaders);
sem_wait(&followers);
sem_wait(&mutex); //Lock mutex
B = seatPocket[myPartner]; //leader replaces follower PID with his own PID
numberOfFreeLeaders++; //Stand Up and Go to leader Room
sem_post(&mutex); //Release the seat change mutex
/*follower is having DANCE with leader \'B\'*/
}
pthread_exit(0);
}

void leaderThread(void *tmp) /*leader Process*/
{   
int index = *(int *)(tmp);
int myNext, C;
printf(\"leader-%d[Id:%d]\",index,pthread_self());
while(1) /*Infinite loop*/   
{   
sem_wait(&leaders);
sem_wait(&mutex); //Lock mutex to protect seat changes
serveMeNext = (++serveMeNext) % MAX_LEADERS; //Select next follower
myNext = serveMeNext;
C = seatPocket[myNext]; //Get selected follower\'s PID
seatPocket[myNext] = pthread_self(); //Leave own PID for follower
sem_post(&mutex);
sem_post(&followers); //Call selected follower
/*leader is Dancing with follower \'C\'*/
printf(\"leader-%d Is Dancing with follower-%d.\ \",index,C);
sleep(DANCE_TIME);
printf(\"leader-%d Finishes dancing. \",index);
}
}

void wait() /*Generates random number between 50000 to 250000*/
{
int x = rand() % (250000 - 50000 + 1) + 50000;
srand(time(NULL));
usleep(x);
}

implement in c++. This is a OS concept Implement the Ballroom Dancers simulation discussed on slides 26-28 in the Semaphores powerpoint. Input: .about n m outpu
implement in c++. This is a OS concept Implement the Ballroom Dancers simulation discussed on slides 26-28 in the Semaphores powerpoint. Input: .about n m outpu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site