![](https://csdnimg.cn/release/download_crawler_static/86325167/bg5.jpg)
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
#define CONSUMER 1
int iflag = 0;
int oflag = 0;
sem_t empty, full, mutex;
int empty_count, full_count;
int data_num = 0;
int num = 0;
int buffer[BUFFER_SIZE] = {};
int p_task_done = -1;
int c_task_done = -1;
struct arg_struct {
arg_struct(int _id, int _start, int _work, string _indent): id(_id),
start(_start), work(_work), indent(_indent) {}
arg_struct(int _id): id(_id), start(0), work(0), indent(string("")) {}
int id;
int start;
int work;
string indent;
};
void* producer(void* argv){
arg_struct arg = *(arg_struct*)argv;
int id = arg.id;
const char* indent = arg.indent.c_str();
sleep(arg.start);
printf("%sSTART\n", indent);
sem_wait(&mutex); /* 顺序错了 */
printf("%saMUTEX\n", indent);
sem_wait(&empty); /* 顺序错了 */
printf("%saEMPTY\n", indent);
printf("%sENTER\n", indent);
int time = rand() % SLEEP_SPAN;
sleep(arg.work);
p_task_done++;
printf("%sProd %d\n", indent, p_task_done);