threads/79887: [patch] freopen() isn't thread-safe
Vasil Dimov
vd at FreeBSD.org
Tue Dec 7 10:00:28 UTC 2010
The following reply was made to PR threads/79887; it has been noted by GNATS.
From: Vasil Dimov <vd at FreeBSD.org>
To: bug-followup at FreeBSD.org, tejblum at yandex-team.ru
Cc:
Subject: Re: threads/79887: [patch] freopen() isn't thread-safe
Date: Tue, 7 Dec 2010 11:52:00 +0200
--wac7ysb48OaltWcw
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Attached is a prog which demonstrates what happens when another thread
calls open() when freopen() is executing. The output is:
open(): /tmp/f1: 3
trx1: freopen() begins, will associate fd=3 with /tmp/f2
trx1: freopen(): open(): /tmp/f2: 4
trx1: freopen(): close(): 3: 0
trx2: open(): /tmp/f_other: 3
trx1: freopen(): dup2(): 4,3: 3
trx1: freopen(): end, now fd=3 is associated with /tmp/f2
trx1: write to fd=3 (supposed to be /tmp/f2)
trx2: write to fd=3 (supposed to be /tmp/f_other)
At the end /tmp/f2 contains "ABCXYZ", /tmp/f_other is empty.
--
Vasil Dimov
gro.DSBeerF at dv
%
The difference between life and the movies is that a script has to
make sense, and life doesn't.
-- Joseph L. Mankiewicz
--wac7ysb48OaltWcw
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="dup2.c"
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
int fd1;
int fd1_orig;
int fd_other;
int ret;
fd1 = open("/tmp/f1", O_RDWR | O_CREAT, 0644);
printf("open(): /tmp/f1: %d\n", fd1);
printf("trx1: freopen() begins, will associate fd=%d with /tmp/f2\n",
fd1);
fd1_orig = fd1;
fd1 = open("/tmp/f2", O_RDWR | O_CREAT, 0644);
printf("trx1: freopen(): open(): /tmp/f2: %d\n", fd1);
ret = close(fd1_orig);
printf("trx1: freopen(): close(): %d: %d\n", fd1_orig, ret);
/* another thread executes in the meantime, opening some file */
fd_other = open("/tmp/f_other", O_RDWR | O_CREAT, 0644);
printf("trx2: open(): /tmp/f_other: %d\n", fd_other);
/* end of another thread, freopen() continues */
ret = dup2(fd1, fd1_orig);
printf("trx1: freopen(): dup2(): %d,%d: %d\n", fd1, fd1_orig, ret);
fd1 = fd1_orig;
printf("trx1: freopen(): end, now fd=%d is associated with /tmp/f2\n",
fd1);
printf("trx1: write to fd=%d (supposed to be /tmp/f2)\n", fd1);
write(fd1, "ABC", 3);
printf("trx2: write to fd=%d (supposed to be /tmp/f_other)\n", fd_other);
write(fd_other, "XYZ", 3);
close(fd1);
close(fd_other);
return(0);
}
--wac7ysb48OaltWcw--
More information about the freebsd-threads
mailing list