4600-project-1/src/producer.cpp

33 lines
688 B
C++

#include <semaphore.h>
#include <unistd.h>
#include "list.hpp"
#include "globals.hpp" // freelist list1 list2
#include "producer.hpp"
#define wait(x) sem_wait(&x)
#define sgnl(x) sem_post(&x)
void produce(block* b);
void *producer (void *) {
while (true) {
wait(semfl); // wait for freelist not empty
wait(mutfl); // lock freelist
block *b = list_unlink(freelist);
sgnl(mutfl); // unlock freelist
//* Produce information in block b
produce(b);
wait(mutl1); // lock l1
list_link(list1, b);
sgnl(mutl1); // unlock l1
sgnl(seml1);
}
return nullptr;
}
void produce(block *b) {
b->data++;
usleep(65);
return;
}