From owner-freebsd-hackers Wed Dec 29 12:13:59 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from fremont.bolingbroke.com (adsl-216-102-90-210.dsl.snfc21.pacbell.net [216.102.90.210]) by hub.freebsd.org (Postfix) with ESMTP id DB69E151D1 for ; Wed, 29 Dec 1999 12:13:56 -0800 (PST) (envelope-from ken@bolingbroke.com) Received: from localhost (ken@localhost) by fremont.bolingbroke.com (Pro-8.9.3/Pro-8.9.3) with ESMTP id MAA65153; Wed, 29 Dec 1999 12:13:52 -0800 (PST) Date: Wed, 29 Dec 1999 12:13:52 -0800 (PST) From: Ken Bolingbroke X-Sender: ken@fremont.bolingbroke.com To: Steffen Merkel Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: Sorry, but another thread problem! In-Reply-To: <000701bf5238$9918aa80$0201a8c0@blade> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG From my own experience, altho I'm not that skilled a programmer, sleep() is not thread-safe. I believe sleep() sets a global SIGALARM, which is reset by every thread that calls it, and thus only the last one ever returns. Replacing sleep() with nanosleep() or something else that is thread-safe should solve that problem. Ken Bolingbroke hacker@bolingbroke.com On Wed, 29 Dec 1999, Steffen Merkel wrote: > Hello, > > first let me thank all of you for all your great help and the dozens > of responses I got. > Unfortunately I have a very strange problem in a multithreaded > programm I'm writing now. I want to write a programm which > checks if a server is up by pinging it. I looks like that: > > ###################################### > main(){ > readconfig() > > for (every server){ pthread_create(..... startscan() ); } > > while(1){ > sleep(1); > printf("Main Awake again\n"); > } > } > > startscan(){ > ping(server); > printf("Going to sleep\n"); > sleep(1); > printf("Awake again!"); > } > ######################################## > > I can see that the servers are getting pinged and that every > thread goes to sleep. As soon as every thread did it's job and > the first thread should awake the program get's a SIGSEGV. > I can't see the message "Awake again" from the first thread but > I saw that the first thread started to sleep and the last thread > finished too and the main thread printed "Awake again!". > Well with my little knowledge of C I would say that there is a > problem with the sleep function. But as soon as I remove the > ping() function the programm operates normally and runs forever. > > Can anyone tell me how I can debug such a problem? A program > which seems to crash because of function which doesn't get executed > at crash time. > > Meanwhile I'm going mad with those threads because the program > worked fine using fork(). Unfortunately it takes too much ram to > fork a process for every server. That's the reason I want to use > threads. > > Steffen > > PS: Sorry that I have to bother you again with my incompetence. > > Please reply directly to me because I'm not on the list. > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message