ex71.pp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. program TestC{lone};
  2. {$ifdef Linux}
  3. // close is very Linux specific. 1.9.x threading is done via pthreads.
  4. uses
  5. Linux, Errors, crt;
  6. const
  7. Ready : Boolean = false;
  8. aChar : Char = 'a';
  9. function CloneProc( Arg: Pointer ): LongInt; Cdecl;
  10. begin
  11. WriteLn('Hello from the clone ',PChar(Arg));
  12. repeat
  13. Write(aChar);
  14. Select(0,0,0,0,600);
  15. until Ready;
  16. WriteLn( 'Clone finished.');
  17. CloneProc := 1;
  18. end;
  19. var
  20. PID : LongInt;
  21. procedure MainProc;
  22. begin
  23. WriteLn('cloned process PID: ', PID );
  24. WriteLn('Press <ESC> to kill ... ' );
  25. repeat
  26. Write('.');
  27. Select(0,0,0,0,300);
  28. if KeyPressed then
  29. case ReadKey of
  30. #27: Ready := true;
  31. 'a': aChar := 'A';
  32. 'A': aChar := 'a';
  33. 'b': aChar := 'b';
  34. 'B': aChar := 'B';
  35. end;
  36. until Ready;
  37. WriteLn('Ready.');
  38. end;
  39. const
  40. StackSze = 16384;
  41. theFlags = CLONE_VM+CLONE_FS+CLONE_FILES+CLONE_SIGHAND;
  42. aMsg : PChar = 'Oops !';
  43. var
  44. theStack : Pointer;
  45. ExitStat : LongInt;
  46. begin
  47. GetMem(theStack,StackSze);
  48. PID := Clone(@CloneProc,
  49. Pointer( LongInt(theStack)+StackSze),
  50. theFlags,
  51. aMsg);
  52. if PID < 0 then
  53. WriteLn('Error : ', LinuxError, ' when cloning.')
  54. else
  55. begin
  56. MainProc;
  57. case WaitPID(0,@ExitStat,Wait_Untraced or wait_clone) of
  58. -1: WriteLn('error:',LinuxError,'; ',StrError(LinuxError));
  59. 0: WriteLn('error:',LinuxError,'; ',StrError(LinuxError));
  60. else
  61. WriteLn('Clone exited with: ',ExitStat shr 8);
  62. end;
  63. end;
  64. FreeMem( theStack, StackSze );
  65. {$else}
  66. begin
  67. {$endif}
  68. end.