Академический Документы
Профессиональный Документы
Культура Документы
h> const char *test_file = "test_lock1"; int main() { int file_desc; int byte_count; char *byte_to_write = "A"; struct flock region_1; struct flock region_2; int res,res1=0,count=0;
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); }
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"); sleep(4);
while(res1<1) { res1 = fcntl(file_desc, F_SETLKW, ®ion_2); if (res1 == -1) { count++; printf("Waiting\n"); //if (count ==20) //exit(1); } else { res1=1;
//close(file_desc); //exit(EXIT_SUCCESS); }