-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcig.c
82 lines (79 loc) · 1.61 KB
/
cig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string.h>
#include <stdlib.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=rand() % 4 + 1;
while(1)
{
agent->Item2=rand() % 4 + 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;
}
}
s