SlideShare a Scribd company logo
1 of 3
Download to read offline
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 //Provides API for POSIX(or UNIX) OS for system calls
#include
#include
#include //For exit() and rand()
#include //Threading APIs
#include //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 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);
}

More Related Content

Similar to implement in c++. This is a OS concept Implement the Ballroom Dancer.pdf

PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfRahul04August
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfmarketing413921
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfmohammadirfan136964
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ applicationDaniele Pallastrelli
 
LCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELinaro
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPatrick Allaert
 

Similar to implement in c++. This is a OS concept Implement the Ballroom Dancer.pdf (20)

Handout#10
Handout#10Handout#10
Handout#10
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdf
 
Bluespec @waseda
Bluespec @wasedaBluespec @waseda
Bluespec @waseda
 
PDCCLECTUREE.pptx
PDCCLECTUREE.pptxPDCCLECTUREE.pptx
PDCCLECTUREE.pptx
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Docopt
DocoptDocopt
Docopt
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
LCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEE
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
PHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & PinbaPHP applications/environments monitoring: APM & Pinba
PHP applications/environments monitoring: APM & Pinba
 

More from ivylinvaydak64229

For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
For the hypothesis test H0  = 5 against H1   5 with variance unkn.pdfFor the hypothesis test H0  = 5 against H1   5 with variance unkn.pdf
For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdfivylinvaydak64229
 
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfEarly in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfivylinvaydak64229
 
Do you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfDo you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfivylinvaydak64229
 
Every year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfEvery year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfivylinvaydak64229
 
Describe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfDescribe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfivylinvaydak64229
 
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfDescribe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfivylinvaydak64229
 
Consider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfConsider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfivylinvaydak64229
 
Analyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfAnalyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfivylinvaydak64229
 
Collect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfCollect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfivylinvaydak64229
 
Assume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfAssume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfivylinvaydak64229
 
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfAre the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfivylinvaydak64229
 
A. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfA. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfivylinvaydak64229
 
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfA species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfivylinvaydak64229
 
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfA New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfivylinvaydak64229
 
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfA protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfivylinvaydak64229
 
What were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfWhat were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfivylinvaydak64229
 
write two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfwrite two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfivylinvaydak64229
 
Where might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfWhere might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfivylinvaydak64229
 
What single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfWhat single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfivylinvaydak64229
 
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfWhat are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfivylinvaydak64229
 

More from ivylinvaydak64229 (20)

For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
For the hypothesis test H0  = 5 against H1   5 with variance unkn.pdfFor the hypothesis test H0  = 5 against H1   5 with variance unkn.pdf
For the hypothesis test H0 = 5 against H1 5 with variance unkn.pdf
 
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdfEarly in 2017 scientists have discovered a new family of eukaryotic b.pdf
Early in 2017 scientists have discovered a new family of eukaryotic b.pdf
 
Do you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdfDo you believe great leaders are born or madeSolutioni believe.pdf
Do you believe great leaders are born or madeSolutioni believe.pdf
 
Every year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdfEvery year, the viral strains included in vaccinations for the flu a.pdf
Every year, the viral strains included in vaccinations for the flu a.pdf
 
Describe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdfDescribe the role of different types of genomic changes in the evolut.pdf
Describe the role of different types of genomic changes in the evolut.pdf
 
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdfDescribe the Darwinian theory of evolutionDescribe the Darwi.pdf
Describe the Darwinian theory of evolutionDescribe the Darwi.pdf
 
Consider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdfConsider any organization where you’ve worked in the past, where you.pdf
Consider any organization where you’ve worked in the past, where you.pdf
 
Analyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdfAnalyze the detected attacks and create a report that describes each.pdf
Analyze the detected attacks and create a report that describes each.pdf
 
Collect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdfCollect 50 or more paired quantitative data items. You may use a met.pdf
Collect 50 or more paired quantitative data items. You may use a met.pdf
 
Assume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdfAssume you have decided to implement DFS so remote sites can access .pdf
Assume you have decided to implement DFS so remote sites can access .pdf
 
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdfAre the following events SOURCES or USES of cashDecrease in Accou.pdf
Are the following events SOURCES or USES of cashDecrease in Accou.pdf
 
A. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdfA. What are two advantages that the use of green fluorescent protein.pdf
A. What are two advantages that the use of green fluorescent protein.pdf
 
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdfA species has a diploid number of 2n. Meiosis I fails during spermato.pdf
A species has a diploid number of 2n. Meiosis I fails during spermato.pdf
 
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdfA New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
A New Look at Bread and RosesIn Bread and Roses, Bruce Watson argu.pdf
 
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdfA protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
A protein, called PHD (protein for retinoblastoma) a synthesized by a.pdf
 
What were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdfWhat were the driving forces behind the creation of the FAA and ICAO.pdf
What were the driving forces behind the creation of the FAA and ICAO.pdf
 
write two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdfwrite two paragraphs on the polices to reduce income inequality and .pdf
write two paragraphs on the polices to reduce income inequality and .pdf
 
Where might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdfWhere might you find the gametophytes of… Where might you find the g.pdf
Where might you find the gametophytes of… Where might you find the g.pdf
 
What single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdfWhat single , unique characteristic of a protist would be conside.pdf
What single , unique characteristic of a protist would be conside.pdf
 
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdfWhat are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
What are the indications that Sarcodina, Apicomplexa and Ciliophora .pdf
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

implement in c++. This is a OS concept Implement the Ballroom Dancer.pdf

  • 1. 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 //Provides API for POSIX(or UNIX) OS for system calls #include #include #include //For exit() and rand() #include //Threading APIs #include //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
  • 2. 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 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
  • 3. 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); }