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
dispatch_read(3)					   BSD Library Functions Manual 					  dispatch_read(3)

NAME
dispatch_read, dispatch_write -- asynchronously read from and write to file descriptors SYNOPSIS
#include <dispatch/dispatch.h> void dispatch_read(int fd, size_t length, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error)); void dispatch_write(int fd, dispatch_data_t data, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error))); DESCRIPTION
The dispatch_read() and dispatch_write() functions asynchronously read from and write to POSIX file descriptors. They can be thought of as asynchronous, callback-based versions of the fread() and fwrite() functions provided by the standard C library. They are convenience func- tions based on the dispatch_io_read(3) and dispatch_io_write(3) functions, intended for simple one-shot read or write requests. Multiple request on the same file desciptor are better handled with the full underlying dispatch I/O channel functions. BEHAVIOR
The dispatch_read() function schedules an asynchronous read operation on the file descriptor fd. Once the file descriptor is readable, the system will read as much data as is currently available, up to the specified length, starting at the current file pointer position. The given handler block will be submitted to queue when the operation completes or an error occurs. The block will be passed a dispatch data object with the result of the read operation. If an error occurred while reading from the file descriptor, the error parameter to the block will be set to the appropriate POSIX error code and data will contain any data that could be read successfully. If the file pointer position is at end-of-file, emtpy data and zero error will be passed to the handler block. The dispatch_write() function schedules an asynchronous write operation on the file descriptor fd. The system will attempt to write the entire contents of the provided data object to fd at the current file pointer position. The given handler block will be submitted to queue when the operation completes or an error occurs. If the write operation completed successfully, the error parameter to the block will be set to zero, otherwise it will be set to the appropriate POSIX error code and the data parameter will contain any data that could not be written. CAVEATS
The data object passed to a handler block is released by the system when the block returns. If data is needed outside of the handler block, it must concatenate, copy, or retain it. Once an asynchronous read or write operation has been submitted on a file descriptor fd, the system takes control of that file descriptor until the handler block is executed. During this time the application must not manipulate fd directly, in particular it is only safe to close fd from the handler block (or after it has returned). If multiple asynchronous read or write operations are submitted to the same file descriptor, they will be performed in order, but their han- dlers will only be submitted once all operations have completed and control over the file descriptor has been relinquished. For details on this and on the interaction with dispatch I/O channels created from the same file descriptor, see FILEDESCRIPTOR OWNERSHIP in dispatch_io_create(3). SEE ALSO
dispatch(3), dispatch_data_create(3), dispatch_io_create(3), dispatch_io_read(3), fread(3) Darwin December 1, 2010 Darwin
All times are GMT -4. The time now is 05:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy