?? test4.c
字號:
#include "unpipc.h"
int
main(int argc, char **argv)
{
int fd;
fd = Open("test1.data", O_RDWR | O_CREAT, FILE_MODE);
Write_lock(fd, 0, SEEK_SET, 0); /* parent write locks entire file */
printf("%s: parent has write lock\n", Gf_time());
if (Fork() == 0) {
/* 4first child */
sleep(3);
printf("%s: first child tries to obtain write lock\n", Gf_time());
Writew_lock(fd, 0, SEEK_SET, 0); /* this should block */
printf("%s: first child obtains write lock\n", Gf_time());
sleep(2);
Un_lock(fd, 0, SEEK_SET, 0);
printf("%s: first child releases write lock\n", Gf_time());
exit(0);
}
if (Fork() == 0) {
/* 4second child */
sleep(1);
printf("%s: second child tries to obtain read lock\n", Gf_time());
Readw_lock(fd, 0, SEEK_SET, 0);
printf("%s: second child obtains read lock\n", Gf_time());
sleep(4);
Un_lock(fd, 0, SEEK_SET, 0);
printf("%s: second child releases read lock\n", Gf_time());
exit(0);
}
/* parent */
sleep(5);
Un_lock(fd, 0, SEEK_SET, 0);
printf("%s: parent releases write lock\n", Gf_time());
exit(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -