Return-Path: william@bourbon.usc.edu Delivery-Date: Mon Sep 8 20:04:31 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 m8934Vfc030364 for ; Mon, 8 Sep 2008 20:04:31 -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 m8933c4f013124 for ; Mon, 8 Sep 2008 20:03:38 -0700 Message-Id: <200809090303.m8933c4f013124@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: dynamic array for pid list Date: Mon, 08 Sep 2008 20:03:38 -0700 From: Bill Cheng Someone wrote: > Why do we need mutex for the global pid's? > Since the server code which will increment/decrement the counter or > manipulate the pid's is a single thread we dont need any mutex right? Let's say that you use a linked list. If you are trying to insert a node into the linked list and this operation got interrupted by an interrupt at a bad time, your pointer will get messed up. Then your code will crash later. -- Bill Cheng // bill.cheng@usc.edu On Mon, Sep 8, 2008 at 6:52 PM, Bill Cheng wrote: > Someone wrote: > > > I created a static array of size 50 for handling the child > > processes but I am having trouble managing the "int pid_list[50]" > > list > > > > Whenever I create a child process, I increment a global counter > > (and add the pid to the pid_list) in the server to reflect this > > and so when I break out of my "forever" loop to handle a server > > timeout. I try to decrement this global counter. Then I parse the > > list to remove the pid from this list. I use a temporary array to > > store the new pid_list but i get errors assigning this (address > > of) new list temp (smaller pid list) as my pid list. > > > > Any other suggestions to do this? I know you mentioned the > > dynamic array but that seemed to require realloc() each time and > > I'm not sure it's a better implementation anyways. > > You can also use a linked list. By the way, just because > you have a dynamic array, it doesn't mean that you have to > call realloc() every time. You can increase its size, when > needed, by 50 objects at a time. > > > I was also worried about mutex for the global pid counter set in > > the main server program that I am modifying in a SIGCHLD handler. > > Should we handle this? > > You can use sigprocmask() to make sure that you won't get > into a deadlock situation. Please note that sigprocmask() > can only be used if you use child processes. If you use > child threads, you should use pthread_sigmask(). > -- > Bill Cheng // bill.cheng@usc.edu > >