SlideShare a Scribd company logo
1 of 4
Download to read offline
Write a C program to simulate the description below using semaphores to synchronize the
processes/threads.
A group of fraternity brother and sorority sisters is having a party and drinking from a large
communal keg that can hold N servings of soda. When a partier wants to drink, he or she fills a
cup from keg, unless it is empty. If the keg is empty, the partier wakes up the pledge and then
waits until the pledge has returned with a new keg.
Solution
#include
#include
#include
#include
#include
#include
#include
#define MAX 10
//Keg which holds no of servings
int N = MAX;
int brothers,sisters,i=0;
int Bservings,Sservings;
void pledge(int signo);
int main()
{
int pidb,pids;
signal( SIGUSR1, pledge ); //instal signal handler
//for semaphore declaration
int sem_id,j; /* semid of semaphore set */
key_t key = 1234; /* key to pass to semget() */
int nsems = 1; /* nsems to pass to semget() */
//for semaphore operation
struct sembuf sop;
sem_id=semget(key,nsems,IPC_CREAT|0666);
printf("No of brothers = ");
scanf("%d",&brothers);
printf("No of sisters = ");
scanf("%d",&sisters);
//create no of brothers
while(brothers)
{
pidb = fork();
if( pidb > 0 )
{
printf("Parent process ");
sop.sem_num = 0;
sop.sem_op = -1;
sop.sem_flg = 0;
if (semop(sem_id, &sop, 1))
{
perror("Could not increment semaphore");
exit(5);
}
kill(getpid(),SIGUSR1);
for(j=0; j=0 && Bservings<= N)
{
++Bservings;
N-=Bservings;
printf("N = %d ",N);
printf("Servings = %d ",Bservings);
printf("Brother%d has taken %d servings ",i,Bservings);
}
else
{
printf("Brorhes else ");
kill(getpid(),SIGUSR1);
sop.sem_num = 0;
sop.sem_op = -1;
sop.sem_flg = 0;
if (semop(sem_id, &sop, 1))
{
perror("Could not increment semaphore");
exit(5);
}
}
}
--brothers;
}
//create no of brothers
i = 0;
while(sisters)
{
pids = fork();
if( pidb > 0 )
{
printf("Parent process ");
for(j=0; j=0 && Sservings<= N)
{
++Sservings;
N-=Sservings;
printf("N = %d ",N);
printf("Servings = %d ",Sservings);
printf("Brother%d has taken %d servings ",i,Sservings);
}
else
{
printf("sisters else ");
kill(getpid(),SIGUSR1);
sop.sem_num = 0;
sop.sem_op = -1;
sop.sem_flg = 0;
if (semop(sem_id, &sop, 1))
{
perror("Could not increment semaphore");
exit(5);
}
}
}
--sisters;
}
}
//if empty execute pledge
void pledge(int signo)
{
//for semaphore operation
struct sembuf sop;
int sem_id; /* semid of semaphore set */
key_t key = 1234; /* key to pass to semget() */
int nsems = 1; /* nsems to pass to semget() */
sem_id=semget(key,nsems,0666);
if (signo == SIGUSR1)
printf("received SIGUSR1 ");
else if (signo == SIGKILL)
printf("received SIGKILL ");
else if (signo == SIGSTOP)
printf("received SIGSTOP ");
//set keg to new value
N = MAX;
sop.sem_num = 0;
sop.sem_op = 1;
sop.sem_flg = 0;
if (semop(sem_id, &sop, 1))
{
perror("Could not increment semaphore");
exit(5);
}
printf("Exiting pledge after setting keg to MAX value");
}

More Related Content

Similar to Write a C program to simulate the description below using semaphores.pdf

PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxAshutoshprasad27
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C LabNeil Mathew
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdfMOJO89
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfgiriraj65
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言Simen Li
 
Please correct my code for me i am lost Thanks i submitted.pdf
Please correct my code for me i am lost Thanks i submitted.pdfPlease correct my code for me i am lost Thanks i submitted.pdf
Please correct my code for me i am lost Thanks i submitted.pdfkitty811
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxjacksnathalie
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY MazedurMazedurr rahman
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdffortmdu
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman KondakciSüleyman Kondakci
 
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
 
Dtrace и немного магии
Dtrace и немного магииDtrace и немного магии
Dtrace и немного магииDan Kruchinin
 

Similar to Write a C program to simulate the description below using semaphores.pdf (20)

PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdf
 
C
CC
C
 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdf
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Yg byev2e
Yg byev2eYg byev2e
Yg byev2e
 
Please correct my code for me i am lost Thanks i submitted.pdf
Please correct my code for me i am lost Thanks i submitted.pdfPlease correct my code for me i am lost Thanks i submitted.pdf
Please correct my code for me i am lost Thanks i submitted.pdf
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docx
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
evil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdfevil_server.cpp#include string #include cstdlib #include.pdf
evil_server.cpp#include string #include cstdlib #include.pdf
 
Programming egs
Programming egs Programming egs
Programming egs
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
 
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
 
Dtrace и немного магии
Dtrace и немного магииDtrace и немного магии
Dtrace и немного магии
 
Presentation1
Presentation1Presentation1
Presentation1
 

More from federaleyecare

A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdf
A ISCSI SAN consists of  Select one or more  A. HBA  B. LUN  C. Fibr.pdfA ISCSI SAN consists of  Select one or more  A. HBA  B. LUN  C. Fibr.pdf
A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdffederaleyecare
 
Which of the following statements about tRNA molecules is TRUEMul.pdf
Which of the following statements about tRNA molecules is TRUEMul.pdfWhich of the following statements about tRNA molecules is TRUEMul.pdf
Which of the following statements about tRNA molecules is TRUEMul.pdffederaleyecare
 
When analyzing any particular primary source, historians must consid.pdf
When analyzing any particular primary source, historians must consid.pdfWhen analyzing any particular primary source, historians must consid.pdf
When analyzing any particular primary source, historians must consid.pdffederaleyecare
 
What is Linux SecuritySolutionLinux Security is a module in.pdf
What is Linux SecuritySolutionLinux Security is a module in.pdfWhat is Linux SecuritySolutionLinux Security is a module in.pdf
What is Linux SecuritySolutionLinux Security is a module in.pdffederaleyecare
 
What is the runtime complexity of Adams famous string splitter cod.pdf
What is the runtime complexity of Adams famous string splitter cod.pdfWhat is the runtime complexity of Adams famous string splitter cod.pdf
What is the runtime complexity of Adams famous string splitter cod.pdffederaleyecare
 
What does personalized learning mean to you What does personal.pdf
What does personalized learning mean to you What does personal.pdfWhat does personalized learning mean to you What does personal.pdf
What does personalized learning mean to you What does personal.pdffederaleyecare
 
What do patients in healthcare expect from their insurance company.pdf
What do patients in healthcare expect from their insurance company.pdfWhat do patients in healthcare expect from their insurance company.pdf
What do patients in healthcare expect from their insurance company.pdffederaleyecare
 
Use Table A to find the number z such that the proportion of observa.pdf
Use Table A to find the number z such that the proportion of observa.pdfUse Table A to find the number z such that the proportion of observa.pdf
Use Table A to find the number z such that the proportion of observa.pdffederaleyecare
 
Two of the main political systems are democracy and totalitarianism..pdf
Two of the main political systems are democracy and totalitarianism..pdfTwo of the main political systems are democracy and totalitarianism..pdf
Two of the main political systems are democracy and totalitarianism..pdffederaleyecare
 
The procedure of transferring journal entries to the ledger accounts .pdf
The procedure of transferring journal entries to the ledger accounts .pdfThe procedure of transferring journal entries to the ledger accounts .pdf
The procedure of transferring journal entries to the ledger accounts .pdffederaleyecare
 
The ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdfThe ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdffederaleyecare
 
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdffederaleyecare
 
QUESTION 7 When using the indirect method to prepare the statement of.pdf
QUESTION 7 When using the indirect method to prepare the statement of.pdfQUESTION 7 When using the indirect method to prepare the statement of.pdf
QUESTION 7 When using the indirect method to prepare the statement of.pdffederaleyecare
 
Q.1. Define noncontrolling (minority) interest. List three methods th.pdf
Q.1. Define noncontrolling (minority) interest. List three methods th.pdfQ.1. Define noncontrolling (minority) interest. List three methods th.pdf
Q.1. Define noncontrolling (minority) interest. List three methods th.pdffederaleyecare
 
Picking a fruit-flavored or primary colored bean. overlapping event .pdf
Picking a fruit-flavored or primary colored bean.  overlapping event .pdfPicking a fruit-flavored or primary colored bean.  overlapping event .pdf
Picking a fruit-flavored or primary colored bean. overlapping event .pdffederaleyecare
 
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdfPart F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdffederaleyecare
 
