sysv.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. --TEST--
  2. General semaphore and shared memory test
  3. --SKIPIF--
  4. <?php // vim600: ts=4 sw=4 syn=php fdm=marker
  5. if(!extension_loaded('sysvsem') || !extension_loaded('sysvshm')) {
  6. die("skip Both sysvsem and sysvshm required");
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. $MEMSIZE = 512; // size of shared memory to allocate
  12. $SEMKEY = ftok(__FILE__, 'P'); // Semaphore key
  13. $SHMKEY = ftok(__FILE__, 'Q'); // Shared memory key
  14. echo "Start.\n";
  15. // Get semaphore
  16. $sem_id = sem_get($SEMKEY, 1);
  17. if ($sem_id === FALSE) {
  18. echo "Fail to get semaphore";
  19. exit;
  20. }
  21. echo "Got semaphore $sem_id.\n";
  22. // Accuire semaphore
  23. if (! sem_acquire($sem_id)) {
  24. echo "Fail to acquire semaphore $sem_id.\n";
  25. sem_remove($sem_id);
  26. exit;
  27. }
  28. echo "Success acquire semaphore $sem_id.\n";
  29. $shm_id = shm_attach($SHMKEY, $MEMSIZE);
  30. if ($shm_id === FALSE) {
  31. echo "Fail to attach shared memory.\n";
  32. sem_remove($sem_id);
  33. exit;
  34. }
  35. echo "Success to attach shared memory : $shm_id.\n";
  36. // Write variable 1
  37. if (!shm_put_var($shm_id, 1, "Variable 1")) {
  38. echo "Fail to put var 1 on shared memory $shm_id.\n";
  39. sem_remove($sem_id);
  40. shm_remove ($shm_id);
  41. exit;
  42. }
  43. echo "Write var1 to shared memory.\n";
  44. // Write variable 2
  45. if (!shm_put_var($shm_id, 2, "Variable 2")) {
  46. echo "Fail to put var 2 on shared memory $shm_id.\n";
  47. sem_remove($sem_id);
  48. shm_remove ($shm_id);
  49. exit;
  50. }
  51. echo "Write var2 to shared memory.\n";
  52. // Read variable 1
  53. $var1 = shm_get_var ($shm_id, 1);
  54. if ($var1 === FALSE) {
  55. echo "Fail to retrieve Var 1 from Shared memory $shm_id, return value=$var1.\n";
  56. } else {
  57. echo "Read var1=$var1.\n";
  58. }
  59. // Read variable 1
  60. $var2 = shm_get_var ($shm_id, 2);
  61. if ($var1 === FALSE) {
  62. echo "Fail to retrieve Var 2 from Shared memory $shm_id, return value=$var2.\n";
  63. } else {
  64. echo "Read var2=$var2.\n";
  65. }
  66. // Release semaphore
  67. if (!sem_release($sem_id)) {
  68. echo "Fail to release $sem_id semaphore.\n";
  69. } else {
  70. echo "Semaphore $sem_id released.\n";
  71. }
  72. // remove shared memory segmant from SysV
  73. if (shm_remove ($shm_id)) {
  74. echo "Shared memory successfully removed from SysV.\n";
  75. } else {
  76. echo "Fail to remove $shm_id shared memory from SysV.\n";
  77. }
  78. // Remove semaphore
  79. if (sem_remove($sem_id)) {
  80. echo "semaphore removed successfully from SysV.\n";
  81. } else {
  82. echo "Fail to remove $sem_id semaphore from SysV.\n";
  83. }
  84. echo "End.\n";
  85. /* NOTE: assigned semids differ depending on the kernel, since
  86. * there are actually 3 semaphores per PHP-created
  87. * semaphores in effect, to keep state information.
  88. * That's the reason for EXPECTF.
  89. */
  90. ?>
  91. --EXPECTF--
  92. Start.
  93. Got semaphore Resource id #%i.
  94. Success acquire semaphore Resource id #%i.
  95. Success to attach shared memory : %s.
  96. Write var1 to shared memory.
  97. Write var2 to shared memory.
  98. Read var1=Variable 1.
  99. Read var2=Variable 2.
  100. Semaphore Resource id #%s released.
  101. Shared memory successfully removed from SysV.
  102. semaphore removed successfully from SysV.
  103. End.