ex66.pp 732 B

12345678910111213141516171819202122232425262728293031323334353637
  1. Program Example66;
  2. { Program to demonstrate the MMap function. }
  3. Uses linux;
  4. Var S : String;
  5. fd,Len : Longint;
  6. args : tmmapargs;
  7. P : PChar;
  8. begin
  9. S:='This is a string'#0;
  10. Len:=Length(S);
  11. fd:=fdOpen('testfile.txt',Open_wrOnly or open_creat);
  12. If fd=-1 then
  13. Halt(1);
  14. If fdWrite(fd,S[1],Len)=-1 then
  15. Halt(2);
  16. fdClose(fd);
  17. fdOpen('testfile.txt',Open_rdOnly);
  18. if fd=-1 then
  19. Halt(3);
  20. args.address:=0;
  21. args.offset:=0;
  22. args.size:=Len+1;
  23. args.fd:=Fd;
  24. args.flags:=MAP_PRIVATE;
  25. args.prot:=PROT_READ or PROT_WRITE;
  26. P:=Pchar(mmap(args));
  27. If longint(P)=-1 then
  28. Halt(4);
  29. Writeln('Read in memory :',P);
  30. fdclose(fd);
  31. if Not MUnMap(P,Len) Then
  32. Halt(LinuxError);
  33. end.