whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

62 Visitors Online


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

Tip Rating (2):  
     


{
  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