Simple FpFork example lazarus / 61-Beginners ---------------------------------------------------------------------- richardcrossley · 2020-07-07 09:35 Hi, I'm new to Lazarus and I am trying to write a simple example of a GUI that can create a new instance of itself. A search suggested using the FpFork function... https://www.freepascal.org/docs-html/rtl/baseunix/fpfork.html I coded a simple example, as below, but my program throws an exception, that suggests I need to to enable multi-threading, which isn't what I am trying to achieve, I really do want a separate instance of the application. Thanks for any assistance you can provide. Terminal output Code: [Select]Parent process. Start child process. fork_test: Fatal IO error 11 (Resource temporarily unavailable) on X server :1. [xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. fork_test: ../../src/xcb_io.c:260: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed. Aborted (core dumped) Application code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit frm_main; {$mode objfpc}{$H+} interface uses  baseunix,  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btn_fork: TButton;    procedure btn_forkClick(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.btn_forkClick(Sender: TObject);var  pid: TPid; begin  pid := FpFork ;   if pid = 0 then  begin    writeln('Start child process.');  end  else  begin    writeln('Parent process.');  end;end; end.   Code: [Select]$ fpc -version [0.004] Free Pascal Compiler version 3.0.4+dfsg-23 [2019/11/25] for x86_64 [0.004] Copyright (c) 1993-2017 by Florian Klaempfl and others [0.004] error: No source file name in command line [0.004] error: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode Lazarus version 2.0.6+dfsq-3 OS is Ubuntu 20.04 64 bit Linux Kernel: 5.4.0-40-generic ---------------------------------------------------------------------- marcov · 2020-07-07 09:41 That response is not by Free Pascal, but by the X library. ---------------------------------------------------------------------- richardcrossley · 2020-07-07 09:49 Is there some way to work around this, I'm using Cinnamon on Ubuntu 20.04? ---------------------------------------------------------------------- Warfley · 2020-07-07 12:19 This is a wild guess, but fork simply copies the open filedescriptors, which includes the xserver connection. So now you have two processes communicating with the X-Server via the same socket, which the X-Server does not like. Your error message already tells you that this can be fixed by enabling multithreaded support in the xserver via XInitThreads, but I think most probably the best way to go about this is rather than copying your application you want to start it from ground up, e.g. using TProcess, and use different mechanisms to restore the state ---------------------------------------------------------------------- marcov · 2020-07-07 12:22 fork is a complicated call that essentially simply duplicates the process record in the process table. However, there are a lot of rules about fork what you can and can't do, and how to treat and divide assets. (like file handles, memory etc). It is not exactly an easy call to master, especially not in a very complicated (GUI) application. So basically there are two ways I can answer this: Start reading *nix manpages and documentation to get deeper knowledge into forkWhat are you exactly trying to achieve with fork ? (p.s. oops, some of this are for other fork kinds like rfork and clone) ---------------------------------------------------------------------- richardcrossley · 2020-07-07 13:50 TProcess, I had started looking at that, thanks for the confirmation. I managed to find an example, that did most of what I wanted... https://wiki.freepascal.org/Executing_External_Programs#Run_detached_p rogram I also managed to find some documentation on the Application variable. I was wondering if there was a TProcess instance within it which I could plunder. However most of the details I need are exposed as properties. Many thanks for the help. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit frm_main; {$mode objfpc}{$H+} interface uses  Process,  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    btn_process: TButton;    procedure btn_processClick(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.btn_processClick(Sender: TObject);var  new_process: TProcess;   env_count: integer; begin   new_process := TProcess.Create(nil);  try    new_process.InheritHandles := False;    new_process.Options := [];  new_process.ShowWindow := swoShow;     // Copy default environment variables including DISPLAY variable for GUI application to work    for env_count := 1 to GetEnvironmentVariableCount do      new_process.Environment.Add(GetEnvironmentString(env_count));     new_process.Executable := Application.ExeName;    new_process.Execute;  finally    new_process.Free;  end; end; end. ---------------------------------------------------------------------- Warfley · 2020-07-08 00:05 Quotenew_process.InheritHandles := False; new_process.Options := []; new_process.ShowWindow := swoShow; for env_count := 1 to GetEnvironmentVariableCount do   new_process.Environment.Add(GetEnvironmentString(env_count));Are these lines really required? Because in my oppinion they shouldn't. ShowWindow is afaik windows only. Linux does not have a concept of GUI applications therefore there is no point in such an option. Options should be initialized empty About InheritHandles, while execve or posix_spawn (don't know what TProcess internally uses, I hope it's posix_spawn, but for this doesn't matter) does not close all filedescriptors, unless you are forkchaining your application (that means that every process spawns a new child process before it dies) you should not run into any problems if you don't do this (that said, if you forkchain, not doing this will fire back hard). Lastly the Environment variables, if you don't specify any environment variables, it should inherit the ones from the parent. Only if you want to modify them you need to copy them. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  p: TProcess;begin  p:= TProcess.Create(nil);  p.Executable:='/usr/bin/env';  p.Execute;end.Prints all environment variables of the parent process. But adding a "p.Environment.Values['foo'] := 'bar';" before the execute line, will result in having only that single environment variable set So your code reduces to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i with TProcess.Create dotry  ExecName := ParamStr(0); // a more portable way, you might not always have an application object available  Execute;finally  Free;end; ---------------------------------------------------------------------- richardcrossley · 2020-07-08 02:33 Thanks for the information, the test application works well after removing the unnecessary lines. Much appreciated.