Laynetworks  
Web laynetworks.com Google
Home | Site Map | Tell a friends
Journal of Management
Management Tutorials
Download
Tutorials
History
Computer Science
Networking
OS - Linux and Unix
Source Code
Script & Languages
Protocols
Glossary
IGNOU
Quiz
About Us
Contact Us
Feedback
 
Sign up for our Email Newsletter
 
Get Paid for Your Tech Turorials / Tips

 

 

Home > Computer Science > Operating Systems
 
CS 01 CS 02 CS 03 CS 04 CS 05 CS 06 CS 07 CS 08 CS 09 CS 10 CS 11 CS 12 CS 13 CS 14 CS 15 CS 16 CS 17
Page : 1 2 3 4 5 6
Operating Systems
 
The Cigarette-Smokers Problem.
contributed by Ramya
 

Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats. Write a program to synchronize the agent and the smokers.

There are four processes in the system. Three represent the smokers, and one represents the supplier.

/* The Cigarette-smokers problem uses semaphores to solve. */
typedef int semaphore;
semaphore items=1; /*used for mutual exclusive access to the table on which the two ingredients are placed */
semaphore more=0; 
semaphore temp=0; /* used to queue the waiting smokers */
int count =0; /*indicates the number is of waiting smokers */
boolean flags[0..2]=initially all false;
/*a flag true indicates if the corresponding item is on the table */
/* the three items needed for smoking are named as 0,1,2 */
/* process i has item i but needs the other two items, i.e (i-1) mod2 and (i+1)mod 2*/
for 0<=i<=2;
Smoker process i;
{
repeat
wait(items); /*enter critical section */
if (flag[i-1 % 2] and flag[+1 % 2])
{
flag[i-1 % 2]=false; 
flag[i+1 % 2]=false;
SMOKE;
While(count >0)do
{
count --;
signal(temp);
}
signal(more);
}else{ /*both items needed for the smoking are not available */
count++;
signal(items);
wait(temp); /*wait for the next round */
}
until false;
}
Supplier process:
{
repeat
put any two items on the table and set the 
corresponding flags to true;
signal(items);
wait(more);
until false;
}

A full C program dealing with smokers problem is as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
enum Ingredients /* Enum representing the ingredients */
{
None,
Paper,
Tobacco,
Matches
};
/* Structure representing a Smoker & Agent process */
typedef struct smoker

char SmokerID[25];
int Item;
}SMOKER;
typedef struct agent
{
char AgentID[25];
int Item1;
int Item2;
}AGENT;
char* GetIngredientName(int Item)
{
if(Item == Paper)
return "Paper";
else if(Item == Tobacco)
return "Tobacco";
else if(Item == Matches)
return "Matches";
}
void GetAgentIngredients(AGENT* agent)
{
/* Simulate random generation of ingredients*/
agent->Item1=random(3)+1;
while(1)
{
agent->Item2=random(3)+1;
if(agent->Item1 != agent->Item2)
break;
}
printf("\nAgent Provides Ingredients- %s,%s\n\n:,
GetIngredientName(agent->Item1),GetIngredientName(agent->Item2));
}
void GiveIngredientToSmoker(AGENT*agent,SMOKER* smoker)
{
int index=0;
while(smoker[index].Item !=NULL)
{
if((smoker[index].Item !=agent->Item1)&&(smoker[index].Item != 
agent->Item2));
{
printf("\nSmoker - \%s\"-is smoking his cigarette\n\n", smoker[index].SmokerID);
agent->Item1=None;
agent->Item2=None;
break;
}
index++;
}
}
void main()
{
/*Create the processes required -1 Agent, 3 Smokers */
AGENT agent;
SMOKER smoker[4] = {{SmokerWithPaper",Paper},
{"SmokerWithTobacco",Tobacco},
{"SmokerWithMatches",Matches},{"\0",None}};
int userChoice=0;
strcpy(agent.AgentID,"Agent");
agent.Item1=None;
agent>item2=None;
while(1)
{
GetAgentIngredients(&agent);
GiveIngredientToSmoker(&agent,smoker);
printf("Press ESC to exit or any key to continue\n\n");
UserChoice=getch();
If(UserChoice ==27)
break;
}
}/*Program Ends*/

{ Problem: }

{ Deadlock occurs if you use a semaphore for the individual ingredients. }

{ Solution: }

{ Use a semaphore for each combination of ingredients. }

 
VAR

tobacco_paper, tobacco_matches, paper_matches, done_smoking: SEMAPHORE;

PROCEDURE vendor
BEGIN
WHILE (making_money) DO
        BEGIN
            CASE (random(1, 3)) OF
                1:
                    signal(tobacco_paper);
                2:
                    signal(tobacco_matches);
                3:
                    signal(paper_matches);
            END;
            wait(done_smoking);
        END;
END;
 
PROCEDURE smoker1   { This smoker has matches }
BEGIN
    WHILE (not_dead) DO
        BEGIN
            wait(tobacco_paper);
            smoke;
            signal(done_smoking);
        END;
END;
 
PROCEDURE smoker2   { This smoker has paper }
BEGIN
    WHILE (addicted_to_nicotine) DO
        BEGIN
            wait(tobacco_matches);
            smoke;
            signal(done_smoking);
        END;
END;

 

PROCEDURE smoker3 { This smoker has tobacco }

BEGIN
WHILE (can_inhale) DO
BEGIN
wait(paper_matches); 
smoke;
signal(done_smoking);
END;
END; 

BEGIN
tobacco_paper := 0;
tobacco_matches := 0;
paper_matches := 0;
done_smoking := 0;

END.

 
  1. RSA ALGORITHM
  2. RSA MD5Source Code(rsa algorithm in Visual C++ source code)
  3. RSA Algorithm program in JAVA (RSA Signature and Calculations) by Orlin from his java cryptography page
  4. RSA Algorithm Implementation in C language
  5. The Cigarette-Smokers Problem.
The Cigarette-smokers problem: Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three ingredients: tobacco, paper and matches. One of the smoker processes has an infinite supply of all the three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes the smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats. Write a program to synchronize the agent and the smokers.
TOP
 
Page : 1 2 3 4 5 6