オペレーティングシステムシミュレーション生産者消費者問題
3476 ワード
pv操作及び生産者消費者問題の解析
#include
#include
#include
#include
#include
void procducer();
void consumer();
int buffer[10];//
int count = 0;//
int length = 10;//
int main() {
memset(buffer, 0, sizeof(buffer));//
while(1) {
Sleep(500);
srand(time(0));
int a = rand() % (21) + 10;// 10 30
if (a % 2) consumer();//
else procducer();//
//printf("%d
", a);
}
}
void consumer() {
if (count > 0) {
for (int i = 0; i < length; i++) {
if (buffer[i] == 1) {
buffer[i] = 0;
count--;
printf("
, %d
", count);
break;
}
}
}else {
printf("
");
}
}
void procducer() {
int i;
int judge = 0;
for (i = 0; i < length; i++) {
if (buffer[i] == 0) {
judge = 1;
break;
}
}
if (judge == 0) printf("
10
");
if (judge == 1) {
buffer[i] = 1;
count++;
printf("
, %d
", count);
}
}