Match the following Data vs Information Reliability as it adds value .pdf
Match the following Data vs Information Reliability as it adds value .pdfMatch the following Data vs Information Reliability as it adds value .pdf
Match the following Data vs Information Reliability as it adds value .pdffederaleyecare
 
Many organizations invest substantial resources in creating their ow.pdf
Many organizations invest substantial resources in creating their ow.pdfMany organizations invest substantial resources in creating their ow.pdf
Many organizations invest substantial resources in creating their ow.pdffederaleyecare
 
Lets compare and contrast the national cultures of Egypt and Brazi.pdf
Lets compare and contrast the national cultures of Egypt and Brazi.pdfLets compare and contrast the national cultures of Egypt and Brazi.pdf
Lets compare and contrast the national cultures of Egypt and Brazi.pdffederaleyecare
 
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdf
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdfGreat alveolar (type II) cells of the alveoli secrete a _______ t.pdf
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdffederaleyecare
 

More from federaleyecare (20)

A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdf
A ISCSI SAN consists of  Select one or more  A. HBA  B. LUN  C. Fibr.pdfA ISCSI SAN consists of  Select one or more  A. HBA  B. LUN  C. Fibr.pdf
A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdf
 
Which of the following statements about tRNA molecules is TRUEMul.pdf
Which of the following statements about tRNA molecules is TRUEMul.pdfWhich of the following statements about tRNA molecules is TRUEMul.pdf
Which of the following statements about tRNA molecules is TRUEMul.pdf
 
When analyzing any particular primary source, historians must consid.pdf
When analyzing any particular primary source, historians must consid.pdfWhen analyzing any particular primary source, historians must consid.pdf
When analyzing any particular primary source, historians must consid.pdf
 
What is Linux SecuritySolutionLinux Security is a module in.pdf
What is Linux SecuritySolutionLinux Security is a module in.pdfWhat is Linux SecuritySolutionLinux Security is a module in.pdf
What is Linux SecuritySolutionLinux Security is a module in.pdf
 
What is the runtime complexity of Adams famous string splitter cod.pdf
What is the runtime complexity of Adams famous string splitter cod.pdfWhat is the runtime complexity of Adams famous string splitter cod.pdf
What is the runtime complexity of Adams famous string splitter cod.pdf
 
What does personalized learning mean to you What does personal.pdf
What does personalized learning mean to you What does personal.pdfWhat does personalized learning mean to you What does personal.pdf
What does personalized learning mean to you What does personal.pdf
 
What do patients in healthcare expect from their insurance company.pdf
What do patients in healthcare expect from their insurance company.pdfWhat do patients in healthcare expect from their insurance company.pdf
What do patients in healthcare expect from their insurance company.pdf
 
Use Table A to find the number z such that the proportion of observa.pdf
Use Table A to find the number z such that the proportion of observa.pdfUse Table A to find the number z such that the proportion of observa.pdf
Use Table A to find the number z such that the proportion of observa.pdf
 
Two of the main political systems are democracy and totalitarianism..pdf
Two of the main political systems are democracy and totalitarianism..pdfTwo of the main political systems are democracy and totalitarianism..pdf
Two of the main political systems are democracy and totalitarianism..pdf
 
The procedure of transferring journal entries to the ledger accounts .pdf
The procedure of transferring journal entries to the ledger accounts .pdfThe procedure of transferring journal entries to the ledger accounts .pdf
The procedure of transferring journal entries to the ledger accounts .pdf
 
The ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdfThe ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdf
 
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
 
QUESTION 7 When using the indirect method to prepare the statement of.pdf
QUESTION 7 When using the indirect method to prepare the statement of.pdfQUESTION 7 When using the indirect method to prepare the statement of.pdf
QUESTION 7 When using the indirect method to prepare the statement of.pdf
 
Q.1. Define noncontrolling (minority) interest. List three methods th.pdf
Q.1. Define noncontrolling (minority) interest. List three methods th.pdfQ.1. Define noncontrolling (minority) interest. List three methods th.pdf
Q.1. Define noncontrolling (minority) interest. List three methods th.pdf
 
Picking a fruit-flavored or primary colored bean. overlapping event .pdf
Picking a fruit-flavored or primary colored bean.  overlapping event .pdfPicking a fruit-flavored or primary colored bean.  overlapping event .pdf
Picking a fruit-flavored or primary colored bean. overlapping event .pdf
 
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdfPart F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
 
