PHP implementation read Memory sequence number _php tutorial

Source: Internet
Author: User
Tags semaphore

PHP implementation read Memory sequence number


This article mainly introduces the PHP implementation of reading memory sequence number, very simple and practical, the need for friends can refer to the next

Just do the recording, OSC this site should have duplicate

semWrapper.class.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

/*

* Semaphore (Semaphore).

* This is a wrapper class that addresses different implementations of "semaphores" under different platforms.

* This class is only symbolic at the moment, and it is actually an empty run under the Windows platform (and does not really implement mutexes).

*/

Class Semwrapper

{

Private $hasSemSupport;

Private $sem;

Const SEM_KEY = 1;

Public Function __construct ()

{

$this->hassemsupport = function_exists (' Sem_get ');

if ($this->hassemsupport) {

$this->sem = Sem_get (Self::sem_key);

}

}

Public function acquire () {

if ($this->hassemsupport) {

Return Sem_acquire ($this->sem);

}

return true;

}

Public Function release () {

if ($this->hassemsupport) {

Return Sem_release ($this->sem);

}

return true;

}

}

SeqGenerator.class.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

/*

* Sequence number generator.

*/

Class Seqgenerator

{

Const SHM_KEY = 1;

/**

* Initialization of Sequence number generator.

* Only the first call after server startup is valid, and calling this method after this does not actually work.

* @param int $start The starting value of the sequence number.

* @return Boolean returns true to indicate success.

*/

static public function init ($start = 1)

{

Mutual exclusion through semaphores to avoid access violation of shared memory

$SW = new Semwrapper;

if (! $SW->acquire ()) {

return false;

}

Open Shared Memory

$shm _id = Shmop_open (Self::shm_key, ' n ', 0644, 4);

if (Empty ($shm _id)) {

Because the ' n ' mode is used, if shared memory cannot be opened, you can assume that the shared memory has been created without having to initialize it again

$SW->release ();

return true;

}

Write an initial value in shared memory

$size = Shmop_write ($shm _id, pack (' L ', $start), 0);

if ($size! = 4) {

Shmop_close ($shm _id);

$SW->release ();

return false;

}

Turn off shared memory, release semaphores

Shmop_close ($shm _id);

$SW->release ();

return true;

}

/**

* Generate the next order number.

* Order number generated by @return int

*/

static public Function Next ()

{

Mutual exclusion through semaphores to avoid access violation of shared memory

$SW = new Semwrapper;

if (! $SW->acquire ()) {

return 0;

}

Open Shared Memory

$shm _id = Shmop_open (Self::shm_key, ' W ', 0, 0);

if (Empty ($shm _id)) {

$SW->release ();

return 0;

}

To read a sequential number from shared memory

$data = Shmop_read ($shm _id, 0, 4);

if (empty ($data)) {

$SW->release ();

return 0;

}

$arr = Unpack (' L ', $data);

$seq = $arr [1];

Write the next sequential number to shared memory

$size = Shmop_write ($shm _id, pack (' L ', $seq + 1), 0);

if ($size! = 4) {

$SW->release ();

return 0;

}

Turn off shared memory, release semaphores

Shmop_close ($shm _id);

$SW->release ();

return $seq;

}

}

page.php

?

1

2

3

4

5

How to use

$seq = Seqgenerator::next ();

Var_dump ($SEQ);

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/976530.html www.bkjia.com true http://www.bkjia.com/PHPjc/976530.html techarticle PHP Implementation Read Memory sequence number This article mainly introduces the implementation of PHP read memory sequence number, very simple and practical, the need for friends can refer to just do the record, the OSC site should have heavy ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.