ファイルとレコードのロックの使用
UNIX のロック機能を使用すると、デッドロックを検出および防止できます。デッドロックが発生する可能性があるのは、システムがレコードロックインタフェースを休眠させようとするときだけです。このとき、2 つのプロセスがデッドロック状態であるかどうかを判断する検索が行われます。潜在的なデッドロックが検出されると、ロックインタフェースは失敗し、デッドロックを示す値が errno に設定されます。F_SETLK を使用してロックを設定するプロセスは、ロックがすぐに取得できなくても、それを待たないので、デッドロックは発生しません。
事務処理に必須の「ファイル・ロッキング」
F_RDLCK リードロック read lock (シェアードロック shared lock)
F_WRLCK ライトロック write lock (排他ロック exclusive lock)
F_UNLCK アンロック (上記ふたつのロックを解除)
F_WRLCK は、ファイルの特定の領域を、ひとつのプロセスだけがロック可能にするためのもので、これは排他制御そのものですから、「排他ロック」(exclusive lock)と呼ばれることがあります。前記の creat(), link(), open() を使った方式に比べて、ファイルの更新に必要な一部分だけをロックすることができるため、更新の場所が違う限り、複数のプロセスが同時に同じファイルにアクセスすることが可能で、データ処理の効率が良くなります。F_WRLCK は、対象とするファイルに、書き込みのためのパーミションを必要とします。
F_RDLCK の方は、通常、ファイルの読み取りに使用され、ファイルの更新を禁止します。この場合は、既に他のプロセスが F_RDLCK した部分に、オーバーラップした F_RDLCK をかけることができます。F_WRLCK された 部分に F_RDLCK をかけることはできません。
また、F_RDLCK された部分に、F_WRLCKをかけることもできません。書き込みのためのロックと違って、ファイルのロックされた部分を、読み込みを行う複数のプロセスが共有することができるため、「シェアードロック」(shared lock) と呼ばれることがあります。もちろん、これを使うためにはリードのパーミションが必要です。
IPC: ファイル・ロック
アドバイザリロックと強制ロックの選択
サンプルコード
アドバイザリロックfcntl(2)
flock_sample1-1.c
#include
1#include
2#include
3#include
4#include
5#include
6
7int main()
8{
9 struct flock fl1;
10 struct flock fl2;
11 int fd;
12
13 fl1.l_type = F_WRLCK;
14 fl1.l_whence = SEEK_SET;
15 fl1.l_start = 0;
16 fl1.l_len = 1;
17 fl1.l_pid = getpid();
18
19 fl2.l_type = F_WRLCK;
20 fl2.l_whence = SEEK_SET;
21 fl2.l_start = 1;
22 fl2.l_len = 1;
23 fl2.l_pid = getpid();
24
25 if ((fd = open("lock.txt", O_RDWR)) == -1) {
26 perror("open");
27 exit(1);
28 }
29
30 printf("Press to try to get lock(1): ");
31 getchar();
32 fputs("waiting...", stdout);
33
34 if (fcntl(fd, F_SETLKW, &fl1) == -1) {
35 perror("fcntl");
36 exit(1);
37 }
38
39 puts("Locked(1).");
40
41 printf("Press to try to get lock(2): ");
42 getchar();
43 fputs("waiting...", stdout);
44
45 if (fcntl(fd, F_SETLKW, &fl2) == -1) {
46 perror("fcntl");
47 exit(1);
48 }
49
50 puts("Locked(2).");
51
52 printf("Press to release lock(2): ");
53 getchar();
54
55 fl2.l_type = F_UNLCK;
56 if (fcntl(fd, F_SETLK, &fl2) == -1) {
57 perror("fcntl");
58 exit(1);
59 }
60
61
62 puts("Unlocked(2).");
63
64 printf("Press to release lock(1): ");
65 getchar();
66
67 fl1.l_type = F_UNLCK;
68 if (fcntl(fd, F_SETLK, &fl1) == -1) {
69 perror("fcntl");
70 exit(1);
71 }
72
73 puts("Unlocked(1).");
74
75 close(fd);
76
77 return 0;
78}
flock_sample1-2.c
#include
1#include
2#include
3#include
4#include
5#include
6
7int main()
8{
9 struct flock fl1;
10 struct flock fl2;
11 int fd;
12
13 fl1.l_type = F_WRLCK;
14 fl1.l_whence = SEEK_SET;
15 fl1.l_start = 0;
16 fl1.l_len = 1;
17 fl1.l_pid = getpid();
18
19 fl2.l_type = F_WRLCK;
20 fl2.l_whence = SEEK_SET;
21 fl2.l_start = 1;
22 fl2.l_len = 1;
23 fl2.l_pid = getpid();
24
25 if ((fd = open("lock.txt", O_RDWR)) == -1) {
26 perror("open");
27 exit(1);
28 }
29
30 printf("Press to try to get lock(2): ");
31 getchar();
32 fputs("waiting...", stdout);
33
34 if (fcntl(fd, F_SETLKW, &fl2) == -1) {
35 perror("fcntl");
36 exit(1);
37 }
38
39 puts("Locked(2).");
40
41 printf("Press to try to get lock(1): ");
42 getchar();
43 fputs("waiting...", stdout);
44
45 if (fcntl(fd, F_SETLKW, &fl1) == -1) {
46 perror("fcntl");
47 exit(1);
48 }
49
50 puts("Locked(1).");
51
52 printf("Press to release lock(1): ");
53 getchar();
54
55 fl1.l_type = F_UNLCK;
56 if (fcntl(fd, F_SETLK, &fl1) == -1) {
57 perror("fcntl");
58 exit(1);
59 }
60
61
62 puts("Unlocked(1).");
63
64 printf("Press to release lock(2): ");
65 getchar();
66
67 fl2.l_type = F_UNLCK;
68 if (fcntl(fd, F_SETLK, &fl2) == -1) {
69 perror("fcntl");
70 exit(1);
71 }
72
73 puts("Unlocked(2).");
74
75 close(fd);
76
77 return 0;
78}
実行例
# ./flock_sample1-1
# ./flock_sample1-1
Press to try to get lock(1):
waiting...Locked(1).
Press to try to get lock(2):
waiting...Locked(2).
Press to release lock(2):
Unlocked(2).
Press to release lock(1):
Unlocked(1).
# ./flock_sample1-2
# ./flock_sample1-2
Press to try to get lock(2):
waiting...Locked(2).
Press to try to get lock(1):
fcntl: Deadlock situation detected/avoided
Deadlock situation detected/avoided
原因
プログラミングのデッドロック状態が検出され、回避されました。
対処方法
システムがデッドロックを検出し回避しなかった場合は、ソフトウェアの一部がハングします。そのプログラムを再度実行してください。デッドロックが再び起こることはないかもしれません。
テクニカルノート
このエラーは、通常、ファイルとレコードのロックに関連しています。ただし、mutex、セマフォ、条件変数、読み取り/書き込みロックが対象になる場合もあります。
このエラーの記号名は、EDEADLK、errno=45 です。
強制ロック
read(2), write(2)
flock_sample2-1.c
#include
1#include
2#include
3#include
4#include
5#include
6
7int main()
8{
9 struct flock fl1;
10 struct flock fl2;
11 int fd;
12
13 char buff[256] = "2222222222";
14 ssize_t n;
15
16 fl1.l_type = F_WRLCK;
17 fl1.l_whence = SEEK_SET;
18 fl1.l_start = 0;
19 fl1.l_len = 10;
20 fl1.l_pid = getpid();
21
22 fl2.l_type = F_WRLCK;
23 fl2.l_whence = SEEK_SET;
24 fl2.l_start = 10;
25 fl2.l_len = 10;
26 fl2.l_pid = getpid();
27
28 if ((fd = open("sample.txt", O_RDWR|O_CREAT, 0600)) == -1) {
29 perror("open");
30 exit(1);
31 }
32
33 printf("Press to try to get lock(1): ");
34 getchar();
35 fputs("waiting...", stdout);
36
37 if (fcntl(fd, F_SETLKW, &fl1) == -1) {
38 perror("fcntl");
39 exit(1);
40 }
41
42 puts("Locked(1).");
43
44 printf("Press to try to read record(2): ");
45 getchar();
46 fputs("waiting...", stdout);
47
48 n = pread(fd, buff, fl2.l_len, fl2.l_start);
49 if (n < 0) {
50 printf("Read(2) fail. errno=%d\n", errno);
51 } else {
52 buff[n] = '\0';
53 printf("Read(2) success. buff=\"%s\"\n", buff);
54 }
55
56 printf("Press to release lock(1): ");
57 getchar();
58
59 fl1.l_type = F_UNLCK;
60 if (fcntl(fd, F_SETLK, &fl1) == -1) {
61 perror("fcntl");
62 exit(1);
63 }
64
65 puts("Unlocked(1).");
66
67 close(fd);
68
69 return 0;
70}
flock_sample2-2.c
#include
1#include
2#include
3#include
4#include
5#include
6
7int main()
8{
9 struct flock fl1;
10 struct flock fl2;
11 int fd;
12
13 char buff[256] = "1111111111";
14 ssize_t n;
15
16 fl1.l_type = F_WRLCK;
17 fl1.l_whence = SEEK_SET;
18 fl1.l_start = 0;
19 fl1.l_len = 10;
20 fl1.l_pid = getpid();
21
22 fl2.l_type = F_WRLCK;
23 fl2.l_whence = SEEK_SET;
24 fl2.l_start = 10;
25 fl2.l_len = 10;
26 fl2.l_pid = getpid();
27
28 if ((fd = open("sample.txt", O_RDWR|O_CREAT, 0600)) == -1) {
29 perror("open");
30 exit(1);
31 }
32
33 printf("Press to try to get lock(2): ");
34 getchar();
35 fputs("waiting...", stdout);
36
37 if (fcntl(fd, F_SETLKW, &fl2) == -1) {
38 perror("fcntl");
39 exit(1);
40 }
41
42 puts("Locked(2).");
43
44 printf("Press to try to write record(1): ");
45 getchar();
46 fputs("waiting...", stdout);
47
48 n = pwrite(fd, buff, fl1.l_len, fl1.l_start);
49 if (n < 0) {
50 printf("Write(1) fail. errno=%d\n", errno);
51 } else {
52 buff[n] = '\0';
53 printf("Write(1) success. buff=\"%s\"\n", buff);
54 }
55
56 printf("Press to release lock(2): ");
57 getchar();
58
59 fl2.l_type = F_UNLCK;
60 if (fcntl(fd, F_SETLK, &fl2) == -1) {
61 perror("fcntl");
62 exit(1);
63 }
64
65 puts("Unlocked(2).");
66
67 close(fd);
68
69 return 0;
70}
実行例
# chmod +l sample.txt
# chmod +l sample.txt
root@solaris11:~# ls -l sample.txt
-rw-r-lr-- 1 root root 31 1月 17日 08:02 sample.txt
# ./flock_sample2-1
Press to try to get lock(1): ★1. レコード1をロックする
waiting...Locked(1).
Press to try to read record(2): ★4. レコード2を読み込み
waiting...Read(2) fail. errno=45
Press to release lock(1):
Unlocked(1).
# ./flock_sample2-2
Press to try to get lock(2): ★2. レコード2をロックする
waiting...Locked(2).
Press to try to write record(1): ★3. レコード1に書き込み
waiting...Write(1) success. buff="1111111111"
Press to release lock(2):
Unlocked(2).
- flock_sample2-1 で sample.txt のレコード1(先頭から10文字)をロックする。
- flock_sample2-2 でレコード2(10文字目から10文字)をロックする。
- flock_sample2-2 でレコード1を書き込む。レコード1はflock_sample2-1でロックされていて書き込めない。writeはスリープしてロックの解除を待機する。
- flock_sample2-1 でレコード2を読み込みむ。レコード2はflock_sample2-2でロックされているので読めない。lock_sample2-2は、flock_sample2-1が持つロックの解除を、スリープ状態で待っている。この状態で、flock_sample2-1がスリープ状態に入れば、デッドロックになる。そのためreadはEDEADLK(45)を返して失敗する。
0 件のコメント:
コメントを投稿