ex71.pp 1.4 KB

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