Ideas: 'select'ing file descriptors


 
Thread Tools Search this Thread
Top Forums Programming Ideas: 'select'ing file descriptors
# 1  
Old 02-26-2004
Ideas: 'select'ing file descriptors

Hey everyone,

I'm writing a threaded database server application and need to communicate the results of several worker threads to a single multiplexed communication output thread. Basically, the communication output thread sits in a select statement waiting for its client sockets to dole out requests, connect, disconnect, and become ready for sending data. This thread is, therefore, using a select statement, and a round robin scheduling policy, to control its scheduling.

Now I have several other threads whos sole purpose is to take the longer, possibly blocking, tasks and construct a deliverable for the client. Of course, when they are through they need to send that data to the communication output thread who is sitting in the select waiting for socket events.

Well, unless I'm missing something there is no select call that will allow me to use typical thread synchronization with mutexes and condition variables and mix this with my other file descriptors. So I'm guessing my only option is to be able to interrupt the select. This means that either I need to signal the thread to break out of the select. This seems unreliable in my opinion given that the thread that handles said signal is randomly selected from a set of threads not having that signal blocked and therefore there exists a small window where a new thread is being created in which it can obtain a signal it is just about to tell the scheduler it wants to block.

Therefore, it seems I have no choice but to use a file descriptor and set its read, write, or execute condition such that when there is information waiting the select will be capable of seeing it and when there is no more data waiting I can remove said condition. This all, of course, needs to be mutex protected, but only when I bounce out of the thread do I need to attempt to attain said mutex.

Basically, I decided to use a pipe and following this procedure:

select for readability on read fd of pipe, when readable:

lock mutex
pull data off mutex protected in memory queue
if end of queue read char off pipe
unlock mutex
handle data

Then to signal this:

lock mutex
push data until mutex protected in memory queue
if first item put char on pipe
unlock mutex

That should work...but it seems wasteful to use up two file descriptors and an OS pipe buffer for a single character which is nothing more than an on/off switch. What I'm wondering is, for those who have made it this far, is there anything more lightweight I can use that could acheive the same behavior, or is how I'm going about this completely rediculous?

I'm looking for ideas...so any would be appreciated...if not it looks like I'm going to use the above method...I just wonder if there is something more standard that I could do or something better.

Thanks everyone,

Brian Gregg
Computer Sciences Corp.
# 2  
Old 02-26-2004
I didn't really follow all of that, but I would try to go in one of two directions...

1) Lose that memory queue
I would not put one byte on the pipe. I would put the whole shebang. Then the main thread would know to read, say, a 30 byte structure rather than a flag byte. This structure would be everything needed to process the request and there would be no other communication between the main thread and the requesting thread.

2) Lose that pipe
The select system call can be given a timeout so it return when a timer expires or i/o is possible. I would set the timer for, say, 1/10 of a second. Then when the select returns, the main thread would first service any fd's, then check the memory queue for any of those tasks.

And beware: you are dancing very close to a race condition. Between the time that a requester makes a request and the time that the main thread notices the request is a window of time. During that window other requests can arrive. So you must loop and service all outstanding requests.
# 3  
Old 02-26-2004
Quote:
Originally posted by Perderabo
I didn't really follow all of that, but I would try to go in one of two directions...

1) Lose that memory queue
--snip--

2) Lose that pipe
--snip--
1) Well, the memory queue is a requirement because the pipe has a byte limit and given the threaded nature and the need for mutex protection it is possible that I run up against a deadlock because the read thread is trying to lock a structure to handle a request and the write thread has it locked but can put it on the pipe because of the byte limit.

I found that out the hard way...and that's why the single byte on and off to prevent this.

2) That's so close to polling it might as well be polling and would decrease the overall responsiveness of the system because I'd have to wait for socket events, switch to wait for response events, and then back. This also wastes CPU cycles as most polling type activity does...and that's bad practice IMO.

As for the race condition...there really is not worry there. First the mutex will protect the memory queue. So a thread putting a request on it has a consistent view of whether it is the first to put a request on or not. The thread taking the data off then (after it locks the mutex) has a consistent view of whether that item is the "last" item. If another is to be added the mutex has to be aquired, which it can not be until the read thread either removes the byte and the request (or the write thread places the byte and the "first" request).

What I'm really looking for is a way to, maybe with fcntl, make a file (rather than a pipe...maybe one open to /dev/null) readable and then non-readable so that a select can see the readable flag set, and then I can clear it. This is acheived with the single byte outstanding on the pipe...but I'm looking for a cleaner way.

Thanks for the suggestions, if you have any more ideas I'd love to hear them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions about file descriptors

Hi, I'm playing with KSH I entered following command in terminal { echo "stdout" >&1; echo "stderr" >&2; } > out And I get only stoud in a new file out. My question is: Where did my stderr vanish ? (5 Replies)
Discussion started by: solaris_user
5 Replies

2. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

3. UNIX for Dummies Questions & Answers

Semaphores and File Descriptors

What is the difference between a file descriptor and a semaphore? My basic understanding is: - a file descriptor is a small positive integer that the system uses instead of the file name to identify an open file or socket. - a semaphore is a variable with a value that indicates the... (1 Reply)
Discussion started by: Mr_Webster
1 Replies

4. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

5. UNIX for Advanced & Expert Users

File Descriptors + cron

Hi All, This thread is going to be a discussion basically bringing out more information from the experts on cron jobs and the associated file handles. So, here is the question. There is definitely a constant ' n ' as the maximum number of file handles alloted to a process ' p '. Will... (7 Replies)
Discussion started by: matrixmadhan
7 Replies

6. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

7. Shell Programming and Scripting

File descriptors problem perplexing me

Greetings, I have a troubling problem with a Korn Shell concept that I know works in Solaris. Essentially I am assigning file descriptors to a coprocess. Also, it should be noted that I am not using the public domain ksh but, rather AT&T ksh93. Here is a test scenario: $ sqlplus -s... (5 Replies)
Discussion started by: tmarikle
5 Replies

8. UNIX for Dummies Questions & Answers

file descriptors

i m trying to learn processes in unix and i've been reading this but i don't quite get it. its regarding file descriptors. : each is a part of file pointers, they point to another area. indexes into an Operating system maintained table called "file descriptor table". one table per process. may... (3 Replies)
Discussion started by: a25khan
3 Replies

9. UNIX for Advanced & Expert Users

File Descriptors

Hello all, A few questions on file descriptors ... scenario : Sun Ultra 30 with Sun OS 5.5.1 , E250 with Solaris 2.6 In one of my servers, the file descriptor status from the soft limit and hard limits are 64 and 1024 respectively for root user. Is the soft limit (64) represents the... (3 Replies)
Discussion started by: shibz
3 Replies

10. Programming

File Descriptors

Hi, I have written a daemon process, to perform certain operations in the background. For this I have to close, the open file descriptors, Does anybody know how to find out the number of open file descriptors ? Thanks in Advance, Sheetal (2 Replies)
Discussion started by: s_chordia
2 Replies
Login or Register to Ask a Question