You are not logged in.
I am reading a book about semaphores and process synchronization. The book gives answers to the exercises.
The exercise in question is the famous "single lane bridge" problem. The answer is given in C, and using a monitor for synchronization.
monitor bridge
{
int num_waiting_north = 0, num_waiting_south = 0, on_bridge = 0;
condition ok_to_cross; // condition data type?
int prev = 0;
void enter_bridge_north()
{
num_waiting_north++;
while (on_bridge || (prev == 0 && num_waiting_south > 0))
ok_to_cross.wait();
num_waiting_north--;
prev = 0;
}
void exit_bridge_north()
{
on_bridge = 0;
ok_to_cross.broadcast();
}
void enter_bridge_south()
{
num_waiting_south++;
while (on_bridge || (prev == 0 && num_waiting_north > 0))
ok_to_cross.wait();
num_waiting_south--;
prev = 1;
}
void exit_bridge_south()
{
on_bridge = 0;
ok_to_cross.broadcast();
}
}
How do you compile this? I have never heard of a monitor data type. Is this just a struct? If so what is the condition data type? I think I must be missing something...
Last edited by Google (2010-10-01 02:57:56)
Offline
Offline
I see C++ and other languages, but how do you do this in C? I read the wiki and it says the pthread library enables them in "other languages" I am assuming they mean C, but how? I understand I must state where the locks are etc, and the code I posted does it. It doesn't explain how you can compile a monitor <name> {} type in C.
Programming languages that have supported monitors include
Ada since Ada 95 (as protected objects)
C# (and other languages that use the .NET Framework)
Concurrent Euclid
Concurrent Pascal
D programming language
Delphi (Delphi 2009 and above, via TObject.Monitor)
Java (via the wait and notify keyword)
Mesa
Modula-3
Python (via threading.Condition object)
Ruby
Squeak Smalltalk
Turing, Turing+, and Object-Oriented Turing
μC++
A number of libraries have been written that allow monitors to be constructed in languages that do not support them natively. When library calls are used, it is up to the programmer to explicitly mark the start and end of code executed with mutual exclusion. PThreads is one such library.
Last edited by Google (2010-09-30 20:57:31)
Offline
I have no experience with semaphores, but I do have experience with C. The code you posted looks totally foreign to me.
Does the author explicitly say it's C code? Do you think they might have been lazy and wrote "C" instead of "C++"? Can anyone confirm or deny that this is C++ code?
Offline
The book is based in C since it's an operating systems book. It states for the answer "C syntax."
The code seems too specific to be pseudo-code.
Offline
The book is based in C since it's an operating systems book. It states for the answer "C syntax."
This implies that it's C pseudo-code, not C code.
Offline
Certain? Syntax was always language based like syntax highlighting etc. I never thought of it as pseudo code. I blame the author becase he uses the word psuedo code and syntax interchangably. Thanks!
Offline