-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
138 lines (126 loc) · 3.68 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <sys/syscall.h>
#include <linux/kernel.h>
sem_t required;
sem_t match;
sem_t paper;
sem_t tobacco;
void *agent ()
{
int i=0;
int x=1;
int s=0,p=0,m=0;
while (1)
{
int number = rand() % 3;
if(i==10)
{
printf("\n\nNumber of times the smoker smoked a cigarette in total: %d\n",s);
printf("\n Total number of times smoker has smoked using paper as a smoking material: %d\n",p);
printf("\nTotal number of times that someone has smoked with match: %d\n",m);
exit(0);
}
sleep(1);
switch (number)
{
case 0: sem_post (&match); /* match and paper */
sem_post (&paper);
syscall(333,"The agent has placed the match onto the table\n");
printf("Agent has put match and paper on the table\n");
printf("SMOKING A CIGAR %d time\n", x++);
s++;
break;
case 1: sem_post (&match); /* match and tobacco */
sem_post (&tobacco);
syscall(333,"The agent has placed the match and tobacco onto the table\n");
printf("Agent has put match and tobacco on the table\n");
printf("SMOKING A CIGAR %d time\n", x++);
p++;
break;
case 2: sem_post (&paper); /* tobacco and paper */
sem_post (&tobacco);
syscall(333,"The agent has placed paper and tobacco onto the table\n");
printf("Agent has put paper and tobacco on the table\n");
printf("SMOKING A CIGAR %d time\n", x++);
m++;
break;
}
sem_wait (&required); /* wait for request for more */
i++;
}
}
void *smoker_with_tobacco ()
{
while (1)
{
sem_wait (&match); /* grab match from table */
if (sem_trywait (&paper) == 0) /* grab paper */
{
/* roll cigarette and smoke */
syscall(333,"Retrieve Match and Paper");
syscall(333,"Smoker with Tobacco is Smoking\n");
printf("Smoker with tobacco smoking\n");
sleep(0.5);
sem_post (&required); /* signal to agent */
}
else sem_post (&match); /* drop the match */
}
}
void *smoker_with_match ()
{
while (1)
{
sem_wait (&paper); /* grab match from table */
if (sem_trywait (&tobacco) == 0) /* grab paper */
{
/* roll cigarette and smoke */
syscall(333,"Reterieve Tobacco and Paper");
syscall(333,"Smoker with Match is Smoking\n");
printf("Smoker with match smoking\n");
sleep(0.5);
sem_post (&required); /* signal to agent */
}
else sem_post (&paper); /* drop the match */
}
}
void *smoker_with_paper ()
{
while (1)
{
sem_wait (&tobacco); /* grab match from table */
if (sem_trywait (&match) == 0) /* grab paper */
{
/* roll cigarette and smoke */
syscall(333,"Retreieve Tobacco and Paper");
syscall(333,"Smoker with Paper is Smoking\n");
printf("Smoker with paper smoking\n");
sleep(0.5);
sem_post (&required); /* signal to agent */
}
else sem_post (&tobacco); /* drop the match */
}
}
int main() {
pthread_t thread1, thread2, thread3, thread4;
sem_init(&required,0,1);
sem_init(&match,0,0);
sem_init(&paper,0,0);
sem_init(&tobacco,0,0);
pthread_create(&thread1,NULL,agent,NULL);
pthread_create(&thread2,NULL,smoker_with_tobacco,NULL);
pthread_create(&thread3,NULL,smoker_with_paper,NULL);
pthread_create(&thread4,NULL,smoker_with_match,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_join(thread3,NULL);
pthread_join(thread4,NULL);
return 0;
}