本文主要介紹了PHP程式中的檔案鎖定、互斥鎖、讀寫鎖定使用技巧解析,其中重點講解了sync模組和pthreads模組中的使用實例,需要的朋友可以參考下。希望對大家有幫助。
檔案鎖定
全名為 advisory file lock, 書中有提及。 這類鎖定比較常見,例如 mysql, php-fpm 啟動之後都會有一個pid檔記錄了進程id,這個檔案就是檔案鎖。
這個鎖定可以防止重複運行一個進程,例如在使用crontab時,限定每一分鐘執行一個任務,但這個進程運行時間可能超過一分鐘,如果不用進程鎖解決衝突的話兩個進程一起執行就會有問題。
使用PID檔案鎖定還有一個好處,方便進程向自己發出停止或重新啟動訊號。例如重啟php-fpm的指令為
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
發送USR2訊號給pid檔記錄的程序,訊號屬於進程通信,會另開一個篇幅。
php的介面為flock,文件比較詳細。先看一下定義,bool flock ( resource $handle , int $operation [, int &$wouldblock ] ).
$handle是檔案系統指針,是典型地由fopen()建立的resource(資源)。這意味著使用flock必須開啟一個檔案。
$operation 是操作類型。
&$wouldblock 如果鎖定是阻塞的,那麼這個變數會設為1.
要注意的是,這個函數預設是阻塞的,如果想非阻塞可以在operation 加一個bitmask LOCK_NB. 接下來測試一下。
$pid_file = "/tmp/process.pid"; $pid = posix_getpid(); $fp = fopen($pid_file, 'w+'); if(flock($fp, LOCK_EX | LOCK_NB)){ echo "got the lock \n"; ftruncate($fp, 0); // truncate file fwrite($fp, $pid); fflush($fp); // flush output before releasing the lock sleep(300); // long running process flock($fp, LOCK_UN); // 释放锁定 } else { echo "Cannot get pid lock. The process is already up \n"; } fclose($fp);
儲存為 process.php,執行php process.php &, 此時再次執行php process.php,就可以看到錯誤提示。 flock也有共用鎖,LOCK_SH.
互斥鎖和讀寫鎖
sync模組中的Mutex:
Mutex是一個組合詞,mutual exclusion。用pecl安裝sync模組, pecl install sync。 文件中的SyncMutex只有兩個方法,lock 和 unlock, 我們就直接上程式碼測試吧。沒有用IDE寫,所以cs異常醜陋,請無視。
$mutex = new SyncMutex("UniqueName"); for($i=0; $i<2; $i++){ $pid = pcntl_fork(); if($pid <0){ die("fork failed"); }elseif ($pid>0){ echo "parent process \n"; }else{ echo "child process {$i} is born. \n"; obtainLock($mutex, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } function obtainLock ($mutex, $i){ echo "process {$i} is getting the mutex \n"; $res = $mutex->lock(200); sleep(1); if (!$res){ echo "process {$i} unable to lock mutex. \n"; }else{ echo "process {$i} successfully got the mutex \n"; $mutex->unlock(); } exit(); }
儲存為mutex.php, run php mutex.php, output is
parent process parent process child process 1 is born. process 1 is getting the mutex child process 0 is born. process 0 is getting the mutex process 1 successfully got the mutex Child 0 completed process 0 unable to lock mutex. Child 0 completed
這裡子程序0和1不一定誰在前面。但是總有一個得不到鎖。這裡SyncMutex::lock(int $millisecond)的參數是 millisecond, 代表阻塞的時長, -1 為無限阻塞。
sync模組中的讀寫鎖定:
SyncReaderWriter的方法類似,readlock, readunlock, writelock, writeunlock,成對出現即可,沒有寫測試程式碼,應該和Mutex的程式碼一致,把鎖替換一下就可以。
sync模組中的Event:
感覺和golang中的Cond比較像,wait()阻塞,fire()喚醒Event阻塞的一個行程。有一篇好文介紹了Cond, 可以看出Cond就是鎖的一種固定用法。 SyncEvent也一樣。
php文件中的例子顯示,fire()方法似乎可以用在web應用中。
上測試程式碼
for($i=0; $i<3; $i++){ $pid = pcntl_fork(); if($pid <0){ die("fork failed"); }elseif ($pid>0){ //echo "parent process \n"; }else{ echo "child process {$i} is born. \n"; switch ($i) { case 0: wait(); break; case 1: wait(); break; case 2: sleep(1); fire(); break; } } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } function wait(){ $event = new SyncEvent("UniqueName"); echo "before waiting. \n"; $event->wait(); echo "after waiting. \n"; exit(); } function fire(){ $event = new SyncEvent("UniqueName"); $event->fire(); exit(); }
這裡故意少寫一個fire(), 所以程式會阻塞,證明了 fire() 一次只喚醒一個行程。
pthreads模組
鎖定與解鎖互斥量:
函數:
pthread_mutex_lock (mutex) pthread_mutex_trylock (mutex) pthread_mutex_unlock (mutex)
用法:
線程用pthread_mutex_lock()函數去鎖定指定的mutex變量,若該mutex已經被另外一個線程鎖定了,該調用將會阻塞線程直到mutex被解鎖。
pthread_mutex_trylock() will attempt to lock a mutex. However, if the mutex is already locked, the routine will return immediately with a "busy" error code. This routine may be ful in pth#readuse_s in pth#readuse_try( # 試著著去鎖定一個互斥量,然而,若互斥量已被鎖定,程式會立刻回傳並傳回一個忙錯誤值。該函數在優先權改變情況下阻止死鎖是非常有用的。執行緒可以用pthread_mutex_unlock()解鎖自己佔用的互斥。在一個執行緒完成對保護資料的使用,而其它執行緒要獲得互斥量在保護資料上工作時,可以呼叫該函數。若有一下情形則會發生錯誤:
- 互斥量已經解鎖
- 互斥量被另一個執行緒佔用
- 互斥量並沒有多麼「神奇」的,實際上,它們就是參與的線程的「君子約定」。寫程式時要確信正確地鎖定,解鎖互斥。
Q:有多個執行緒等待同一個鎖定的互斥量,當互斥量解鎖後,那個執行緒會第一個鎖定互斥量?
A:除非執行緒使用了優先權調度機制,否則,執行緒會被系統調度器去分配,那個執行緒會第一個鎖定互斥是隨機的。#
#include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<pthread.h> typedef struct ct_sum { int sum; pthread_mutex_t lock; }ct_sum; void * add1(void *cnt) { pthread_mutex_lock(&(((ct_sum*)cnt)->lock)); for(int i=0; i < 50; i++) { (*(ct_sum*)cnt).sum += i; } pthread_mutex_unlock(&(((ct_sum*)cnt)->lock)); pthread_exit(NULL); return 0; } void * add2(void *cnt) { pthread_mutex_lock(&(((ct_sum*)cnt)->lock)); for(int i=50; i<101; i++) { (*(ct_sum*)cnt).sum += i; } pthread_mutex_unlock(&(((ct_sum*)cnt)->lock)); pthread_exit(NULL); return 0; } int main(void) { pthread_t ptid1, ptid2; ct_sum cnt; pthread_mutex_init(&(cnt.lock), NULL); cnt.sum=0; pthread_create(&ptid1, NULL, add1, &cnt); pthread_create(&ptid2, NULL, add2, &cnt); pthread_join(ptid1,NULL); pthread_join(ptid2,NULL); printf("sum %d\n", cnt.sum); pthread_mutex_destroy(&(cnt.lock)); return 0; }
信号量
sync模块中的信号量:
SyncSemaphore文档中显示,它和Mutex的不同之处,在于Semaphore一次可以被多个进程(或线程)得到,而Mutex一次只能被一个得到。所以在SyncSemaphore的构造函数中,有一个参数指定信号量可以被多少进程得到。
public SyncSemaphore::__construct ([ string $name [, integer $initialval [, bool $autounlock ]]] ) 就是这个$initialval (initial value)
$lock = new SyncSemaphore("UniqueName", 2); for($i=0; $i<2; $i++){ $pid = pcntl_fork(); if($pid <0){ die("fork failed"); }elseif ($pid>0){ echo "parent process \n"; }else{ echo "child process {$i} is born. \n"; obtainLock($lock, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } function obtainLock ($lock, $i){ echo "process {$i} is getting the lock \n"; $res = $lock->lock(200); sleep(1); if (!$res){ echo "process {$i} unable to lock lock. \n"; }else{ echo "process {$i} successfully got the lock \n"; $lock->unlock(); } exit(); }
这时候两个进程都能得到锁。
sysvsem模块中的信号量
sem_get 创建信号量
sem_remove 删除信号量(一般不用)
sem_acquire 请求得到信号量
sem_release 释放信号量。和 sem_acquire 成对使用。
$key = ftok('/tmp', 'c'); $sem = sem_get($key); for($i=0; $i<2; $i++){ $pid = pcntl_fork(); if($pid <0){ die("fork failed"); }elseif ($pid>0){ //echo "parent process \n"; }else{ echo "child process {$i} is born. \n"; obtainLock($sem, $i); } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; } sem_remove($sem); // finally remove the sem function obtainLock ($sem, $i){ echo "process {$i} is getting the sem \n"; $res = sem_acquire($sem, true); sleep(1); if (!$res){ echo "process {$i} unable to get sem. \n"; }else{ echo "process {$i} successfully got the sem \n"; sem_release($sem); } exit(); }
这里有一个问题,sem_acquire()第二个参数$nowait默认为false,阻塞。我设为了true,如果得到锁失败,那么后面的sem_release会报警告 PHP Warning: sem_release(): SysV semaphore 4 (key 0x63000081) is not currently acquired in /home/jason/sysvsem.php on line 33, 所以这里的release操作必须放在得到锁的情况下执行,前面的几个例子中没有这个问题,没得到锁执行release也不会报错。当然最好还是成对出现,确保得到锁的情况下再release。
此外,ftok这个方法的参数有必要说明下,第一个 必须是existing, accessable的文件, 一般使用项目中的文件,第二个是单字符字符串。返回一个int。
输出为
parent process parent process child process 1 is born. process 1 is getting the mutex child process 0 is born. process 0 is getting the mutex process 1 successfully got the mutex Child 0 completed process 0 unable to lock mutex. Child 0 completed
相关推荐:
以上是PHP中的檔案鎖定、互斥鎖、讀寫鎖定詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中