New Tip - ...clone a process in Linux ?

  • From: "SwissDelphiCenter.ch" <newsletter@xxxxxxxxxxxxxxxxxxxx>
  • To: sdcnewtip@xxxxxxxxxxxxx
  • Date: Thu, 7 Mar 2002 20:47:40 +0100

* SwissDelphiCenter new Tip newsletter
* www.swissdelphicenter.ch
*
* To unsubscribe send a mail to sdcnewtip-request@xxxxxxxxxxxxx with the
* subject unsubscribe


Hallo,

Folgender neuer Tip ist neu auf SwissDelphiCenter verfügbar:

Besuchen Sie die Programmier Tips unter 
http://www.swissdelphicenter.ch/de/tipsindex.php


Bewerten Sie diesen Tip nach Schwierigkeitsgrad, Nützlichkeit und Gesamthaft auf
http://www.swissdelphicenter.ch/de/showcode.php?id=1061



Autor:
Elias Zurschmiede <e.zurschmiede@xxxxxxxxxx>
http://www.delight.ch


-------------
...unter Linux einen Prozess klonen ?
-------------
Kategorie: Kylix



{
  Unter Linux kann ein Process mit fork dupliziert werden. Im original
  Prozess gibt fork das Handle auf den duplizierten Prozess zurück, im
  Duplizierten wird 0 zurück gegeben.
}

{
  In Linux it is possible to duplicate a process with fork. In the original
  process, fork will return the handle to the duplicated process. The
  duplicated process will return zero.
}

program TestFork;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Libc;

var
  ForkedProcessHandle: __pid_t;
  bForked: Boolean;

procedure ForkNow;
begin
  bForked := true;
  ForkedProcessHandle := fork;
end;

function IsForked: Boolean;
begin
  Result := (ForkedProcessHandle = 0) and bForked;
end;

var
  Lf: Integer;

begin
  sigignore(SIGCHLD);
  bForked := false;

  WriteLn('do some stuff');

  WriteLn('before fork');
  ForkNow;
  WriteLn('after fork - we have dublicated the process');

  if IsForked then begin
    WriteLn('do some stuff in forked process (wait 5s)');
    for Lf := 0 to 50 do begin
      Write('f');
      sleep(100);
    end;
  end else begin
    WriteLn('do stuff in original process (wait 10)');
    for Lf := 0 to 100 do begin
      Write('o');
      sleep(100);
    end;
  end;

  WriteLn;

  if IsForked then
    WriteLn('forked process end')
  else
    WriteLn('original process end');
end.


{
Output of this demo app:

do some stuff
before fork
after fork - we have dublicated the process
after fork - we have dublicated the process
do some stuff in forked process (wait 5s)
fdo stuff in original process (wait 10)
ooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooff
ooffooffooffooffooffooffooffoo
forked process end
ooooooooooooooooooooooooooooooooooooooooooooooooo
original process end
}




Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatisch generierte EMail]


----------------------------------------------------
ENGLISH NEWSLETTER
----------------------------------------------------

Hi,

This new tip is new available on SwissDelphiCenter.ch:

Visit the programming tips at http://www.swissdelphicenter.ch/en/tipsindex.php


Rate this tip after skill, useful and overall at
http://www.swissdelphicenter.ch/en/showcode.php?id=1061



Author:
Elias Zurschmiede <e.zurschmiede@xxxxxxxxxx>
http://www.delight.ch


-------------
...clone a process in Linux ?
-------------
Category: Kylix



{
  Unter Linux kann ein Process mit fork dupliziert werden. Im original
  Prozess gibt fork das Handle auf den duplizierten Prozess zurück, im
  Duplizierten wird 0 zurück gegeben.
}

{
  In Linux it is possible to duplicate a process with fork. In the original
  process, fork will return the handle to the duplicated process. The
  duplicated process will return zero.
}

program TestFork;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Libc;

var
  ForkedProcessHandle: __pid_t;
  bForked: Boolean;

procedure ForkNow;
begin
  bForked := true;
  ForkedProcessHandle := fork;
end;

function IsForked: Boolean;
begin
  Result := (ForkedProcessHandle = 0) and bForked;
end;

var
  Lf: Integer;

begin
  sigignore(SIGCHLD);
  bForked := false;

  WriteLn('do some stuff');

  WriteLn('before fork');
  ForkNow;
  WriteLn('after fork - we have dublicated the process');

  if IsForked then begin
    WriteLn('do some stuff in forked process (wait 5s)');
    for Lf := 0 to 50 do begin
      Write('f');
      sleep(100);
    end;
  end else begin
    WriteLn('do stuff in original process (wait 10)');
    for Lf := 0 to 100 do begin
      Write('o');
      sleep(100);
    end;
  end;

  WriteLn;

  if IsForked then
    WriteLn('forked process end')
  else
    WriteLn('original process end');
end.


{
Output of this demo app:

do some stuff
before fork
after fork - we have dublicated the process
after fork - we have dublicated the process
do some stuff in forked process (wait 5s)
fdo stuff in original process (wait 10)
ooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooffooff
ooffooffooffooffooffooffooffoo
forked process end
ooooooooooooooooooooooooooooooooooooooooooooooooo
original process end
}



Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatic generated EMail]

Other related posts:

  • » New Tip - ...clone a process in Linux ?