#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> const char *test_file = "/tmp/test_lock"; int main(int argc, char const *argv[]) { int file_desc; int byte_count; char *byte_to_write = "A"; struct flock region_1; struct flock region_2; int res; file_desc = open(test_file, O_RDWR | O_CREAT , 0666); if (!file_desc) { /* code */ fprintf(stderr, "Unable to open %s for read/write\n", test_file ); exit(EXIT_FAILURE); } for (byte_count = 0; byte_count < 100; byte_count ++) { /* code */ (void)write(file_desc, byte_to_write, 1); } region_1.1_type = F_RDLCK; region_1.1_whence = SEEK_SET; region_1.1_start = 10; region_1.1_len = 20; region_2.1_type = F_WRLCK; region_2.1_whence = SEEK_SET; region_2.1_start = 40; region_2.1_len = 10; printf("Process %d locking file \n", getpid() ); res = fcntl(file_desc, F_SETLK, ®ion_1); if (res == -1) { /* code */ fprintf(stderr, "Failed to lock region_1\n"); } res = fcntl(file_desc, F_SETLK, ®ion_2); if (res == -1) { /* code */ fprintf(stderr, "Failed to lock region_2\n" ); } sleep(60); printf("Process %d closing file \n", getpid()); close(file_desc); exit(EXIT_SUCCESS); }
代码我照着书本打了,检查了好几遍,也没看出哪里错了,也不太清楚到底提示的是什么错误!
1_type应该是l_type吧?(L,不是数字1)
唔,话说变量名也不能以数字开头啊 ######我草,被坑了,原来是这样l啊,看上去和1一样,呵呵,太粗心了!###### 1_type
invalid suffix "_type",数字开头的变量名?(C全忘光了) ######理论上应该找光盘来复制,或者google。 纯手打往往就是找打。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。