SlideShare a Scribd company logo
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.docx
Ashutoshprasad27
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
Neil Mathew
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdf
MOJO89
 
C
CC
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
Sarkunavathi Aribal
 
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
giriraj65
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
Simen Li
 
Cpds lab
Cpds labCpds lab
Yg byev2e
Yg byev2eYg byev2e
Yg byev2e
longphi2812
 
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
kitty811
 
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
jacksnathalie
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
Export Promotion Bureau
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
Mazedurr 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.pdf
fortmdu
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
Rahul Pandit
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
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
marketing413921
 
Dtrace и немного магии
Dtrace и немного магииDtrace и немного магии
Dtrace и немного магии
Dan Kruchinin
 
Presentation1
Presentation1Presentation1
Presentation1
daisy_arcangel
 

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.pdf
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 
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
federaleyecare
 

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

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 

Recently uploaded (20)

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 

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"); }