whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews ¦  misc ¦  forum
 kylix ¦  tutorials ¦  online shop ¦  photos ¦  Add&Win Game

Tips (1565)

Database (91)
Files (139)
Forms (113)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (133)
IntraWeb (0)
Kylix (10)
Math (77)
Misc (128)
Multimedia (46)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (246)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

36 Visitors Online


SwissDelphiCenter is a Borland Technology Partner
 
...clone a process in Linux?
Autor: Elias Zurschmiede
Homepage: http://www.delight.ch
[ Print tip ]  

Tip Rating (1):  
     


{
  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
}

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners