behaviour of read() and write() after a select()


 
Thread Tools Search this Thread
Top Forums Programming behaviour of read() and write() after a select()
# 8  
Old 10-01-2008
Quote:
Originally Posted by Perderabo
If you select on a terminal and I am typing on it, select returns on the first character. If you then do a blocking read for 10 characters, you will block until I type nine more. I really can't imagine any alternative behavior.
I use read(), not fread(). read() could for example only return 1 byte. As all available docs say, that read() *can* return less bytes then requested. So the return value of the read() call in your case would be 1 (in my optinion). Then a program would decide to either
  1. Wait for the rest, by calling read() again, this time requesting the 9 bytes missing
  2. call select() again, to be notified, when you press your next key
# 9  
Old 10-01-2008
Quote:
Originally Posted by stevexyz
If you're using Linux look at select_tut(2)...

Cheers,Steve
Thank you, didn't know that page. This really helped, since it confirmed my expectations Smilie and prove all other posters here wrong (no offence)

EDIT: well, not about the thing with the first write after opening a file. But that is not that important anyway. And nobody disputed that

Last edited by calv; 10-01-2008 at 11:15 AM.. Reason: Added some stuff
# 10  
Old 10-09-2008
After some further reading, I found out that all this is not true for regular files. select() behaves as if a file is ready for reading and writing at any time:

"File descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions."

select

This makes Unix more inconsistent than I thought. Most documents about select don't mention that. I guess it's a "they won't notice" thing. Also, O_NONBLOCK does not work with regular files. I didn't want to do multithreading or multiprocessing to conserve resources. For the same reason I can't use aio_read(), because on Linux it is implemented with threads. The kernel does not support async I/O. This is a suboptimal solution in my opinion, because only the kernel knows, which operations would make sense to do in parallel (because the files might be on different disks)... I'm gonna write my own kernel Smilie

But I think I can live with all that. I just need to look out, that my file I/O operations are small, so they don't block for long.
# 11  
Old 10-09-2008
Good luck.

You stated that you don't want to set O_NONBLOCK
which is standard to *nix for alleviating blocking behaviors
and is the basis for fast event interfaces like kqueue and
epoll for sockets, pipes, etc...
You don't want to use threads or processes to allow
for blocking on reads and writes to regular files due to
resource conservation.
Finally, you state that you believe that as long as i/o operations are
'small' you should be okay (as if this somehow alleviates the wait
behavior incurred should an operation block..).
It just seems you are fighting a bad fight against programming with unix.

Here's a little advice:
Optimization (computer science - Wikipedia, the free encyclopedia)
# 12  
Old 10-09-2008
Yes, I realized that given the alternatives I have to "die one death". I think I will make a single threaded app, that multiplexes sockets using select(). File operations will then still block, but if I keep them small, they should not hurt performance much. After all, file I/O should be much faster then network I/O. At least in max. 20 ms every small file read should be finished.

Its just that I realized now, that neither select() nor O_NONBLOCK ("standard to *nix for alleviating blocking behaviors") work for ordinary files (I mean, who would have thought?)

By the way, as long as you don't use ordinary files, O_NONBLOCK is not needed. select() does it's job very well even without O_NONBLOCK. On the other hand, if you use select(), O_NONBLOCK is not really needed anymore. Read man select_tut(2) on this.

So the only alternative would be threading. But this solves only part of the problem, because file operations would be asynchrouos with the select part of my app, but not with each other. Who knows, one file could still be in cache by the time it's needed...

Quote:
It just seems you are fighting a bad fight against programming with unix.
That's why I'm here, right? Smilie

btw, your little advice is neither little nor an advice Smilie

You mean I'm trying to optimize too soon? I don't think so, because in this case I try to figure out a good overall structure for the app. This is better done before I write the bulk of code.
# 13  
Old 10-09-2008
Hello,

Quote:
Its just that I realized now, that neither select() nor O_NONBLOCK ("standard to *nix for alleviating blocking behaviors") work for ordinary files (I mean, who would have thought?)
This is one of the well known *nix gotchas.
Most unix systems programmers have gotten used to it.
Not that it's ideal.

Quote:
By the way, as long as you don't use ordinary files, O_NONBLOCK is not needed. select() does it's job very well even without O_NONBLOCK. On the other hand, if you use select(), O_NONBLOCK is not really needed anymore. Read man select_tut(2) on this.
"
Read or write not blocking on a resource after select returns is not entirely reliable at all times. The select_tut example is it's own best argument against using the non-blocking model imho. Also see: http://cr.yp.to/unix/nonblock.html

Quote:
You mean I'm trying to optimize too soon? I don't think so, because in this case I try to figure out a good overall structure for the app. This is better done before I write the bulk of code
You are ,imo, optimizing too soon and encountering problems that research may have
ameliorated. Please see if you haven't already: The C10K problem
# 14  
Old 10-10-2008
Re:High Level Programming

Hi, this is jack Steven. I have an Interest to answers the questions on JAVA. If any one want to or interest in asking questions, you are welcome.
--------------------------------
Jack Steven
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. If there are 3folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Programming

FIFO write and read

Can someone help me to write this program in C in QNX? Using the FIFO queues write a simple communication system consisting of programs write and read. The program write the parameters given strings enclosed in single quotes. These strings are written to the FIFO file. Reads the program read... (1 Reply)
Discussion started by: ebasse2
1 Replies

3. Shell Programming and Scripting

Read and write in the file

Hello Guys, How all are doing? I have an issue in Unix and want help from all of you I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1' and If at the end of line '1' is written then it... (10 Replies)
Discussion started by: adisky123
10 Replies

4. IP Networking

write() / read() syntax

hi am newbie to unix and socket programing I am trying to figuring out syntax for read and write to send data from server to client and client can read it I have to send two integers write(newsockfd,buffer,"%d %d",x,y,0) writing from client where x and y are two integers.. ... (7 Replies)
Discussion started by: karthik1238
7 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

7. UNIX Desktop Questions & Answers

Wall, Write, select users, pipe a text file, HELP Before I'm Bald!

OK... I'm fairly new to unix having the admin handed to me on a platter w/almost no training. However, being a programmer, I do pick up things fairly easily, but this one is getting the best of me. I have a unix server that runs multiple versions of the same ERP system, hand crafted for our... (1 Reply)
Discussion started by: chimodel
1 Replies

8. Shell Programming and Scripting

read and write from a file

I have tried to show the file name whose size is greater than 200 byte in current directory. Please help me. ls -l | tr -s " " " " | cut -f 5,9 -d " " >out.txt #set -a x `cat out.txt` i=0 `cat out.txt` | while do read x echo $x #re=200 j=0 if }" < "200" ] then echo $j j=`expr $j... (2 Replies)
Discussion started by: rinku
2 Replies

9. Programming

popening for read and write

How can 'popen()' be used for reading and writing to opening pipe? If i try 'popen("prog", "rw")' and then put and get chars - it does not work. What is wrong? (1 Reply)
Discussion started by: szzz
1 Replies

10. Programming

select() and read()

Hi, Could you please tell me how can I know which position a given read descriptor ocuppies in the descriptors table. I know that, for instance, 'stdin' ocuppies position ... what about the others descriptors that I create usings pipes or sockets?? Thanks! P.S.: Sorry for my bad English (2 Replies)
Discussion started by: jnuno
2 Replies
Login or Register to Ask a Question