Php implements sequential number of Read Memory

Source: Internet
Author: User

Php implements sequential number of Read Memory

This article mainly introduces how to read the serial number of the memory in php, which is very simple and practical. For more information, see

 

 

Only record, the osc site should have repeated

SemWrapper. class. php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/*

* Semaphore ).

* This is a packaging class used to solve different implementation methods of the "semaphore" on different platforms.

* Currently, this class is symbolic. It is actually a dry run on Windows platforms (and does not actually implement mutual exclusion ).

*/

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

21

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;

 

/**

* Initialize the sequence number generator.

* This method is valid only when it is called for the first time after the server is started.

* @ Param int $ start generates the starting value of the sequence number.

* @ Return boolean true indicates success.

*/

Static public function init ($ start = 1)

{

// Mutex through semaphores to avoid access conflicts to the shared memory

$ Sw = new SemWrapper;

If (! $ Sw-> acquire ()){

Return false;

}

 

// Enable shared memory

$ Shm_id = shmop_open (self: SHM_KEY, 'n', 0644, 4 );

If (empty ($ shm_id )){

// Because the 'N' mode is used, if you cannot enable the shared memory, you can think that the shared memory has been created and you do not need to initialize it again.

$ Sw-> release ();

Return true;

}

 

// Write the initial value to the shared memory

$ Size = shmop_write ($ shm_id, pack ('l', $ start), 0 );

If ($ size! = 4 ){

Shmop_close ($ shm_id );

$ Sw-> release ();

Return false;

}

 

// Disable shared memory and release semaphores

Shmop_close ($ shm_id );

$ Sw-> release ();

Return true;

}

 

/**

* Generate the next sequence number.

* @ Return int indicates the sequence number generated.

*/

Static public function next ()

{

// Mutex through semaphores to avoid access conflicts to the shared memory

$ Sw = new SemWrapper;

If (! $ Sw-> acquire ()){

Return 0;

}

 

// Enable shared memory

$ Shm_id = shmop_open (self: SHM_KEY, 'w', 0, 0 );

If (empty ($ shm_id )){

$ Sw-> release ();

Return 0;

}

 

// Read the sequence number from the 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 sequence number to the shared memory

$ Size = shmop_write ($ shm_id, pack ('l', $ seq + 1), 0 );

If ($ size! = 4 ){

$ Sw-> release ();

Return 0;

}

 

// Disable shared memory and release semaphores

Shmop_close ($ shm_id );

$ Sw-> release ();

Return $ seq;

}

}

Page. php

?

1

2

3

4

5

// Usage

 

$ Seq = SeqGenerator: next ();

 

Var_dump ($ seq );

The above is all the content of this article. I hope you will like it.

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.