Write a program for synchronization of the cigarette smokers problem - descripti
ID: 3563757 • Letter: W
Question
Write a program for synchronization of the cigarette smokers problem - description as given in the link : http://en.wikipedia.org/wiki/Cigarette_smokers_problem. Model each agent (e.g. a producer, consumer, reader, philosopher, etc.) as a process, and each shared data structure using shared memory. The execution of the various active entities should be synchronized using semaphores.
General guidelines: . Design your code so that it is easy to demonstrate the race condition by omitting the entry_section and exit_section (i.e. if you leave out the entry_section and exit_section, your programme should exhibit the inconsistencies that arise from race conditions). . The behaviour of each agent should have an element of randomness (use a function like rand() along with sleep() in a suitable way for this). . Include suitable printf functions in each agent so that the sequence of operations performed by all the agents is clearly understandable.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <stdbool.h>
#include <sys/sem.h>
#include <sys/ipc.h>
// #define key = 1234
/*
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
struct sembuf {
ushort sem_num;
short sem_op;
short sem_flg;
};
*/
void SmokerTob(int semid){
struct sembuf sb_PaperMatch, sbAgent;
while(true){
printf("Somker(Tobacco) is waiting ");
sb_PaperMatch.sem_num = 0;
sb_PaperMatch.sem_op = -1;
sb_PaperMatch.sem_flg = SEM_UNDO;
semop(semid, &sb_PaperMatch,1);
sbAgent.sem_num = 3;
sbAgent.sem_op = 1;
sbAgent.sem_flg = SEM_UNDO;
semop(semid, &sbAgent,1);
printf("The table is now free - Smoker(Tobacco) ");
printf("Smoker(Tobacco) has started smoking ");
// delay(1000);
printf("Smoker(Tobacco) has completed smoking ");
}
}
void SmokerPap(int semid){
struct sembuf sb_MatchTobacco, sbAgent;
while(true){
printf("Smoker(Paper) is waiting ");
sb_MatchTobacco.sem_num = 1;
sb_MatchTobacco.sem_op = -1;
sb_MatchTobacco.sem_flg = SEM_UNDO;
semop(semid, &sb_MatchTobacco,1);
sbAgent.sem_num = 3;
sbAgent.sem_op = 1;
sbAgent.sem_flg = SEM_UNDO;
semop(semid, &sbAgent,1);
printf("The table is now free - SmokerPap ");
printf("Smoker(Paper) has started smoking ");
// delay(1000);
printf("Smoker(Paper) has completed smoking ");
}
}
void SmokerMatch(int semid){
struct sembuf sbAgent, sb_TobaccoPaper;
while(true){
printf("Smoker(match) is waiting ");
sb_TobaccoPaper.sem_num = 2;
sb_TobaccoPaper.sem_op = -1;
sb_TobaccoPaper.sem_flg = SEM_UNDO;
semop(semid, &sb_TobaccoPaper,1);
sbAgent.sem_num = 3;
sbAgent.sem_op = 1;
sbAgent.sem_flg = SEM_UNDO;
semop(semid, &sbAgent,1);
printf("The table is now free - Smoker(Match) ");
printf("Smoker(match) has started smoking ");
// delay(1000);
printf("Smoker(match) has completed smoking ");
}
}
void Agent(int noOfIter, int semid){
struct sembuf sbAgent, sb_PaperMatch, sb_MatchTobacco, sb_TobaccoPaper;
int i = 0;
for(i = 1; i <= noOfIter; i++){
printf("%d ", i);
// srand(time(0));
int select_no;
printf("Agent waiting for the table to be free ");
sbAgent.sem_num = 3;
sbAgent.sem_op = -1;
sbAgent.sem_flg = SEM_UNDO;
semop(semid, &sbAgent,1);
printf("Agent recognized that the table is free ");
select_no = rand() % 3;
if(select_no == 0){
sb_PaperMatch.sem_num = 0;
sb_PaperMatch.sem_op = 1;
sb_PaperMatch.sem_flg = SEM_UNDO;
semop(semid, &sb_PaperMatch,1);
printf("paper and match placed on the table ");
}
else if(select_no == 1){
sb_MatchTobacco.sem_num = 1;
sb_MatchTobacco.sem_op = 1;
sb_MatchTobacco.sem_flg = SEM_UNDO;
semop(semid, &sb_MatchTobacco,1);
printf("Match and Tobacco placed on the table ");
}
else if (select_no == 2){
sb_TobaccoPaper.sem_num = 2;
sb_TobaccoPaper.sem_op = 1;
sb_TobaccoPaper.sem_flg = SEM_UNDO;
semop(semid, &sb_TobaccoPaper,1);
printf("Tobacco and Paper placed on the table ");
}
}
}
int main(){
int semid;
key_t key1 = 1234;
semid = semget(key1,4,0666 | IPC_CREAT);
union semun arg0, arg1,arg2,arg3;
arg0.val = 0;
arg1.val = 0;
arg2.val = 0;
arg3.val = 1;
semctl(semid, 0, SETVAL, arg0);
semctl(semid, 1, SETVAL, arg1);
semctl(semid, 2, SETVAL, arg2);
semctl(semid, 3, SETVAL, arg3);
// sem_init(&PaperMatch,1,0);
// sem_init(&MatchTobacco,1,0);
// sem_init(&TobaccoPaper,1,0);
// sem_init(&Table,1,1);
int noOfIter;
printf("Enter the number of iterations: ");
scanf("%d", &noOfIter);
printf("PROCESS STARTED ");
pid_t pid_SmokerTobacco, pid_SmokerPaper, pid_SmokerMatch;
pid_SmokerTobacco = fork();
if ( pid_SmokerTobacco == 0)
{
SmokerTob(semid);
}
else if(pid_SmokerTobacco > 0)
{
pid_SmokerMatch = fork();
if(pid_SmokerMatch == 0)
SmokerMatch(semid);
else if (pid_SmokerMatch > 0)
{
pid_SmokerPaper = fork();
if (pid_SmokerPaper == 0)
SmokerPap(semid);
else if (pid_SmokerPaper > 0)
{
Agent(noOfIter, semid);
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.