For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > Example of select() writes?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Example of select() writes?
bwaichu@yahoo.com

2006-09-11, 4:01 am

I spent part of the wend playing around with select(). I am
comfortable
using it to "sleep" while descriptors wait to be read, but I am not
quite sure
how writes work with select().

If I want to read and write to a file with the same file descriptor,
how do I handle
this with select()? And how do I avoid a race condition if I am using
select()?
Can a regular file be ready for reading and writing at the same time?
Can I be
writing to a file and be ready for reading at the same time that I am
writing?

Thanks!

Nils O. Selåsdal

2006-09-11, 4:01 am

bwaichu@yahoo.com wrote:
> I spent part of the wend playing around with select(). I am
> comfortable
> using it to "sleep" while descriptors wait to be read, but I am not
> quite sure
> how writes work with select().

Set the fd to non-blocking.
When you want to write something, you place the fd in the write set.
When select returns ready for writing, you write to it, noting how
much the write call wrote, and you start from that offset the next
time select indicates you can write.

When you're done writing all you need to, you remove the fd from the
write set.

> If I want to read and write to a file with the same file descriptor,
> how do I handle
> this with select()? And how do I avoid a race condition if I am using
> select()?

You put the same fd in the read and write set.
Which race condition did you have in mind ?

> Can a regular file be ready for reading and writing at the same time?

select on a descriptor connected to a regular file is normally futile,
as it will always return ready immediatly.

> Can I be
> writing to a file and be ready for reading at the same time that I am
> writing?

Sure.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com