-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathpctest.cpp
More file actions
153 lines (132 loc) · 3.36 KB
/
pctest.cpp
File metadata and controls
153 lines (132 loc) · 3.36 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by jxq on 19-8-15.
//
// p39 posix信号量与互斥锁
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <semaphore.h>
#include <pthread.h>
using namespace std;
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
#define CONSUMERS_COUNT 1
#define PRODUCERS_COUNT 5
#define BUFFSIZE 10
int g_buffer[BUFFSIZE];
unsigned short in = 0;
unsigned short out = 0;
unsigned short produce_id = 0;
unsigned short consume_id = 0;
sem_t g_sem_full;
sem_t g_sem_empth;
pthread_mutex_t g_mutex;
pthread_t g_thread[CONSUMERS_COUNT + PRODUCERS_COUNT];
void *produce (void *arg)
{
int num = *((int *)arg);
int i;
while (1)
{
printf("%d produce is waiting\n", num);
sem_wait(&g_sem_full);
pthread_mutex_lock(&g_mutex);
for (i = 0; i < BUFFSIZE; ++i)
{
printf("%02d ", i);
if (g_buffer[i] == -1)
{
printf("%s ", "null");
}
else
{
printf("%d ", g_buffer[i]);
}
if (i == in)
{
printf("\t<--produce");
}
printf("\n");
}
printf("%d produce begin produce product %d\n", num, produce_id);
g_buffer[in] = produce_id;
cout << g_buffer[in] << endl;
in = (in + 1) % BUFFSIZE;
printf("%d produce end produce product %d\n", num, produce_id++);
pthread_mutex_unlock(&g_mutex);
sem_post(&g_sem_empth);
sleep(5);
}
return NULL;
}
void *consume (void *arg)
{
int num = *((int *)arg);
int i;
while (1)
{
printf("%d consume is waiting\n", num);
sem_wait(&g_sem_empth);
pthread_mutex_lock(&g_mutex);
for (i = 0; i < BUFFSIZE; ++i)
{
printf("%02d ", i);
if (g_buffer[i] == -1)
{
printf("%s", "null");
}
else
{
printf("%d", g_buffer[i]);
}
if (i == out)
{
printf("\t<--consume");
}
printf("\n");
}
consume_id = g_buffer[out];
printf("%d consume begin consume product %d\n", num, consume_id);
g_buffer[out] = -1;
out = (out + 1) % BUFFSIZE;
printf("%d consume end consume product %d\n", num, consume_id);
pthread_mutex_unlock(&g_mutex);
sem_post(&g_sem_full);
sleep(1);
}
return NULL;
}
int main(int argc, char** argv)
{
for (int i = 0; i < BUFFSIZE; ++i)
{
g_buffer[i] = -1;
}
sem_init(&g_sem_full, NULL, BUFFSIZE);
sem_init(&g_sem_empth, NULL, 0);
pthread_mutex_init(&g_mutex, NULL);
int i;
for (i = 0; i < CONSUMERS_COUNT; ++i)
{
pthread_create(&g_thread[i], NULL, consume, &i);
}
for (i = 0; i < PRODUCERS_COUNT; ++i)
{
pthread_create(&g_thread[CONSUMERS_COUNT+i], NULL, produce, &i);
}
for (i = 0; i < CONSUMERS_COUNT + PRODUCERS_COUNT; ++i)
{
pthread_join(g_thread[i], NULL);
}
sem_destroy(&g_sem_full);
sem_destroy(&g_sem_empth);
pthread_mutex_destroy(&g_mutex);
return 0;
}