Artists and Viewers. In the new Center for Strange Arts (CESA) in New York City,
ID: 3754179 • Letter: A
Question
Artists and Viewers. In the new Center for Strange Arts (CESA) in New York
City, there is a famous Weird Hall, open 24 hours a day, with a very large painting
being perpetually developed by the walk-in artists. Because the artists tend to fight
with one another as well as accost viewers, only one artist at a time may enter the
Weird Hall, and once an artist is there, no other artists or viewers are allowed in.
(They have to wait in front of the Weird Hall.) If no artist is in, the viewers may come
in at any time and in any numbers; they are not queued behind the artists.
Furthermore, as long as there is at least one viewer is in the Weird Hall, no artist is
allowed in. Write the pseudo-code for synchronization of viewers and artists using
semaphores.
Explanation / Answer
// pseudo code for viewer and artist problem
define variables:
semaphore: artist=1 // to keep track of artist in the hall
count variable: viewer =0// to keep viewer count
semaphore: s=1 // to secure viewer count critical section.
// artist code
do
{
wait(artist); // to ensure only one artist can enter.
// edit the art
signal(artist);
} while(true);
// viewer code
do
{
wait(s); // viewer is entering critical section.
viewer++;
// as soon as the first viewer enters we block artist to enter by changing the artist semaphore
if(viewer==1)
wait(artist);
// other viewer can also enter so we signal the semaphore s.
signal(s);
// after viewing the art a person exits.
wait(s);
viewer--; //a viewer exits.
if(viewer==0)
signal(artist); // if there is no viewer left the artist can enter.
signal(s);
} while(true);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.