// The lock1.c/* program first creates a file and opens it in a readable way. Then, it adds some content to the file, and then sets two regions in the file, the first region is 10-30 bytes and the shared lock is used. The second region is 40-50 bytes and the exclusive lock is used. Then the program calls the fcntl function to lock the two regions, and wait one minute before closing the file and exiting the Program */# include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <fcntl. h> const char * test_file = "/tmp/test_lock"; int main () {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) {fprintf (stderr, "unable to open % s for read/write \ n", test_file); exit (exit_failure);} For (byte_count = 0; byte_count <100; byte_count ++) {write (file_desc, byte_to_write, 1);}/* set the 10-30 bytes of the file to Region 1 and set the shared lock */region_1.l_type = f_rdlck; region = seek_set; region_1.l_start = 10; region_1.l_len = 20;/* set the 40-50 bytes of the file to Region 2 and set the exclusive lock on it */region_2.l_type = f_wrlck; region = seek_set; region_2.l_start = 40; region_2.l_len = 10; 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");} res = fcntl (file_desc, f_setlk, ion_2 ); if (RES =-1) {fprintf (stderr, "failed to lock Region 2 \ n");} Sleep (60 ); printf ("process % d closing file \ n", getpid (); close (file_desc); exit (exit_success); Return 0 ;}//lock2.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>const char *test_file = "/tmp/test_lock";#define SIZE_TO_TRY 5void show_lock_info(struct flock *to_show);int main(){ int file_desc; int res; struct flock region_to_test; int start_byte; file_desc = open(test_file,O_RDWR | O_CREAT,0666); if(!file_desc) { fprintf(stderr,"Unable to open %s for read/write",test_file); exit(EXIT_FAILURE); } for(start_byte = 0;start_byte < 99;start_byte++) { region_to_test.l_type = F_WRLCK; region_to_test.l_whence = SEEK_SET; region_to_test.l_start = start_byte; region_to_test.l_len = SIZE_TO_TRY; region_to_test.l_pid = -1; printf("Testing F_WRLCK on region from %d to %d\n",start_byte,start_byte+SIZE_TO_TRY); res = fcntl(file_desc,F_GETLK,ion_to_test);if(res == -1){ fprintf(stderr,"F_GETLCK falied\n"); exit(EXIT_FAILURE);}if(region_to_test.l_pid != -1){ printf("Lock would fail.F_GETLK returned.\n"); show_lock_info(ion_to_test);}else{ printf("F_WRLCK - LOCK would succeed\n");}region_to_test.l_type = F_RDLCK;region_to_test.l_whence = SEEK_SET;region_to_test.l_start = start_byte;region_to_test.l_len = SIZE_TO_TRY;region_to_test.l_pid = -1;printf("Testing F_RDLCK on region from %d to %d\n",start_byte,start_byte+SIZE_TO_TRY);res = fcntl(file_desc,F_GETLK,ion_to_test);if(res == -1){ fprintf(stderr,"F_GETLCK falied\n"); exit(EXIT_FAILURE);}if(region_to_test.l_pid != -1){ printf("Lock would fail.F_GETLK returned.\n"); show_lock_info(ion_to_test);}else{ printf("F_RDLCK - LOCK would succeed\n");}}close(file_desc);exit(EXIT_SUCCESS);}void show_lock_info(struct flock *to_show){ printf("\tl_type %d",to_show->l_type); printf("\tl_whence %d",to_show->l_whence); printf("\tl_start %d",to_show->l_start); printf("\tl_len %d",to_show->l_len); printf("\tl_pid %d",to_show->l_pid);}
To test the lock, run lock1 (running in the background) and then lock2.
$./Lock1 &
[1] 6321
[Anpan @ anpan Data Management] $ process 6321 locking file
Process 6321 closing File
./Lock2
Testing f_wrlck on region from 0 to 5
F_wrlck-lock wocould succeed
Testing f_rdlck on region from 0 to 5
F_rdlck-lock wocould succeed
Testing f_wrlck on region from 1 to 6
F_wrlck-lock wocould succeed
Testing f_rdlck on region from 1 to 6
F_rdlck-lock wocould succeed
....................................