From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII X-Google-Thread: f7344,3ca4240a4c402bae X-Google-Attributes: gidf7344,public X-Google-Thread: fee84,3ca4240a4c402bae X-Google-Attributes: gidfee84,public X-Google-Thread: fc89c,3ca4240a4c402bae X-Google-Attributes: gidfc89c,public X-Google-Thread: ffc1e,3ca4240a4c402bae X-Google-Attributes: gidffc1e,public X-Google-Thread: 11035d,3ca4240a4c402bae X-Google-Attributes: gid11035d,public From: Bil Lewis Subject: Re: pthreads and sockets Date: 1996/12/06 Message-ID: <32A8A2EA.4097@LambdaCS.com> X-Deja-AN: 202755619 distribution: inet references: <589ceq$g76$1@asgard.sprintlink.co.za> cc: bil@lambdacs.com content-type: text/plain; charset=iso-8859-1 organization: MediaCity World http://www.mediacity.com mime-version: 1.0 newsgroups: comp.programming.threads,comp.unix.osf.osf1,comp.unix.solaris,comp.unix.programmer,comp.os.vms x-mailer: Mozilla 2.01 (X11; I; SunOS 5.5 sun4m) Philip, Here's my two-bits worth. Mind you, I am not a network dude, so my thoughts on that part is questionable. First, using threads is a fine idea. You'll like the results. I know that VMS implements pthreads, as does Solaris 2.5 and DEC UNIX. There's a nice article by Doug Schmidt & Steve Vinoski on this kind of programming in C++ report April '96. I also include some of my opinions (soon to be in a book) below. You can look at some comparative code which does this on my web page (below). -Bil philip Shearer wrote: > > I am assessing the possibility of replacing a network communications > subsystem with a TCP/IP implementation using pthreads. It is the > first pthreaded application I have attempted , but I have a lot > of experience of this type of application on various UNIX systems > using sockets and fork(ing). > > At the moment the sub-system runs as a server on VMS and a client > on MS-Windows using UDP on one PORT. Built into the comms layer is > an in-house reliable connection mechanism between the VMS server > MS-Windows clients. > > The current sub-system works for about 550 connections but it is > starting to become flaky as more clients are added. > > The development (of the server software) will be done on Solaris > x86 V 2.5.0 and Digital UNIX 4.0. > > Ideally the server code should be portable to a VMS platform. > > Is is there anything that we need to do to the kernels of the > 3 systems to allow for 1000 TCP connections? > > Is there any software on the Net which does this task or > something similar. > > If not then at the moment I envisage having : > > One thread accepting connections and creating one new thread > per connection. (Accepter) > > One thread read-blocking on the each socket connection. Each > thread will write to an internal IPC that is connected to the > main application.(Reader) 1000 is a lot of threads. Probably better to do a single select() and place the request on a queue for a producer/consumer model. > One thread will read from an internal IPC to receive processed > messages from the main application. (Dispatcher) > > One thread per-connection which will block waiting for the > Dispatcher to send it a message with it will send out onto > the network. (Writer) > > Is there a simple method by which the Dispatcher can multiplex > to the correct Writer (assuming that any info needed to do this > can be prefixed to the outgoing message by the main application) > Or will I be forced to implement some sort of structure/lookup table > containing a mutex lock for each Writer? Don't understand the question. > > Is there a better way to write this sort of code? > > -- > Regards Philip Shearer > > eMail: philips@asiatech.co.za > Tel +27 (0)11 784 8925 > Fax +27 (0)11 784 8919 -- ================ Bil@LambdaCS.com http://www.LambdaCS.com Lambda Computer Science 555 Bryant St. #194 Palo Alto, CA, 94301 Phone/FAX: (415) 328-8952 ===================================================== Program Design There is a small number of high-level design strategies which have been discussed in several books. [fn: "Programming with Threads", SunSoft Press; "Multithreaded Programming with Windows NT", Prentice Hall.] Notably these names are not used completely uniformly. They are: � Master/Slave: One thread does the main work of the program, creating other threads to help in some portion of the work. � Master/Slave (Thread per Request): One thread listens for requests, then creates a new thread to handle each request. � Master/Slave (Thread per Client): One thread listens for new clients to attach, then creates a new thread to handle each client. The thread is dedicated to its client, doing work only for that client. � Produce/Consumer (Work Queue or Workpile): Some threads create work requests, and put them on a queue. Other threads take the work requests off of the queue and execute them. � Dogpile: Similar to the workpile, but instead of maintaining a queue, all the threads look for work directly. � Pipeline: Each thread does some work on a task, then passes the partially completed task to the next thread. In the discussion below, we will elaborate on each of the designs and include some sample code. All of the code will be based on a client/server program which takes in requests from a socket, processes them, and sends replies back out over the same socket fd. The complete code for three versions of the program (Thread per Request, Producer/Consumer, and non-threaded) are on the web site. Master/Slave The master/slave design is the most obvious for many kinds of tasks. In its most elemental form, it will be implemented by a library and the programmer will not even be aware of there being multiple threads. A matrix multiply routine (such as Dakota Scientific's) may well spawn a set of threads to do the work, but all the programmer knows is that she called matrix_multiply(). Master/Slave (Thread per Request) This is a M/S design for client/server programs. The master thread will do the listening. In the fragment of the socket program below, each time a new request comes in from a client, the main thread spawns off a thread to handle that request. The main thread then returns to its accept() loop while the thread works on the request independently, exiting when it's done. Code�Example�8-13 Master/Slave Socket Example main() { ... while(TRUE) {newsockfd = accept(sockfd, &cli_addr, &clilen); pthread_create(NULL, &thr_attr, process_request, newsockfd); } } void *process_request(void *arg) { read(mysocfd, data, LENGTH); result = process_data(data); write(mysocfd, result, LENGTH); close(mysocfd); pthread_exit(); } While this design has some positive aspects to it, e.g. simplicity and directness, it also admits to some drawbacks. The cost of thread creation is not going to be significant unless the task itself is very short (< 10ms). Of more significance is that the programmer has no control over the number of threads running at any one time. Should there be a sudden spike in the number of requests, there will be a equal spike in the number of threads, causing performance degradation due to the excessive number of threads competing for the same locks, CPUs, and other resources. Rewriting the program to limit the number of threads would be somewhat ugly, and there are better ways of handling the problem. This is probably not a good design for any program! Producer/Consumer In the producer/consumer model, the programmer can exert full control over the number of threads with very little effort. The threads may be created at startup time, and then left to wait for work appearing on the queue. Should some of the threads never be used, it will be no great cost -- probably immeasurable. Should there be too many incoming requests, they can be placed on the queue and handled when convienent. Code�Example�8-14 Producer/Consumer Socket Example main() { for (i=0; i