Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 29 Dec 1999 12:13:52 -0800 (PST)
From:      Ken Bolingbroke <hacker@bolingbroke.com>
To:        Steffen Merkel <d_f0rce@gmx.de>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: Sorry, but another thread problem!
Message-ID:  <Pine.BSF.4.05.9912291210510.64465-100000@fremont.bolingbroke.com>
In-Reply-To: <000701bf5238$9918aa80$0201a8c0@blade>

next in thread | previous in thread | raw e-mail | index | archive | help

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9912291210510.64465-100000>