When is it OK to change O_NONBLOCK mode of a socket? - Unix
This is a discussion on When is it OK to change O_NONBLOCK mode of a socket? - Unix ; First of all, what I want to do:
1. Create socket (will be either SOCK_STREAM/SOCK_DGRAM + PF_INET).
2. Set as nonblocking.
3. Connect with timeout (connect + select).
4. Set as blocking.
5. Other I/O stuff.
In my application, a ...
-
When is it OK to change O_NONBLOCK mode of a socket?
First of all, what I want to do:
1. Create socket (will be either SOCK_STREAM/SOCK_DGRAM + PF_INET).
2. Set as nonblocking.
3. Connect with timeout (connect + select).
4. Set as blocking.
5. Other I/O stuff.
In my application, a blocking socket is more convenient, but I would
like to set it to non-blocking mode for connect() so I have control
over the timeout. Is this always OK?
Also, in general, are there any rules about when it's OK to change the
blocking/non-blocking mode of a socket? Can I just do it arbitrarily?
Thanks,
Jason
-
Re: When is it OK to change O_NONBLOCK mode of a socket?
In article
<2419d918-a467-4c06-ac4a-fcbfd1adf358@m45g2000hsb.googlegroups.com>,
"jason.cipriani@gmail.com" wrote:
> First of all, what I want to do:
>
> 1. Create socket (will be either SOCK_STREAM/SOCK_DGRAM + PF_INET).
> 2. Set as nonblocking.
> 3. Connect with timeout (connect + select).
> 4. Set as blocking.
> 5. Other I/O stuff.
>
> In my application, a blocking socket is more convenient, but I would
> like to set it to non-blocking mode for connect() so I have control
> over the timeout. Is this always OK?
Yes.
>
> Also, in general, are there any rules about when it's OK to change the
> blocking/non-blocking mode of a socket? Can I just do it arbitrarily?
Yes. It just affects future calls to operations that can block.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
-
Re: When is it OK to change O_NONBLOCK mode of a socket?
On May 11, 11:57 pm, Barry Margolin wrote:
> "jason.cipri...@gmail.com" wrote:
> > In my application, a blocking socket is more convenient, but I would
> > like to set it to non-blocking mode for connect() so I have control
> > over the timeout. Is this always OK?
>
> Yes.
>
> > Also, in general, are there any rules about when it's OK to change the
> > blocking/non-blocking mode of a socket? Can I just do it arbitrarily?
>
> Yes. It just affects future calls to operations that can block.
Great, thanks!
Jason
-
Re: When is it OK to change O_NONBLOCK mode of a socket?
Barry Margolin wrote:
> In article
> <2419d918-a467-4c06-ac4a-fcbfd1adf358@m45g2000hsb.googlegroups.com>,
> "jason.cipriani@gmail.com" wrote:
>
>> First of all, what I want to do:
>>
>> 1. Create socket (will be either SOCK_STREAM/SOCK_DGRAM + PF_INET).
>> 2. Set as nonblocking.
>> 3. Connect with timeout (connect + select).
>> 4. Set as blocking.
>> 5. Other I/O stuff.
>>
>> In my application, a blocking socket is more convenient, but I would
>> like to set it to non-blocking mode for connect() so I have control
>> over the timeout. Is this always OK?
>
> Yes.
>
>> Also, in general, are there any rules about when it's OK to change the
>> blocking/non-blocking mode of a socket? Can I just do it arbitrarily?
>
> Yes. It just affects future calls to operations that can block.
>
I thought that applies within a single thread, changing the mode of a
socket from another thread during an operation that can block is undefined.
--
Ian Collins.