From owner-freebsd-hackers@FreeBSD.ORG Fri Jun 3 23:55:35 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CB72316A41C for ; Fri, 3 Jun 2005 23:55:35 +0000 (GMT) (envelope-from french.linuxian@gmail.com) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.195]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8451543D1D for ; Fri, 3 Jun 2005 23:55:35 +0000 (GMT) (envelope-from french.linuxian@gmail.com) Received: by zproxy.gmail.com with SMTP id 12so1293387nzp for ; Fri, 03 Jun 2005 16:55:29 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=nDyKRcamTQ4hFtM1k4ugdWwbYFVfzUsL2zR7JRKvBYDe9cgJIWNV5ZdwAuuNbVQsJ5sonP9zcPkw0MSAmJlPv7jcck/EPxQVeg6dLcrQY3XCSS4yQf0GU7nGBGiRM6oUx8oODFtVDBt4iT2W6wYs0qLhsuDC2U3qunlsdgNKSas= Received: by 10.36.222.57 with SMTP id u57mr1555321nzg; Fri, 03 Jun 2005 16:55:29 -0700 (PDT) Received: by 10.36.58.12 with HTTP; Fri, 3 Jun 2005 16:55:29 -0700 (PDT) Message-ID: <3727392705060316555071c4ad@mail.gmail.com> Date: Fri, 3 Jun 2005 19:55:29 -0400 From: Aziz Kezzou To: freebsd-hackers Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Subject: Fork mystries.... X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Aziz Kezzou List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Jun 2005 23:55:35 -0000 Hi all, It's probably not the right mailing list to ask but I am really surprised about global variable sharing in a multithreaded C application. If I remember well my multithreading course global variables are shared between threads, right ? =20 Example : ---------------------------- int counter =3D 0; int main() { if( fork()=3D=3D0) { while(1) { sleep(1); counter++; printf("Son : counter =3D %d\n", counter); =20 } =20 } else { while(1) { sleep(1); printf("Parent : counter =3D %d\n", counter); =20 } }=09=20 return 0; } ---------------------------- All I get is : Parent : counter =3D 0 Son : counter =3D 1 Son : counter =3D 2 Parent : counter =3D 0 Son : counter =3D 3 Parent : counter =3D 0 Son : counter =3D 4 Parent : counter =3D 0 why counter isn't shared between the two threads ??! thanks, -aziz