I just want to check answer that i think is correct.
Process producer {
while(1) {
// produce c
lock(empty);
lock(mutex);
data = c;
unlock(mutex);
unlock(full);
}
}
Process consumer {
while(1) {
lock(full);
lock(mutex);
c = data;
unlock(mutex);
unlock(empty);
// consume c
}
}
how many mutex are in use?
i think pthread_mutex_t empty , mutex, full;
three mutexes are used here.
what is the initial state of the mutexes?
I have no idea about this. What is the initial state of mutex?
Here is another example of mutex
Process producer {
while(1) {
lock(mutex);
if (count == 0){
// produce c
data = c;
count = 1;
}
unlock(mutex);
}
}
Process consumer {
while(1) {
lock(mutex);
if (count == 1){
c = data;
count = 0;
}
unlock(mutex);
// consume c
}
}
In my power point, the problems for this code are produced inside of lock and busy wait, but I think it is good. When something enters in the producer function or consumer function, it has mutex lock and escape them unlocked.
what is wrong?