Results 1 to 2 of 2

Thread: Can any one tell me why this doesnt work

Hybrid View

  1. #1
    TaraMc
    Guest

    Can any one tell me why this doesnt work

    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);
    }
    
    }

  2. #2
    JimH
    Guest

    Re:Can any one tell me why this doesnt work

    Quote Originally Posted by TaraMc
    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.
    It looks to me from your code that after you read the file you don't release the read lock you put on the file. When you then go and try to write to it you get the message that it is already locked.

    Jim H

Similar Threads

  1. DOesnt let me do ./configure
    By Gaweph in forum Linux - Software, Applications & Programming
    Replies: 18
    Last Post: 04-21-2004, 09:05 PM
  2. doesnt get much better then this...
    By TeRG in forum General Chat
    Replies: 13
    Last Post: 11-01-2003, 08:19 AM
  3. MOZILLA FIREBIRD DOESNT LIKE PNG!!!
    By sujoki in forum Linux - Software, Applications & Programming
    Replies: 7
    Last Post: 09-20-2003, 09:36 PM
  4. Intel onboard sound doesnt work?
    By rick420 in forum Linux - General Topics
    Replies: 5
    Last Post: 06-07-2003, 08:27 PM
  5. Nvoption doesnt do anything for me?
    By rick420 in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 07-09-2002, 07:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •