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
VOL(1)							      General Commands Manual							    VOL(1)

NAME
vol - split input on or combine output from several volumes SYNOPSIS
vol [-rw1] [-b blocksize] [-m multiple] [size] device DESCRIPTION
Vol either reads a large input stream from standard input and distributes it over several volumes or combines volumes and sends them to standard output. The size of the volumes is determined automatically if the device supports this, but may be specified before the argument naming the device if automated detection is not possible or if only part of the physical volume is used. The direction of the data is automatically determined by checking whether the input or output of vol is a file or pipe. Use the -r or -w flag if you want to specify the direction explicitly, in shell scripts for instance. Vol waits for each new volume to be inserted, typing return makes it continue. If no size is explicitely given then the size of the device is determined each time before it is read or written, so it is possible to mix floppies of different sizes. If the size cannot be deter- mined (probably a tape) then the device is assumed to be infinitely big. Vol can be used both for block or character devices. It will buffer the data and use a block size appropriate for fixed or variable block sized tapes. Vol reads or writes 8192 bytes to block devices, usually floppies. Character devices are read or written using a multiple of 512 bytes. This multiple has an upper limit of 32767 bytes (16-bit machine), 64 kb (32-bit), or even 1 Mb (32-bit VM). The last partial write to a character device is padded with zeros to the block size. If a character device is a tape device that responds to the mtio(4) status call then the reported tape block size will be used as the smallest unit. If the tape is a variable block length device then it is read or written like a block device, 8192 bytes at the time, with a minimum unit of one byte. All sizes may be suffixed by the letters M, k, b or w to multiply the number by mega, kilo, block (512), or word (2). The volume size by default in kilobytes if there is no suffix. OPTIONS
-rw Explicitly specify reading or writing. Almost mandatory in scripts. -1 Just one volume, start immediately. -b blocksize Specify the device block size. -m multiple Specify the maximum read or write size of multiple blocks. The -b and -m options allow one to modify the block size assumptions that are made above. These assumptions are -b 1 -m 8192 for block devices or variable length tapes, and -b 512 -m 65536 for charac- ter devices (32 bit machine.) These options will not override the tape block size found out with an mtio(4) call. The multiple may be larger then the default if vol can allocate the memory required. EXAMPLES
To back up a tree to floppies as a compressed tarfile: tar cf - . | compress | vol /dev/fd0 To restore a tree from 720 kb images from possibly bigger floppies: vol 720 /dev/fd0 | uncompress | tar xfp - Read or write a device with 1024 byte blocks: vol -b 1k /dev/rsd15 Read or write a variable block length tape using blocking factor 20 as used by default by many tar(1) commands: vol -m 20b /dev/rst5 Note that -m was used in the last example. It sets the size to use to read or write, -b sets the basic block size that may be written in multiples. SEE ALSO
dd(1), tar(1), mt(1), mtio(4). VOL(1)
All times are GMT -4. The time now is 05:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy