*BSD News Article 8978


Return to BSD News archive

Organization: Sophomore, Math/Computer Science, Carnegie Mellon, Pittsburgh, PA
Path: sserve!manuel.anu.edu.au!munnari.oz.au!uunet!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!andrew.cmu.edu!aw2t+
Newsgroups: comp.unix.bsd
Message-ID: <Ef9VY1S00iV082EFob@andrew.cmu.edu>
Date: Wed,  9 Dec 1992 11:04:17 -0500 
From: "Alex R.N. Wetmore" <aw2t+@andrew.cmu.edu>
Subject: file locking problems
Lines: 40

My machine seems to have problems with file locking.  I have written
some bbs software that runs under 386bsd, and it appears that when two
processes try and lock the same file at the same time that the machine
will lock up.  When it first happens nothing in the ~bbs directory can
be accessed, but everything else works fine.  Once something in the ~bbs
directory is accessed the machine seems to go into a race state (you can
type, and it will echo characters, but won't pay attention to anything
else).  At this point I have to reboot the machine.

It always appears to happen with the bbs user file (when I reboot the
file has been truncated, so it appears to hang when the two processes
first open the file).  Here is the code from that section of the
software:
void writeusers(int read) {
    if (read) {
        u_us = user[u];
        readusers();
        user[u] = u_us;
    }
    fo = open(F_USERS, O_WRONLY | O_TRUNC | O_CREAT | O_EXLOCK, 660);
    if (fo == -1) {
        sputs("error opening users.dat for write");
        writelog("$ writeusers: error opening users.dat for write");
        killconnection(K_FERR);
    }
    write(fo, user, sizeof(urec) * usernum);
    close(fo);
}

So, it just uses the open command to lock the file.  Should I be using
flock or something else?  This bug has really gotten annoying, because
the system will just crash when 4 or 5 people get on the system (and the
user file becomes busy a lot).

BTW, if you want to poke around on the system from the user end, it is
phred.pc.cc.cmu.edu.

thanks,
alex