Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 8 Nov 2008 15:58:57 +0200
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Manish Jain <unxfbsdi@gmail.com>
Cc:        freebsd-fs@freebsd.org
Subject:   Re: A problem with fork() and subsequent flock()
Message-ID:  <20081108135857.GH18100@deviant.kiev.zoral.com.ua>
In-Reply-To: <4914D501.4090400@gmail.com>
References:  <4914D501.4090400@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--BaAhobP9R+klFZfp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Nov 08, 2008 at 05:23:37AM +0530, Manish Jain wrote:
>=20
> Hi,
>=20
> I am starting out as a C/C++ programmer on FreeBSD and I am stuck with a=
=20
> small but irritating problem. I was trying out the flock() call and=20
> wrote flocksample.cpp, which starts out with a fork() and subsequent=20
> calls to flock() from both processes (parent as well as child; the child=
=20
> does an initial sleep(1) before anything else). It compiles okay and the=
=20
> parent's  flock() call succeeds. But the child's flock() call too=20
> succeeds on the same file descriptor even before the first flock()=20
> unlocks. Can anyone please point out where the problem is ? I am not=20
> even sure whether the problem is FreeBSD specific.
This is right behaviour for flock. Note that manpage specifies that
lock is attached to file. When you open a filedescriptor, you get
the structure like that
	fd -> file -> vnode
,-> means references.

Fork makes a copy of all fds opened in the process, and structure
becomes

	fd [1] -> file -> vnode
	fd [2] /
where fd[1] lives in parent, and fd[2] in child. Lock is still attached
to file, so both parent and child share the lock.

--BaAhobP9R+klFZfp
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (FreeBSD)

iEYEARECAAYFAkkVmyEACgkQC3+MBN1Mb4h12ACfX/o6eL51AYGJRU8quBDpzQei
tJsAnj83rJWWyxXf5R3SyyeW1AY3AqzE
=Hnkw
-----END PGP SIGNATURE-----

--BaAhobP9R+klFZfp--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20081108135857.GH18100>