Solve the problem (+1)!
This commit is contained in:
parent
05cd1033cd
commit
ff108dcd0b
@ -17,6 +17,6 @@ extern list lists[3];
|
||||
extern list *freelist, *list1, *list2;
|
||||
|
||||
// count semaphores
|
||||
extern sem_t sem_freelist, sem_list1, sem_list2;
|
||||
extern sem_t sem_freelist, sem_list1, sem_list2, sem_freelist_minus_1;
|
||||
// binary semaphores
|
||||
extern sem_t mut_freelist, mut_list1, mut_list2;
|
@ -9,19 +9,27 @@ int consume(block* c);
|
||||
void *consumer (void *) {
|
||||
block* c;
|
||||
while (true) {
|
||||
// wait for element on list2
|
||||
wait(sem_list2);
|
||||
// mutual exclusion for list2
|
||||
wait(mut_list2);
|
||||
//* c:=unlink(list-2);
|
||||
c = unlink(list2);
|
||||
// mutual exclusion for list2
|
||||
signal(mut_list2);
|
||||
|
||||
//* consume_information_in_block(c)
|
||||
consume(c);
|
||||
|
||||
// mutual exclusion for freelist
|
||||
wait(mut_freelist);
|
||||
//* link(c, freelist)
|
||||
link(c, freelist);
|
||||
// mutual exclusion for freelist
|
||||
signal(mut_freelist);
|
||||
// signal thread-1 to continue
|
||||
signal(sem_freelist_minus_1);
|
||||
// signal new element on freelist
|
||||
signal(sem_freelist);
|
||||
|
||||
}
|
||||
@ -29,5 +37,5 @@ void *consumer (void *) {
|
||||
}
|
||||
|
||||
int consume(block *c) {
|
||||
return c->data;
|
||||
return 0;
|
||||
}
|
||||
|
@ -16,11 +16,9 @@ list lists[3] = {0};
|
||||
list *freelist = &lists[0], *list1 = &lists[1], *list2 = &lists[2];
|
||||
|
||||
// count semaphores
|
||||
sem_t sem_freelist, sem_list1, sem_list2;
|
||||
sem_t sem_freelist, sem_freelist_minus_1, sem_list1, sem_list2;
|
||||
// binary semaphores
|
||||
sem_t mut_freelist, mut_list1, mut_list2;
|
||||
// binary semaphores for reading/writing
|
||||
sem_t mut_take, mut_give;
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
// initialize the freelist
|
||||
@ -29,6 +27,7 @@ int main (int argc, char* argv[]) {
|
||||
//TODO: Implement a semaphore solution to the problem
|
||||
// counting semaphores measure the length
|
||||
sem_init(&sem_freelist, 0, freelist->length);
|
||||
sem_init(&sem_freelist_minus_1, 0, freelist->length-1);
|
||||
sem_init(&sem_list1, 0, 0);
|
||||
sem_init(&sem_list2, 0, 0);
|
||||
// binary semaphores create mutual exclusion
|
||||
|
@ -8,24 +8,29 @@ void produce(block* b);
|
||||
|
||||
void *producer (void *) {
|
||||
block *b;
|
||||
while (true)
|
||||
{
|
||||
int v;
|
||||
while(sem_getvalue(&sem_freelist, &v), v <= 1);
|
||||
wait(sem_freelist); // wait for freelist not empty
|
||||
wait(mut_freelist); // lock freelist
|
||||
while (true) {
|
||||
// wait for >1 block on freelist
|
||||
wait(sem_freelist_minus_1);
|
||||
// wait for element on freelist
|
||||
wait(sem_freelist);
|
||||
// mutual exclusion for freelist
|
||||
wait(mut_freelist);
|
||||
//* b:= unlink(freelist);
|
||||
b = unlink(freelist);
|
||||
signal(mut_freelist); // unlock freelist
|
||||
// mutual exclusion for freelist
|
||||
signal(mut_freelist);
|
||||
|
||||
//* produce_information_in_block(b)
|
||||
produce(b);
|
||||
|
||||
wait(mut_list1); // lock mutex
|
||||
// mutual exclusion for list1
|
||||
wait(mut_list1);
|
||||
//* link(b, list1);
|
||||
link(b, list1);
|
||||
signal(mut_list1); // unlock mutex
|
||||
signal(sem_list1); // signal new item on list
|
||||
// mutual exclusion for list1
|
||||
signal(mut_list1);
|
||||
// signal new element on list1
|
||||
signal(sem_list1);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -9,31 +9,43 @@ void transform(block* x, block* y);
|
||||
void *transformer (void *) {
|
||||
block *x, *y;
|
||||
while (true) {
|
||||
// wait for element on list1
|
||||
wait(sem_list1);
|
||||
wait(sem_freelist);
|
||||
// mutual exclusion for list1
|
||||
wait(mut_list1);
|
||||
//* x:=unlink(list1);
|
||||
x = unlink(list1);
|
||||
// mutual exclusion for list1
|
||||
signal(mut_list1);
|
||||
|
||||
// wait for element on freelist
|
||||
wait(sem_freelist);
|
||||
// mutual exclusion for freelist
|
||||
wait(mut_freelist);
|
||||
//* y:=unlink(freelist);
|
||||
y = unlink(freelist);
|
||||
// mutual exclusion for freelist
|
||||
signal(mut_freelist);
|
||||
|
||||
//* use_block_x_to_produce_info_in_y(x,y)
|
||||
transform(x, y);
|
||||
|
||||
// mutual exclusion for freelist
|
||||
wait(mut_freelist);
|
||||
//* link(x, freelist);
|
||||
link(x, freelist);
|
||||
// mutual exclusion for freelist
|
||||
signal(mut_freelist);
|
||||
// freelist++
|
||||
signal(sem_freelist);
|
||||
|
||||
// mutual exclusion for list2
|
||||
wait(mut_list2);
|
||||
//* link(y, list2);
|
||||
link(y, list2);
|
||||
// mutual exclusion for list2
|
||||
signal(mut_list2);
|
||||
// signal new element on list2
|
||||
signal(sem_list2);
|
||||
|
||||
}
|
||||
@ -41,5 +53,6 @@ void *transformer (void *) {
|
||||
}
|
||||
|
||||
void transform(block *x, block *y) {
|
||||
usleep(timescale);
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user