Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: File locking within zsh?



Lloyd Zusman <ljz@xxxxxxxxxx> writes:

> "Brian K. White" <brian@xxxxxxxxx> writes:
> 
> > From: "Tim Writer" <tim@xxxxxxxxxxx>
> >
> >> [ ... ]
> >>
> >>    while ! ln file file.lock 2>/dev/null
> >>    do
> >>        sleep 1
> >>    done
> >>    # Lock obtained
> >>
> >>    print foo bar baz >>file
> >>    # do a whole lot of other stuff to "file"
> >>
> >>    rm -f file.lock
> >>    # Lock released
> >>
> >> Wrapping this idiom into lock/unlock functions is left as an exercise
> >> for the
> >> reader. :-)
> >
> >
> > Is that better or worse, and why, than  umask 222 ; >file ?
> >
> > http://www.unix.org.ua/orelly/unix/upt/ch45_36.htm
> 
> Thanks to both of you (Brian K. White and Tim Writer).  I personally
> like the umask version better, because I think I can implement a
> variation of it without spawning any extra processes (my goal), given
> that umask is an internal zsh command.

Keep in mind that you have to put umask in a subshell, like this:

    ( umask 222; >file )

in order to preserve it. IIRC, zsh avoids forking in subshells but don't most
shells implement subshells with fork()?

Keep in mind that ln is a tiny wrapper around the link(2) system call and so
there's only a very small amount of overhead in running it. With zsh, you
can have your cake and eat it too using the zsh/files which makes ln a
builtin (among other things).

-- 
tim writer <tim@xxxxxxxxxxx>                                  starnix inc.
647.722.5301                                      toronto, ontario, canada
http://www.starnix.com              professional linux services & products



Messages sorted by: Reverse Date, Date, Thread, Author