Have a Question?
Print

00271: C program that demonstrates bbx style lock method

Title:

C program that demonstrates bbx style lock method

Description:

The following C programs can be used to test locking capabilities of an operating system. 

The first C program, when compiled, should be run before the second. It opens a file “lcktst” for READ/WRITEaccess and write locks the first 10 bytes and loops forever. If you run it in background, you can then attempt to open the file in BBx and LOCK(chan)the channel. The LOCK(chan) should fail. 

This version of lock.c creates a file called “lcktst” and locks it. 

Compile and run this program and background it since it locks the file and sleeps forever, then run another instance of this program in the same directory. The second instance should fail when it tries to lock the file and get a errno=13. 

To background it run with the & at the end, as in: 

lock & 

——————————– cut here ———————————- 

#include<stdio.h> 
#include<fcntl.h> 
#include<errno.h> 

#define LCKFILE “./lcktst” 

#define LCKLEN 10 

static struct flock lock_info; 

main() 

int fd; 

if ((fd = open(LCKFILE,O_RDWR)) < 0 ) { 
printf(“open failed: %s\n”,LCKFILE); 
exit(1); 


/* 
* set up to lock LCKLEN bytes of file 
*/ 
lock_info.l_type = F_WRLCK; 
lock_info.l_whence = 0; 
lock_info.l_start = 0; 
lock_info.l_len = LCKLEN; 

if (fcntl(fd, F_SETLK, &lock_info)) { 
printf(“lock failed: %d\n”,errno); 
exit(1); 


while(1) 
sleep(1); 


———————————————————————————————————— 
The second C program, when compiled, creates a test file “tfile” of 10000 bytes and writes a pattern to the file. It then seeks to the beginning of the file and locks every other byte with a read lock and reads the data from the location and verifies it is what it expects. A comment in the code says the test should be modified also to set a write lock for verifying that the write lock works also. If either of these programs fail, I’d say they have a problem with their NFS system. 

—————————- cut here for second version of lock.c ———————– 

#include <stdio.h> 
#include <fcntl.h> 

extern int errno; 

main() 

int fd, i, ret; 
char c, buf[80]; 
struct flock locktype; 

printf(“Creating file.\n”); 
unlink(“tfile”); 
fd = open(“tfile”, O_CREAT|O_RDWR|O_EXCL, 0666); 

if (fd == -1) 

perror(“Cannot create file. “); 
exit(errno); 


printf(“Writing file.\n”); 

/* 
* Create a file of 10000 bytes which cycle through the value 0 – 255. 
*/ 
for (i = 0; i < 10000; i++) 

c = (char ) (i & 0xff); 

if ((ret = write(fd, &c, 1)) == -1) 

perror(“Cannot write file. “); 
exit(errno); 




printf(“Locking and reading file.\n”); 
locktype.l_whence = 0; 
locktype.l_len = 1; 

/* 
* Lock and read every other byte in the file. Just for kicks, verify the 
* data read. 
*/ 
for (i = 0; i< 10000; i += 2) 

locktype.l_type = F_RDLCK;         /* Try using F_WRLCK also */ 
locktype.l_start = i; 

/* 
* Lock the byte and wait for the lock to return. 
*/ 
if ((ret = fcntl(fd, F_SETLKW, &locktype)) == -1) 

sprintf(buf, “Cannot lock record at %d. “, i); 
perror(buf); 
exit(errno); 


/* 
* Seek to the correct byte location for reading. 
*/ 
if ((ret = lseek(fd, (long)i, 0)) == -1) 

sprintf(buf, “Cannot seek to location %d. “, i); 
perror(buf); 
exit(errno); 


/* 
* Read the byte. 
*/ 
if ((ret = read(fd, &c, 1)) == -1) 

sprintf(buf, “Cannot read file at %d. “, i); 
perror(buf); 
exit(errno); 


/* 
* Validate the data. Error out if its invalid. 
*/ 
if (c != (char) (i & 0xff)) 

sprintf(buf, “Data mismatch at %d. Expected %c but got %c. “, 
         i, (char) (i&0xff), c); 
perror(buf); 
exit(errno); 


locktype.l_type = F_UNLCK; 

/* 
* Unlock the byte. 
*/ 
if ((ret = fcntl(fd, F_SETLKW, &locktype)) == -1) 

sprintf(buf, “Cannot unlock record at %d. “, i); 
perror(buf); 
exit(errno); 


/*write(1,”.”,1);                           /* Indicate we just read a byte. */ 


/*write(1,”\n”,1);                           /* All done, goto next line and leave.*/ 

printf(“Done.\n”); 
close(1); 
unlink(“tfile”);                           /* Don’t forget to wipe.          */ 



Last Modified: 07/27/1998 Product: PRO/5 Operating System: All platforms Unix

BASIS structures five components of their technology into the BBx Generations.

Table of Contents
Scroll to Top