Match the following Data vs Information Reliability as it adds value .pdf
Match the following Data vs Information Reliability as it adds value .pdfMatch the following Data vs Information Reliability as it adds value .pdf
Match the following Data vs Information Reliability as it adds value .pdf
 
Many organizations invest substantial resources in creating their ow.pdf
Many organizations invest substantial resources in creating their ow.pdfMany organizations invest substantial resources in creating their ow.pdf
Many organizations invest substantial resources in creating their ow.pdf
 
Lets compare and contrast the national cultures of Egypt and Brazi.pdf
Lets compare and contrast the national cultures of Egypt and Brazi.pdfLets compare and contrast the national cultures of Egypt and Brazi.pdf
Lets compare and contrast the national cultures of Egypt and Brazi.pdf
 
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdf
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdfGreat alveolar (type II) cells of the alveoli secrete a _______ t.pdf
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdf
 

Recently uploaded

diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 

Recently uploaded (20)

diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 

Write a C program to simulate the description below using semaphores.pdf

  • 1. Write a C program to simulate the description below using semaphores to synchronize the processes/threads. A group of fraternity brother and sorority sisters is having a party and drinking from a large communal keg that can hold N servings of soda. When a partier wants to drink, he or she fills a cup from keg, unless it is empty. If the keg is empty, the partier wakes up the pledge and then waits until the pledge has returned with a new keg. Solution #include #include #include #include #include #include #include #define MAX 10 //Keg which holds no of servings int N = MAX; int brothers,sisters,i=0; int Bservings,Sservings; void pledge(int signo); int main() { int pidb,pids; signal( SIGUSR1, pledge ); //instal signal handler //for semaphore declaration int sem_id,j; /* semid of semaphore set */ key_t key = 1234; /* key to pass to semget() */ int nsems = 1; /* nsems to pass to semget() */ //for semaphore operation struct sembuf sop; sem_id=semget(key,nsems,IPC_CREAT|0666);
  • 2. printf("No of brothers = "); scanf("%d",&brothers); printf("No of sisters = "); scanf("%d",&sisters); //create no of brothers while(brothers) { pidb = fork(); if( pidb > 0 ) { printf("Parent process "); sop.sem_num = 0; sop.sem_op = -1; sop.sem_flg = 0; if (semop(sem_id, &sop, 1)) { perror("Could not increment semaphore"); exit(5); } kill(getpid(),SIGUSR1); for(j=0; j=0 && Bservings<= N) { ++Bservings; N-=Bservings; printf("N = %d ",N); printf("Servings = %d ",Bservings); printf("Brother%d has taken %d servings ",i,Bservings); } else { printf("Brorhes else "); kill(getpid(),SIGUSR1); sop.sem_num = 0; sop.sem_op = -1; sop.sem_flg = 0; if (semop(sem_id, &sop, 1))
  • 3. { perror("Could not increment semaphore"); exit(5); } } } --brothers; } //create no of brothers i = 0; while(sisters) { pids = fork(); if( pidb > 0 ) { printf("Parent process "); for(j=0; j=0 && Sservings<= N) { ++Sservings; N-=Sservings; printf("N = %d ",N); printf("Servings = %d ",Sservings); printf("Brother%d has taken %d servings ",i,Sservings); } else { printf("sisters else "); kill(getpid(),SIGUSR1); sop.sem_num = 0; sop.sem_op = -1; sop.sem_flg = 0; if (semop(sem_id, &sop, 1)) { perror("Could not increment semaphore"); exit(5);
  • 4. } } } --sisters; } } //if empty execute pledge void pledge(int signo) { //for semaphore operation struct sembuf sop; int sem_id; /* semid of semaphore set */ key_t key = 1234; /* key to pass to semget() */ int nsems = 1; /* nsems to pass to semget() */ sem_id=semget(key,nsems,0666); if (signo == SIGUSR1) printf("received SIGUSR1 "); else if (signo == SIGKILL) printf("received SIGKILL "); else if (signo == SIGSTOP) printf("received SIGSTOP "); //set keg to new value N = MAX; sop.sem_num = 0; sop.sem_op = 1; sop.sem_flg = 0; if (semop(sem_id, &sop, 1)) { perror("Could not increment semaphore"); exit(5); } printf("Exiting pledge after setting keg to MAX value"); }