From owner-freebsd-hackers@FreeBSD.ORG Sat Jun 4 00:47:14 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 6845F16A41C for ; Sat, 4 Jun 2005 00:47:14 +0000 (GMT) (envelope-from ray@redshift.com) Received: from outgoing.redshift.com (outgoing.redshift.com [207.177.231.8]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4379943D48 for ; Sat, 4 Jun 2005 00:47:14 +0000 (GMT) (envelope-from ray@redshift.com) Received: from workstation (216-228-19-21.dsl.redshift.com [216.228.19.21]) by outgoing.redshift.com (Postfix) with SMTP id AE2D5970CF; Fri, 3 Jun 2005 17:47:12 -0700 (PDT) Message-Id: <3.0.1.32.20050603174714.00a44990@pop.redshift.com> X-Mailer: na X-Sender: redshift.com Date: Fri, 03 Jun 2005 17:47:14 -0700 To: Aziz Kezzou , freebsd-hackers From: ray@redshift.com In-Reply-To: <3727392705060316555071c4ad@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Cc: Subject: Re: Fork mystries.... X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Jun 2005 00:47:14 -0000 Aziz, Fork is for process creation. Look up pthread_create() and/or POSIX thread creation, etc. You should be able to find a lot of info on google with a quick search: http://math.arizona.edu/~swig/documentation/pthreads/ From the OS standpoint a process is like a different program when you fork it. A thread is more like a single program with a bunch of subsections all running at the same time, then the OS and CPU jump back and forth giving a little run-time to each subsection. Threads share a common space; processes [as far as I recal] do not (which is where inter process communication comes into play). Anyway, have fun! :) Ray At 07:55 PM 6/3/2005 -0400, Aziz Kezzou wrote: | 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 ? | | Example : | ---------------------------- | int counter = 0; | int main() { | if( fork()==0) { | while(1) { | sleep(1); | counter++; | printf("Son : counter = %d\n", counter); | } | } else { | while(1) { | sleep(1); | printf("Parent : counter = %d\n", counter); | } | } | return 0; | } | ---------------------------- | | All I get is : | Parent : counter = 0 | Son : counter = 1 | Son : counter = 2 | Parent : counter = 0 | Son : counter = 3 | Parent : counter = 0 | Son : counter = 4 | Parent : counter = 0 | | why counter isn't shared between the two threads ??! | thanks, | -aziz | _______________________________________________ | freebsd-hackers@freebsd.org mailing list | http://lists.freebsd.org/mailman/listinfo/freebsd-hackers | To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" | |