Recent

Author Topic: Trouble with pipes, AssignStream, sockets  (Read 3641 times)

WalterK

  • New Member
  • *
  • Posts: 48
Trouble with pipes, AssignStream, sockets
« on: October 24, 2011, 02:13:28 am »
All of a sudden I'm having trouble with getting data from a Fortran program.
The code to run this program and feed and read data is something like this:

---------
   AssignStream (tsin,tsout,'pgm',[]);

   <write input data to tsout>
   Flush (tsout);

   while (not eof(tsin)) do
      begin
      Readln (tsin,aline);
      writeln (aline);
      end;

   Close (tsout); // have to close after "pgm" writes output or it hangs!
   Close (tsin);
-------
Data is fed into this program via a network tcp connection from a client on another computer.

Up until recently it worked without any trouble, but now it seems to be hanging on the "Readln(tsin,aline)" statement until I close the client program.

The fortran program "pgm" does its job, but (now) its output only gets printed after the client (on another computer) is terminated.  I had put in a signal handler for SigPipe but I've taken that back out now to no effect.

This code (fpc 2.4.2 and fortran) is running on a FreeBSD 8.2, P2 computer.

Any ideas?

Thanks.

Walter

WalterK

  • New Member
  • *
  • Posts: 48
Re: Trouble with pipes, AssignStream, sockets
« Reply #1 on: October 24, 2011, 08:56:28 pm »
I've pulled out the code that runs the external program... something like this:

EDIT: I should have noted that I am in the process of changing the way to run the external program, and that is why the code I first posted in this thread uses AssignStream, and the code below uses TProcess.  The mis-behavior is the same.

Code: [Select]
program runtest;

uses SysUtils, StrUtils, Unix,
     PgmData,
     BaseUnix,
     Classes, Process;

var
   tf : text;
...
   i,j : integer;

function  RunBatch: integer;
const
   TDiffToSecs = 100000.0;
var
   istat : integer;
   i : integer;
   j : integer;
   tf : text;
   P : TProcess;
   aline : string;
   PID : cint;
   checkt : cardinal;
   currPri : cint;
   newPri : cint;
   Cindex : cardinal;
   PStart : TDateTime;
   runtime : cardinal;
begin
   istat := 0;

// Create/CD into directory specific to user
// .....

   System.Assign (tf, 'run.IN');
   ReWrite (tf);
   <write data to 'tf'>
   Close (tf);

   P := TProcess.Create(nil);
   P.CommandLine := 'pgm run';
   P.Execute;

   PStart := now;

   PID := P.ProcessID;

   currPri := fpGetPriority(prio_process,PID);
   Cindex := 0;
   checkt := CheckTimes[Cindex];

   while (P.Running) do
      begin
      sleep (checkt);
      runtime := round ((now - PStart) * TDiffToSecs);
      if (Cindex < MaxIntervals) then
         begin
         if (runtime >= TransTimes[Cindex]) then
            begin
            inc (Cindex);
            checkt := CheckTimes[Cindex];
            newpri := currPri + Cindex;
            fpSetPriority (prio_process,PID,newpri);
            end;
         end
      else if (runtime >= MaxRunTime) then
         begin
         fpKill (PID,SigKill);
         istat := 999;
         end;
      end; // while running

   P.Free;

   if (istat = 0) then
      begin
      System.Assign (tf, 'run.OUT');
      Reset (tf);
      while (not eof(tf)) do
         begin
         Readln (tf,aline);
//         writeln (Sout,aline);
         writeln (aline);
         end;
      Close (tf);
      end
   else
      begin
//      writeln (Sout,'*ERROR Job ran too long');
      end;

   RunBatch := istat;
end;

begin
   RunBatch;
end.

It runs fine as a standalone program, but hangs when waiting for input in the larger program.  When I kill the tcp-connected client on the other computer it spits out the results.

Anyone have a clue as to why it hangs?  This is a project killer.

Walter
« Last Edit: October 24, 2011, 11:31:55 pm by WalterK »

 

TinyPortal © 2005-2018