forked from huannguyen2008/os2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.practical.work.pthread.c
111 lines (82 loc) · 1.52 KB
/
08.practical.work.pthread.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <pthread.h>
#define BUFFER_SIZE 10
int pid =0;
typedef struct product
{
char type;
int amount;
char unit;
} item;
item buffer[BUFFER_SIZE];
int first = 0;
int last = 0;
void initBeef(item *smt)
{
smt->type = 1;
smt->amount = 1;
smt->unit = 1;
}
void initBurger(item *smt)
{
smt->type =0;
smt->amount=0;
smt->amount=0;
}
void produce(item *i)
{
while ((first + 1) % BUFFER_SIZE == last)
{
printf("No buffer item!!!!!\n");
return;
}
memcpy(&buffer[first], i, sizeof(item));
first = (first + 1) % BUFFER_SIZE;
printf("First = %d\n", first);
}
item *consume()
{
item *i = malloc(sizeof(item));
while (first == last)
{
printf("Nothing to consume!\n");
}
memcpy(i, &buffer[last], sizeof(item));
last = (last + 1) % BUFFER_SIZE;
printf("Last = %d\n", last);
return;
}
void *produThread(void* param)
{
item *Beef, gram;
Beef = &gram;
printf("3 Grams of beef please !!!\n");
initBeef(Beef);
produce(Beef);
produce(Beef);
produce(Beef);
pthread_exit(NULL);
}
void *consuThread(void* param)
{
printf("Selling 2 grams of beef\n");
consume();
consume();
pthread_exit(NULL);
}
int main()
{
pthread_t tid;
printf("WELCOME TO THE BEEF-HOUSE !!\n");
printf("HOPE YOU ENJOY IT\n");
pthread_create(&tid,NULL,produThread,NULL);
printf("2 customers are coming to buy beef");
pthread_create(&tid,NULL,consuThread,NULL);
pthread_join(tid,NULL);
pthread_join(tid,NULL);
pthread_exit(NULL);
}