Search on the Internet, there are two ways, but not too good: one is simply a process id+ time stamp, or process id+ random number to produce an approximate unique ID, although simple but for the pursuit of "perfect" I do not want to do so, and then Apache2 the process will be maintained for quite a long time, The probability of the generated ID collision is still relatively large; the second idea is through the self-added field of MySQL, this can not be considered, inefficient, not to say, my design is not a database.
The acquisition of an increment ID is a procedure:
1. Read IDs from a global store
2. Add 1 to ID
3. Re-save IDs to global storage
You need to use these 3 steps as a single step atomic operation in a multiple process or thread program to guarantee the unique ID.
Java is a good solution, because Java programs are mostly multithreaded, and each thread can share variables in the Java process, and it is convenient for the lock to control the synchronization of threads. In PHP, ID global storage is not a problem, you can put in the session, a big deal in the file, but synchronization between processes is the problem.
In fact, process scheduling, management is the operating system kernel must implement the function, the semaphore introduced today (also known as the semaphore) is on the Unix/linux to resolve process synchronization of a technology.
Signal light is used in the railway management mechanism, we see today most of the railway is double line parallel, but some sections by the mountain, topography affects only a single rail, must ensure that at the same time only a train running through these sections. The previous railroad was managed by a signal light: No train passing, signal, etc. in idle state, once a train into this section, the signal is in use state, other trains will need to wait, waiting for the previous train to drive out of the section of the signal into idle, before entering this section, once again a train into, The signal lights become busy again ... in order to ensure the safety and smooth operation of the railway.
The UNIX system manages the state of the Semaphore as the railway authority controls the semaphore, so it is also possible to say that the semaphore is managed by the kernel, and that the semaphore not only controls synchronization between processes, but also controls synchronization between threads.
The semaphore belongs to the system interprocess communication Technology (IPC), today we only from the PHP angle to introduce the use of semaphores, the technical details of IPC can refer to Stevens's authoritative book "UNIX Network Programming second volume interprocess communication".
First look at the final code:
Copy Code code as follows:
<?php
// ---------------------------------------------------
Increment serial number ID (1~1000000000)
//
IDs are stored in shared memory (shared memory), synchronized via Semaphore (semaphore)
// ---------------------------------------------------
$IPC _key = 0x1234; System V IPC KEY
$SEQ _key = "SEQ"; Key for storing serial number IDs in shared memory
Creates or obtains an existing semaphore with "1234" as the key
$sem _id = sem_get ($IPC _key);
Create or associate an existing shared memory with "1234" as key
$shm _id = Shm_attach ($IPC _key, 64);
The amount of signal, the equivalent of locking, at the same time only one process to run this section of code
Sem_acquire ($sem _id);
Get serial number ID from shared memory
$id = @shm_get_var ($shm _id, $SEQ _key);
if ($id = NULL | | $id >= 1000000000)
{
$id = 1;
}
Else
{
$id + +;
}
Write ID after "+ +" to shared memory
Shm_put_var ($shm _id, $SEQ _key, $id);
Releasing the semaphore, which is equivalent to unlocking
Sem_release ($sem _id);
Turn off shared memory associations
Shm_detach ($shm _id);
echo "Serial number id:{$id}";
?>
009 lines, defines a 16-in-one shaping key, which only supports System V's IPC mechanism in PHP, and needs to be associated with a specified resource (message queue, semaphore, shared memory) through a key.
010 line, defines a key that stores the increment ID in shared memory, which is idle for System V shared memory: it needs to be stored in a hashtable-like Key-value way. In the above code in the use of shared memory to do the ID of the storage container, can also be exchanged for sessions, files and other mechanisms, this article is focused on the semaphore, the knowledge of shared memory later on (don't forget the previous recommended book).
013 lines, get the semaphore with 1234 key in the system, and create one if not on the system.
015 lines, similar to 13 lines, gets the shared memory with 1234 key in the system, if one is not created on the system, and the second parameter 64 represents the creation of a 64bytes-size shared memory.
018~034 line, synchronizing the code area, when a process or thread executes a sem_acquire function that takes up the semaphore, and when it calls the Sem_release function to release the semaphore, other processes or threads execute to the Sem_acquire block. 021 lines get IDs from shared memory, function Shm_get_var prefix "@" to mask error messages (when first executed, there is no "SEQ" key in shared memory, and a warning message is printed on the page).
The other statements are very simple and do not need to be spoken more.
After the program is edited, accessing this PHP page will increment the output number.
We can view the semaphore and shared memory created by the program through the system command IPCS:
$ ipcs
------Shared Memory Segments--------
Key shmid owner perms bytes nattch Status
0x00001234 1212443 Www-data 666 64 0
------Semaphore Arrays--------
Key Semid owner Perms Nsems
0x00001234 163841 Www-data 666 3
------Message Queues--------
Key Msqid owner perms used-bytes messages
The first two paragraphs are shared memory and semaphores, and 0x00001234 is both the key we created.
You can also delete by command IPCRM:
$ ipcrm-m 0x00001234 #删除共享内存
$ ipcrm-s 0x00001234 #删除信号量
---------------------------------------------
PHP Manual on the IPC is very little information, this is not difficult to imagine, Stevens has been more than 10 years ago through the Dongdong, in PHP is only packaged, how much more need to explain it?
Text just by ID said the use of semaphores, if you have a simpler way to generate the ID, but also look enlighten.
Maybe a friend wants to know more about the efficiency of the semaphore, and I'm going to summarize it in an old-fashioned fashion: pretty fast.