CIS332-004, CIS 332-104 (Spring 1996) PRINCIPLES OF OPERATING SYSTEMS

Professor: Franz J. Kurfess

Home page:http://www.cis.njit.edu/franz   E-mail:franz@cis.njit.edu 

Assignment

The following Ada code is a part of the solution for the producer-consumer problem. Code the consumer task so as to implement the whole solution.

Use the comments as guidelines to insert the code for the new task.

To test your program, type in characters; when you type `Z', the program stops.

Submission

You need to hand in a printout of your program (the Ada code, not the output)a, by Thursday, February 22nd.

You can send a mail message to Shiwen Chen, Schen@homer.njit.edu, to receive the incomplete code which you will have to complete.

Code

with TEXT_IO;
use  TEXT_IO;

procedure prodcons is
    item : CHARACTER;

    task type consumer;

    task type producer; 

--  Declare the new task

    task type buffer is
        entry start_buffer;
        entry insert(item : in CHARACTER);
		-- deposit an item in the buffer
        entry remove(item : out CHARACTER);
		-- retrieve an item from the buffer
    end buffer;

--  Create an instance of each task 
    b  : buffer;
    p  : producer;
    c  : consumer;

    task body buffer is
        n             : constant integer := 100;	-- buffer size
        count         : integer range 0..n := 0;	-- items in the buffer
        in_ch, out_ch : integer range 1..n := 1;	-- in and out indices
        bound_buf     : array(1..n) of CHARACTER;	-- the buffer
        item          : CHARACTER;			-- items are characters
    begin
        accept start_buffer do
             put_line("Starting buffer");
        end;
        put_line("Buffer is active");
        loop
            select
                when count < n =>		-- buffer is not full
                    accept insert( item: in CHARACTER ) do
                        bound_buf(in_ch) := item;
                        put_line("Item inserted into the buffer: ");
                        put(item);		-- display produced item 
                        in_ch:= (in_ch mod n) + 1;
                        count := count + 1;
                    end;
                or when count > 0 =>		-- buffer is not empty
                    accept remove(item : out CHARACTER) do
                        item := bound_buf(out_ch);
                        out_ch := (out_ch mod n) + 1;
                        count := count - 1;
                    end remove;
                or terminate;
            end select;
        end loop;
        put_line("Exiting buffer");
    end buffer;

    task body producer is
        item : CHARACTER;
    begin
        put_line("Producer is active");
        loop
            get(item);		-- read item from the keyboard
            b.insert(item);	-- put it into the buffer
            get(item);		-- read item from the keyboard
            b.insert(item);	-- put it into the buffer
            exit when item = 'Z';
         end loop;
        put_line("Exiting producer");
    end producer;


    task body consumer is

-- You need to write the code for the consumer task.
-- Each time it is invoked, the consumer task consumes TWO items.
-- Use the producer task as a guideline, and try to find
-- useful commmands in the rest of the code.
-- Everything you need is already somewhere in here,
-- you just have to put it together in the right way.


begin
    put("Producer/Consumer Problem");
    NEW_LINE;
    b.start_buffer;
end prodcons;

CIS 332 Principles of Operating Systems Spring 1996; Franz Kurfess.