Producer Consumer
Producer ()
{
btype * next , * ptr ;
while (true) do
{
produce (next);
wait(empty_buffer);
wait(mutex);
ptr = get.(empty_buffer);
Signal(mutex)
Copy buffer(next,ptr);
Wait(mutex);
Release(ptr,full-buffer);
Signal(mutex); //Signaling that buffer is now full//)
Signal(full_buffer);
//end while//
} //end Producer function//
Consumer ()//beginning of
{
btype *next, *ptr;
while (true)
{
wait(full_buffer);
wait(mutex);
ptr = get(full_buffer);
Signal(mutex);
copy_buffer(ptr,next);
wait(mutex);
realease(ptr,empty_buffer);//consume items of buffer//.
Signal(mutex);
Signal(empty_buffer);
Consume(next);
}//end of while loop//
} // end consumer program.
Unbounded buffers
Process Producer X
{
while(true)do
{
produce_item;
place_item_in_buffer;
signal(produce);
other_x_processing;
} // end of while loop //
} // end of Producer X process. //
Process Consumer Y
{
while(true)do
{
wait(produce);
obtain_item_from_buffer;
consume_item;
other_Y_processing;
}//end of while loop//
} //end of processor Y//
Main process
{
initiate producer X;
initiate consumer Y;
} // end of main process//
Reader_writer_problem
Reader_process()
{
While(true)
{
WAIT(r_mutex);
Reader_count = reader_count + 1;
If(reader_count==1)
{
WAIT(Write_date);
Signal(r_mutex); //Critical_section//
access(resource);
WAIT(r_mutex);
}
reader_count=reader_count-1;
if(reader_count==0)
{
SIGNAL(Write_data);
SIGNAL(r_mutex);
}//end if//
}//end of while loop
}//end of process reader//
Writer_process()
{
while(true)
{
WAIT(write_data);
Access(resource);
SIGNAL(write_data);
} //end of while loop//
} //end process write//
resourcetype*resource;
intreader_count=0;
Semaphore r_mutex=1;
Semphore Write_data=1;
Main_process()
{
reader_count=0;
Signal(r_mutex);
Signal(write_data);
Initiate reader_process;
Initiate writer_process;
} // end of parent process//