Homework Week 8 2
Homework Week 8 2
#de ne BUFFER_SIZE 5
bu er_item bu er[BUFFER_SIZE];
int in = 0, out = 0;
pthread_mutex_t mutex;
sem_t empty, full;
pthread_mutex_unlock(&mutex);
sem_post(&full);
return 0;
}
*item = bu er[out];
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
return 0;
}
if (insert_item(item) == 0) {
prin ("produced %d\n", item);
}
ff
ff
tf
ff
ff
}
}
if (remove_item(&item) == 0) {
prin ("consumed %d\n", item);
}
}
}
if (argc != 4) {
return -1;
}
sleep_ me = atoi(argv[1]);
num_producers = atoi(argv[2]);
num_consumers = atoi(argv[3]);
pthread_mutex_init(&mutex, NULL);
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
ff
ti
tf
ti
pthread_t producers[num_producers], consumers[num_consumers];
for (int i = 0; i < num_producers; i++) {
pthread_create(&producers[i], NULL, producer, NULL);
}
for (int i = 0; i < num_consumers; i++) {
pthread_create(&consumers[i], NULL, consumer, NULL);
}
sleep(sleep_ me);
prin ("done");
return 0;
}
Screenshot of Result:
tf
ti