Return-Path: william@bourbon.usc.edu Delivery-Date: Sat Sep 6 13:58:24 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m86KwOa1025345 for ; Sat, 6 Sep 2008 13:58:24 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m86KuwGI027661 for ; Sat, 6 Sep 2008 13:56:58 -0700 Message-Id: <200809062056.m86KuwGI027661@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: CS551 confusing about child thread? Date: Sat, 06 Sep 2008 13:56:58 -0700 From: Bill Cheng Someone wrote: > I declared a global variable (e.g. NumOfChildThread =0) to keep track > total numbers of child threads in server. > > But whenever I do NumOfChildThread++ in fork() loop, the parent's > NumOfChildThread doesn't increase accordant with child thread. > > It seems like child thread CREATE his own NumOfChildThread and this new > variable doesn't has any relation with parents. > > In this situation, how do I keep track child thread, because since each > variable or array define in main() cannot synchronize with child thread. If you are using child "thread", you should not fork()! Forking is to create a child "process". A child process does *not* share memory content with its parent process! I think what you want is pthread_create() and not fork(). If you want to use fork(), you need to increment the number of child variable in the parent process. -- Bill Cheng // bill.cheng@usc.edu