Recent

Author Topic: TProcess Troubles  (Read 2395 times)

Frank

  • Jr. Member
  • **
  • Posts: 62
TProcess Troubles
« on: January 21, 2015, 08:44:35 pm »
Hi.
Lazarus-1.2.6 w/fpc-2.6.4 on clean install Slackware64 14.1  ( I had unexplainable issues, so I formated/reinstalled &
so far everything is repeatable)
This is my issue:
I am attempting to use TProcess (with "Run detached program" example from "http://wiki.freepascal.org/Executing_External_Programs#Run_detached_program" ) to have ProgramA start & run ProgramB. ProgramB is
to keep running regardless if ProgramA shuts down. But these are my results :

1/ ProgramA started from within Lazarus, Result : ProgramB started by ProgramA runs correctly, but shuts down
    when ProgramA shuts  down ( terminal command "ps -A" shows both programs as "pts");

2/ ProgramA started from "File Manager", Result : ProgramB does not start correctly, "ps -A" shows
   "tty  ProgramB <defunct>" & ProgramB  disappears from "ps -A" list when ProgramA is shut down.

3/ ProgramA is started from Terminal with "./ProgramA", Result : All works as I wanted, ProgramB is started by ProgramA, and stays running even after ProgramA and Terminal are shutdown.

First, are these the results I should be expecting? Second is it possible to get #3's results without launching a terminal (#2) ?

Thanks, Frank

ps, Here is my code...

{
  ###################  Run Program, ShellExecute Replacement ###################
}

Procedure linuxShellExecute(ExeName : String);
var
  Process: TProcess;
  I: Integer;
begin
  Process := TProcess.Create(nil);
  try
    Process.InheritHandles := False;
    Process.Options        := [];
    Process.ShowWindow     := swoShow;

    // Copy default environment variables including DISPLAY variable for GUI application to work
    for I := 0 to GetEnvironmentVariableCount - 1 do
      Process.Environment.Add(GetEnvironmentString(I));

//    Process.Executable:= ExeName;
    Process.Executable:= '/usr/bin/kwrite';
    Process.Execute;
  finally
    Process.Free;
  end;
end;
       

Frank

  • Jr. Member
  • **
  • Posts: 62
Re: TProcess Troubles
« Reply #1 on: January 31, 2015, 09:53:17 pm »
Finally got back to this, & for those who are interested, I was able to achieve my required results.
I don't consider it to be great code because, at this point I don't understand the under lying Linux
& for me this all learning curve... If you have any suggestions, I'm interested.

Here is my code :

Procedure BareBonesShellExecute(ExeName : String);
Var
  ClonePid    : TPid;
  ChildPid    : TPid;
  Child2Pid   : TPid;
Begin
  // 1st instance of program
  ChildPid  := fpGetPid;

  // if = -1,  = error...
  // if = 0 ,  = Child Process
  // if > 0 ,  = Pid of Parent Process
  ClonePid:= fpfork();

  ExecuteProcess(ExeName, '', []);
  // This works, it just runs ExeName in Terminal (great for troubleshooting !)
  // ExecuteProcess('/usr/bin/' + DetectXterm, '"' + ExeName + '"', []);

  Child2Pid := fpGetPid;

  // SIGTERM asks program to stop,
  // SIGKILL init kills program...

  // fpFork returned Pid of new child
  if ClonePid > 0 then
    // request new child to terminate...
    fpKill(ClonePid, SIGTERM);

     //  new Pid = 0 = new child
  if (ClonePid = 0) and
     // ChildPid BEFORE fpFork call <> ChildPid AFTER fpFork...
     (ChildPid <> Child2Pid) then
       // request child before fpFork call terminate...
       fpKill(ChildPid, SIGTERM);

end;

Thanks, Frank