I am writing a small distributed application as a university assignment. So far I have on the client side a nice little menu system written using curses, the value of the chosen menu item is sent down a TCP/IP socket at which point i would like to use this value to determine how a particular file is opened. I have a poece of test code sort of working, if I chose to write to the file first then everything is ok, but if i chose to read only first then and then open another Konsole and ask to write I am told that the file is already locked. I have inserted debug lines and discovered that it isn't the initial read that has locked the file for writing so how can I find out what has.
The code for locking the files is: (think when I cut the code there might be a bracket missing at the end so don't worry about that)
Code:
if(buf[0] == 'w')
{
if((fd = open("test.dat", O_RDWR)) < 0)
{
perror("OPEN");
exit(EXIT_FAILURE);
}

/*Set up the write lock on last 100 bytes of the file*/

lock.l_type = F_WRLCK;
lock.l_whence = SEEK_END;
lock.l_start = 0;
lock.l_len = 100;

/*Set the lock*/

if(!fcntl(fd, F_SETLK, &lock))

puts("Write lock established");
else
{
fcntl(fd, F_GETLK, &lock);
printf("write locked by PID %d
", lock.l_pid);
}
}
else if(buf[0] == 'v')
{

/*Do read lock*/if((fd = open("test.dat", O_RDONLY)) < 0)
{
perror("OPEN");
exit(EXIT_FAILURE);
}

lock.l_type = F_RDLCK;
lock.l_whence = SEEK_END;
lock.l_start = 0;
lock.l_len = 100;

if(!fcntl(fd, F_SETLKW, &lock))

puts("read lock established");

else
{
fcntl(fd, F_GETLK, &lock);
printf("read locked by PID %d
", lock.l_pid);
}

}