From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA20099; Thu, 9 Aug 2001 10:55:09 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA20299 for ; Thu, 9 Aug 2001 10:55:08 +0200 (MET DST) Received: from drazi.auctionwatch.ro (nat-192.auctionwatch.ro [193.226.187.192]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f798t5512076; Thu, 9 Aug 2001 10:55:06 +0200 (MET DST) Received: from auctionwatch.ro (IDENT:costins@scostin.auctionwatch.ro [172.16.0.87]) by drazi.auctionwatch.ro (8.9.3/8.9.3) with ESMTP id LAA14744; Thu, 9 Aug 2001 11:55:06 +0300 Message-ID: <3B7255E2.1C214D51@auctionwatch.ro> Date: Thu, 09 Aug 2001 12:20:34 +0300 From: Costin Stefan Organization: AuctionWatch Romania X-Mailer: Mozilla 4.72 [en] (X11; U; Linux 2.2.14-5.0 i686) X-Accept-Language: en MIME-Version: 1.0 To: David Mentre CC: caml-list@inria.fr Subject: Re: [Caml-list] Thread and sockets References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk David Mentre wrote: > Hello, > > I'm building a Caml client/server program using posix threads and > sockets. Right now, I have 3 threads: > > 1. normal thread for user computation > > 2. a thread doing a blocking call to Unix.accept to setup new > connections > > 3. a thread doing a blocking call to Thread.select (with infinite > timeout) to get messages on opened connections First of all read the man pages for read/write/select of the C system calls. > > > I've observed that my design is bad and I have several race > conditions. Worse problems are when I open and close connections. For > example, if thread (3) is blocked and a new connection is opened by > thread (2), thread (3) won't read new messages. I have similar problems > when sockets are closed. How to do it properly? A better approach (for thread 3) is : do forever{ 1. a select with a timeout. 2. When the select exit see if have data available or timeout occurred ! 3. Then you can test if exists new accepted connections. } > > > I think I should open a dedicated thread for each new socket [1]. > However I don't know how to stop a thread waiting inside a blocking > call. What happens if the remote part close the socket where a thread is > doing a select (or a read). Does this thread get an exception? When you try to read from a socket and the remote part close the socket you will receive a SIGPIPE signal (try to handle this ;-) ) > > > I also don't know how to stop cleanly thread (2). Should another thread > close the socket where thread (2) is doing an Unix.accept on? > > I would accept with pleasure any advice on this subjet. > > Best regards, > david > > [1] At first, I did not choose this design to avoid creating zillions of > new posix (i.e. kernel, on Linux) threads. Who many simultaneous connections do you want to handle ? Is enough a single thread for this ? > > -- > David.Mentre@inria.fr > Opinions expressed here are only mine. > ------------------- > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ > To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr