📄 test2.c
字号:
#include "unpipc.h"
int
main(int argc, char **argv)
{
int fd;
fd = Open("test1.data", O_RDWR | O_CREAT, FILE_MODE);
Read_lock(fd, 0, SEEK_SET, 0); /* parent read locks entire file */
printf("%s: parent has read lock\n", Gf_time());
if (Fork() == 0) {
/* 4first child */
sleep(1);
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(3);
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);
}
/* 4parent */
sleep(5);
Un_lock(fd, 0, SEEK_SET, 0);
printf("%s: parent releases read lock\n", Gf_time());
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -