From owner-freebsd-threads@FreeBSD.ORG Tue Dec 7 10:00:28 2010 Return-Path: Delivered-To: freebsd-threads@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 25D36106566B for ; Tue, 7 Dec 2010 10:00:28 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id EF3888FC14 for ; Tue, 7 Dec 2010 10:00:27 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id oB7A0R6h025618 for ; Tue, 7 Dec 2010 10:00:27 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id oB7A0RqX025593; Tue, 7 Dec 2010 10:00:27 GMT (envelope-from gnats) Date: Tue, 7 Dec 2010 10:00:27 GMT Message-Id: <201012071000.oB7A0RqX025593@freefall.freebsd.org> To: freebsd-threads@FreeBSD.org From: Vasil Dimov Cc: Subject: Re: threads/79887: [patch] freopen() isn't thread-safe X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Vasil Dimov List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Dec 2010 10:00:28 -0000 The following reply was made to PR threads/79887; it has been noted by GNATS. From: Vasil Dimov To: bug-followup@FreeBSD.org, tejblum@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@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 #include #include #include #include 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--