comp.lang.ada
 help / color / mirror / Atom feed
* stumped with reader/writter task problem
@ 1997-12-14  0:00 Dan Pettus
  0 siblings, 0 replies; only message in thread
From: Dan Pettus @ 1997-12-14  0:00 UTC (permalink / raw)





I've been working on a program where  I should design and implement an
Ada-95 application, such that, for N distinct given files all having
identical record structures, M distinct writers should each create a
distinct ``Master'' file of own, i.e. M distinct Sequential file as a
merge of N distinct files should exist at the end of the application. 

 My solution should be based on tasks running concurrently, such that,
each file should be read "mutually excluded" from the others; the
order of merge is irrelevant since by another application whenever the
``Master'' should be sorted; not by this application. 

the problen is that if I have tho reader/writters (or tasks) and two
input files. The first task will only putone input file into its
sequential Master" file while the second task will do both input
files.

Should I use discriminants with my task? If so how?

REMOVE NOSPAM IF RELPYING VIA EMAIL.

Thanks in advance.
 
I have implemented this using an array of tasks. the tasks package is
as follows:

-- tasks_00.adb

with Text_IO; use Text_IO;
with SeqFP_00_P; use SeqFP_00_P;
                 
package body Tasks_00 is
                 
procedure Tasks (N_of_MFiles       : in Integer;
                 N_of_IFiles       : in Integer;
                 Master_File_Names : in F_N_Type;
                 Input_File_Names  : in F_N_Type;
                 GotAccess         : in out I_A_Type) is
  
  package Int_IO is new Integer_IO(Integer); use Int_IO;
                     
  T : Integer := 0;
                     
  task type Writter;

  WA : array(1..N_of_MFiles) of Writter;
  
  
  Task body Writter is
  AllRead      : Boolean;
  Created_SeqF : Boolean := False;
  HaveRead     : array(1..N_of_IFiles) of Integer;
  i,j,k        : Integer;
  begin
    T := T + 1;
    for i in 1..N_of_IFiles loop  -- Initialize to haven't read any
      HaveRead(i) := 0;
    end loop;
    loop                                 -- loop until read all files
      for k in 1..N_of_IFiles    -- check for free files to read
      loop
        if (GotAccess(k) = 0) and (HaveRead(k) = 0) then
          GotAccess(k) := 1;    -- gain control of the file
          put("Task ");
          put(T);
          put("   File ");
          put(k);
          new_line;
          HaveRead(k) := 1;    -- have read the file
          if (Created_SeqF) then -- append to already created file
            Append_Seqf (Master_File_Names(T), Input_File_Names(k));
          else     --first file read, create sequential master file
            Create_Seqf (Master_File_Names(T), Input_File_Names(k));
            Created_SeqF := True; 
          end if;
          GotAccess(k) := 0;  -- return access to other tasks
        end if;  
      end loop;
      AllRead := True;
      for j in 1..N_of_IFiles
      loop
        put(HaveRead(i));
        if HaveRead(i)=0 then
          AllRead:=False;  -- at least 1 file not read
        end if;
      end loop;
      New_Line;  
      exit when AllRead;
    end loop;    
  end Writter; 
      
      
begin
        
  null;
          
end Tasks;
      
end Tasks_00;
-------------------------------------------------------------------------------------------------

Perhaps "SeqFP_00_P" is needed to be seen to understand fully.
-- seqfp_00_p.adb



package body SeqFP_00_P is

  procedure Init (File_Names : out F_N_Type;
                  N_of_Files : out Integer) is

    NoF, L : Integer;
    FName  : String(1..16);

  begin

    Put_Line("========================");
    Put_Line("=    Name the Files    =");
    Put_Line("========================");
    New_Line(2);
    Put("  N_of_Files : ");
    Get(NoF); Skip_Line;
    New_Line(2);
    for i in 1..NoF
    loop
      Put("File-Name-");
    Get(NoF); Skip_Line;
    New_Line(2);
    for i in 1..NoF  
    loop
      Put("File-Name-");
      Put(i, Width => 0);
      Put(" : ");
      Get_Line(FName,L);
      File_Names(i) := new String'(FName(1..L));
    end loop;
    N_of_Files := NoF;
    
  end Init;
    
  procedure Create_SeqF (OuN,
                         InN  : in Ptr_Str) is
      
    In_FP  : File_Type;  
    Out_FP : Seq_IO.File_Type;
    R0     : Rec;
      
  begin
    
    Open(In_FP,In_FILE,InN.all);
  
    Seq_IO.Create(Out_FP,Seq_IO.Out_File,OuN.all);
  
    while not End_Of_File(In_FP)
    loop
      Get(In_FP,R0.f0);  
      Get(In_FP,R0.f1);
      Get(In_FP,R0.f2); Skip_Line(In_FP);
      Seq_IO.Write(Out_FP,R0);
    end loop;
    Close(In_FP);
    Seq_IO.Close(Out_FP);
  
  end Create_SeqF;
  
  procedure Append_SeqF (OuN,   
                         InN  : in Ptr_Str) is
      
    In_FP  : File_Type;
    Out_FP : Seq_IO.File_Type;
    R0     : Rec;
    
  begin
    
    Open(In_FP,In_FILE,InN.all);
  
    Seq_IO.Open(Out_FP,Seq_IO.Append_File,OuN.all);
  
    while not End_Of_File(In_FP)
    loop
      Get(In_FP,R0.f0);
      Get(In_FP,R0.f1);
      Get(In_FP,R0.f2); Skip_Line(In_FP);
      Seq_IO.Write(Out_FP,R0);
    end loop;
    Close(In_FP);
    Seq_IO.Close(Out_FP);
  
  end Append_SeqF;
  
  procedure View_SeqF (OuN,
                       InN  : in Ptr_Str) is
      
    In_FP  : Seq_IO.File_Type;
    Out_FP : File_Type;
    R0     : Rec;
    
  begin
    
    Seq_IO.Open(In_FP,Seq_IO.In_File,InN.all);
  
    Create(Out_FP,Out_File,OuN.all);
    Put_Line(Out_FP,"from Sequential_File to Text_File /");
    New_Line(Out_FP);
    while not Seq_IO.End_Of_File(In_FP)
    loop
      Seq_IO.Read(In_FP,R0);
      Put(Out_FP,R0.f0);
      Put(Out_FP,R0.f1);
      Put(Out_FP,R0.f2);
      New_Line(Out_FP);
    end loop;
    New_Line(Out_FP);
    Seq_IO.Close(In_FP);
    Close(Out_FP);
      
  end View_SeqF;
      
end SeqFP_00_P;




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-12-14  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-14  0:00 stumped with reader/writter task problem Dan Pettus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox