Sponsored Content
Top Forums Programming behaviour of read() and write() after a select() Post 302241849 by calv on Tuesday 30th of September 2008 12:41:33 PM
Old 09-30-2008
Question behaviour of read() and write() after a select()

Hello, I have problems finding clear information about the use of select(). I want to write an application, and for that I need to be sure about the functions behaviour. So to ensure, that I understood it all correctly I make some statements or questions. So a qualified answer could just be: yes, you are right in all points.

The manpage says:

"Three independent sets of descriptors are watched. Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a file descriptor is also ready on end-of-file), those in writefds will be watched to see if a write will not block, and those in exceptfds will be watched for exceptions. On exit, the sets are modified in place to indicate which descriptors actually changed status."

So if I read() from a file after select() says it would not block, it will under no circumstances block, right? When I try to read 1 MB, which might not be available without blocking, read() would then just return the data that is available (it has the liberty to read less bytes then I told it to), right? A second read() after that, on the other hand could block, because read has not the liberty to return 0 bytes, because that would mean EOF.

The same thing with write(): When select() says a file is writable, a write() to that file would under no circumstances block. But write might have a size limit, that is unknown to me. So when I write 1MB, it uses the data to fill some internal kernel buffer, and might return without actually writing all the data, but only part of it. It would take only that much data, that is possible to take without blocking. So the first write does under no circumstances block, but the second write could block.

Of course a read() or write() could block, if someone else (another thread/process) does the first read/write after the select, but thats not what I'm interested in, because I don't need to share files with someone else.

Does all this still hold completely true with sockets?

Does all this still hold completely true without using non blocking I/O?

Is it always possible to safely assume, that the first write() after opening a socket/file will not block even without checking this with select()?

The reason why I am not 100% sure about all that is that I did some googling, and some people in some forums says that one can never be 100% sure, if a read/write won't block, even with select, when not using O_NONBLOCK. Some say, that a write does block, if the size is bigger then some magic value. Also most texts about select don't really cover that write case.

Of course I could just use nonblocking I/O to be sure, but I want to make my app as simple as possible, and I don't want to have a "double net" if it is not required.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
EVIO(3) 								 1								   EVIO(3)

The EvIo class

INTRODUCTION
EvIo watchers check whether a file descriptor(or socket, or a stream castable to numeric file descriptor) is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data. This behaviour is called level-triggering because events are kept receiving as long as the condition persists. To stop receiving events just stop the watcher. The number of read and/or write event watchers per $fd is unlimited. Setting all file descriptors to non-blocking mode is also usually a good idea(but not required). Another thing to watch out for is that it is quite easy to receive false readiness notifications, i.e. the callback might be called with Ev::READ but a subsequent read() will actually block because there is no data. It is very easy to get into this situation. Thus it is best to always use non-blocking I/O: An extra read() returning EAGAIN (or similar) is far preferable to a program hanging until some data arrives. If for some reason it is impossible to run the $fd in non-blocking mode, then separately re-test whether a file descriptor is really ready. Some people additionally use SIGALRM and an interval timer, just to be sure thry won't block infinitely. Always consider using non-blocking mode. CLASS SYNOPSIS
EvIo EvIoextends EvWatcher Properties o public$fd o public$events Inherited properties o public$is_active o public$data o public$is_pending o public$priority Methods o public EvIo::__construct (mixed $fd, int $events, callable $callback, [mixed $data], [int $priority]) o finalpublicstatic EvIo EvIo::createStopped NULL (mixed $fd, int $events, callable $callback, [mixed $data], [int $priority]) o public void EvIo::set (mixed $fd, int $events) Inherited methods o public int EvWatcher::clear (void ) o abstractpublic EvWatcher::__construct (void ) o public void EvWatcher::feed (int $revents) o public EvLoop EvWatcher::getLoop (void ) o public void EvWatcher::invoke (int $revents) o public bool EvWatcher::keepalive ([bool $value]) o public void EvWatcher::setCallback (callable $callback) o public void EvWatcher::start (void ) o public void EvWatcher::stop (void ) PROPERTIES
o $fd - o $events - PHP Documentation Group EVIO(3)
All times are GMT -4. The time now is 10:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy