linux下檔案鎖定有兩種:一種是以原子操作方式建立鎖檔案;另一種是允許鎖定檔案的一部分,從而獨享對這一部分內容的訪問。
1、鎖檔案
許多應用程式只需要能夠針對某個資源建立一個鎖檔案,然後其他程式通過檢查這個檔案來判斷它們是否被允許訪問這個資源。建立鎖檔案使用fcntl.h標頭檔(樓主機器上位於/usr/include下)定義的open系統調用,並帶上O_CREAT和O_EXCL標誌。這樣就以原子操作完成兩項工作:確定檔案不存在,然後 建立
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <errno.h>int main(){ int file_desc; int save_errno; file_desc = open("/tmp/LCK.test", O_RDWR | O_CREAT | O_EXCL, 0444); if (file_desc == -1) { save_errno = errno; printf("Open failed with error %d\n", save_errno); } else { printf("Open succeeded\n"); } exit(EXIT_SUCCESS);}
不過在第二次運行以上程式的時候,會提示錯誤:open failed with error 17(檔案已存在,錯誤碼在/usr/include/asm-generic/error-base.h),如果想讓程式再次執行成功,就必須刪除那個鎖檔案。在c語言調用中,我們可以使用unlink函數(定義於/usr/include/unistd.h)。另外,以上的代碼也不是很合理的,只有open沒有close,正常情況下,應該加上以下兩行:
(void)close(file_desc); (void)unlink( "/tmp/LCK.test");
關於unlink,可以參考http://bbs.chinaunix.net/thread-1920145-1-1.html 的說法。unlink原型如下:
#include <unistd.h>int unlink (__const char *__name)
函數功能:刪除目錄項,並將由__name所引用檔案的連結計數減1,當連結計數為0時,檔案被刪除。
關於unlink的使用,可以參考《unix環境進階編程》第17章,17.3.2唯一連結,在listen前,先unlink以防檔案已經存在,accept後再unlink,防止下次調用處問題。曾經樓主在ACE的unix域通訊端ACE_LSOCK上遇到address in use,其實就是鎖檔案已存在沒有刪除的問題。
2、地區鎖定
地區鎖定出現,是因為鎖檔案方式並不適用於訪問大型的共用檔案。如果一個大檔案,由一個程式寫入資料,但卻由不同的程式同時對這個檔案進行更新。處理常式不能等待記錄程式結束,所以需要一些協調方法來提供對同一個檔案的並發訪問。linux提供了2種方法來實現:一是fcntl系統調用和lockf調用。
一個使用fcntl鎖定檔案的例子如下:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>const char *test_file = "/tmp/test_lock";int main() { int file_desc; int byte_count; char *byte_to_write = "A"; struct flock region_1; struct flock region_2; int res; /* open a file descriptor */ file_desc = open(test_file, O_RDWR | O_CREAT, 0666); if (!file_desc) { fprintf(stderr, "Unable to open %s for read/write\n", test_file); exit(EXIT_FAILURE); } /* put some data in the file */ for(byte_count = 0; byte_count < 100; byte_count++) { (void)write(file_desc, byte_to_write, 1); } /* setup region 1, a shared lock, from bytes 10 -> 30 */ region_1.l_type = F_RDLCK; region_1.l_whence = SEEK_SET; region_1.l_start = 10; region_1.l_len = 20; /* setup region 2, an exclusive lock, from bytes 40 -> 50 */ region_2.l_type = F_WRLCK; region_2.l_whence = SEEK_SET; region_2.l_start = 40; region_2.l_len = 10; /* now lock the file */ printf("Process %d locking file\n", getpid()); res = fcntl(file_desc, F_SETLK, ion_1); if (res == -1) fprintf(stderr, "Failed to lock region 1\n"); res = fcntl(file_desc, F_SETLK, ion_2); if (res == -1) fprintf(stderr, "Failed to lock region 2\n"); /* and wait for a while */ sleep(60); printf("Process %d closing file\n", getpid()); close(file_desc); exit(EXIT_SUCCESS);}
程式首先建立一個檔案,然後以讀寫方式開啟,添加一些資料。接著在檔案中設定2個地區,第一個是0-30,用讀(共用)鎖;第二個是40-50,用寫(獨佔) 鎖,然後調用fcntl來鎖定這2個地區。
fcntl參數提供了3個命令選項
F_GETLK、F_SETLK、F_SETLKW,l_type提供的選項有F_RDLCK、F_UNLCK、F_WRLCK,分別為讀鎖,解鎖,寫鎖
測試鎖的程式如下:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>const char *test_file = "/tmp/test_lock";#define SIZE_TO_TRY 5void show_lock_info(struct flock *to_show);int main() { int file_desc; int res; struct flock region_to_test; int start_byte; /* open a file descriptor */ file_desc = open(test_file, O_RDWR | O_CREAT, 0666); if (!file_desc) { fprintf(stderr, "Unable to open %s for read/write", test_file); exit(EXIT_FAILURE); } for (start_byte = 0; start_byte < 99; start_byte += SIZE_TO_TRY) { /* set up the region we wish to test */ region_to_test.l_type = F_WRLCK; region_to_test.l_whence = SEEK_SET; region_to_test.l_start = start_byte; region_to_test.l_len = SIZE_TO_TRY; region_to_test.l_pid = -1; printf("Testing F_WRLCK on region from %d to %d\n", start_byte, start_byte + SIZE_TO_TRY); /* now test the lock on the file */ res = fcntl(file_desc, F_GETLK, ion_to_test); if (res == -1) { fprintf(stderr, "F_GETLK failed\n"); exit(EXIT_FAILURE); } if (region_to_test.l_pid != -1) { printf("Lock would fail. F_GETLK returned:\n"); show_lock_info(ion_to_test); } else { printf("F_WRLCK - Lock would succeed\n"); } /* now repeat the test with a shared (read) lock */ /* set up the region we wish to test */ region_to_test.l_type = F_RDLCK; region_to_test.l_whence = SEEK_SET; region_to_test.l_start = start_byte; region_to_test.l_len = SIZE_TO_TRY; region_to_test.l_pid = -1; printf("Testing F_RDLCK on region from %d to %d\n", start_byte, start_byte + SIZE_TO_TRY); /* now test the lock on the file */ res = fcntl(file_desc, F_GETLK, ion_to_test); if (res == -1) { fprintf(stderr, "F_GETLK failed\n"); exit(EXIT_FAILURE); } if (region_to_test.l_pid != -1) { printf("Lock would fail. F_GETLK returned:\n"); show_lock_info(ion_to_test); } else { printf("F_RDLCK - Lock would succeed\n"); } } /* for */ close(file_desc); exit(EXIT_SUCCESS);}void show_lock_info(struct flock *to_show) { printf("\tl_type %d, ", to_show->l_type); printf("l_whence %d, ", to_show->l_whence); printf("l_start %d, ", (int)to_show->l_start); printf("l_len %d, ", (int)to_show->l_len); printf("l_pid %d\n", to_show->l_pid);}
(以上大部分內容來自《linux程式設計》)
就是這樣吧,遇到的東西是越來越多,慢慢總結