473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

non-blocking file access possible in c++?

[Topic also posted in alt.comp.lang.l earn.c-c++]

Hello,

I couldn't find a solution to the following problem (tried
google and dejanews), maybe I'm using the wrong keywords?

Is there a way to open a file (a linux fifo pipe actually) in
nonblocking mode in c++? I did something ugly like

--- c/c++ mixture ---
mkfifo( "testpipe", 777);
int fdesc = open( "testpipe", O_RDONLY|O_NONB LOCK);
while( true)
{
bytes_read = read( fdesc, inbuffer, 255);
if( bytes_read > 0)
std::cerr << "Read " << inbuffer << std::endl;
else
{
std::cerr << "Nothing to read here!" << std::endl;
usleep( 250000);
}
}
--- end ---

Now I want the same result in c++, there has to be some flag
or fctrl or something?

Thanx for any help,

Mario
Jul 23 '05 #1
3 12253
On Wed, 13 Apr 2005 11:27:56 +0200, Mario <su******@gmx.d e> wrote:
[Topic also posted in alt.comp.lang.l earn.c-c++]

Hello,

I couldn't find a solution to the following problem (tried
google and dejanews), maybe I'm using the wrong keywords?

Is there a way to open a file (a linux fifo pipe actually) in
nonblocking mode in c++? I did something ugly like

[...]

Using the C I/O functions is an option. If you want the type-safety
of C++ I/O operations or an OO interface, you could layer a class on
top of the C I/O.

A non-C++ standard, non-portable alternative would be to get a C++
library which supports FIFOs or Unix domain sockets (e.g. GNU Common
C++'s UnixSocket). These tend to support non-blocking I/O. That this
is of limited portability shouldn't be any hindrance in your case.

Another option is to use istream::readso me, which is probably
supported by your c++ library. It reads up to the number of
characters remaining in the stream buffer into a character array. If
the stream buffer is empty, no characters are copied. This may not be
quite the same as non-blocking input because of the difference between
an input buffer and an input sequence. If an input buffer is empty
but its associated input sequence currently has available characters,
readsome() will still copy 0 characters. Note that for an ifstream, I
would expect buffered input to have this problem. If it's impossible
to have an empty buffer but available input, readsome should give you
non-blocking input. Try making an ifstream with a small buffer size
(say, 2) for a file larger than the buffer size and then call readsome
for the ifstream. Of course, if readsome works on your system that
doesn't mean this is a complete solution.

You can use 'in.rdbuf()->in_avail()' to check whether istream in's
buffer has any characters left to be read, but you may have the same
issue as with readsome (readsome usually uses in_avail). in_avail()
!= 0 means get() will not block, but in_avail() == 0 only means get()
MAY block. Using in_avail is what Stroustrup suggests to avoid
blocking. For example:
inline bool wouldblock(std: :istream& in)
{return in.rdbuf()->in_avail() == 0;}
A better implementation of wouldblock may depend on the internals of
stream buffers (see local documentation of showmanyc(), which the
local in_avail() may already call).

If you want to make input via '>>' non-blocking, you could extend
i(f)stream. You could also extend the appropriate stream buffer class
(called perhaps "unblockable_st reambuf") and make blocking during
reads dependent on some state of unblockable_str eambuf. Look at the
underflow and uflow methods of streambuf before embarking on this
path. Extending stream buffers is probably a better option than
extending i(f)stream as blocking behavior is more related to a stream
buffer than a stream. It also may be easier to implement and better
behaved in the face of the polymorphic behavior of i(f)stream.

You could also make types which use non-blocking input. e.g.
template <typename _Type>
struct Nonblocked {
_Type& val;
Nonblocked(_Typ e& v) : val(v) {}
/*
Nonblocked::wou ldblock allows for specializations which have
more appropriate definitions of wouldblock (e.g. a Nonblocked
to input pairs as "(first, second)" which would fail if input
stopped at "(first, " and no other characters are currently
available in the input sequence)
*/
inline bool wouldblock(std: :istream& in)
{return ::wouldblock(in );}
};

template <typename _Type>
std::istream& operator>>(std: :istream& in, Nonblocked<_Typ e>& n)
{
//how to signal nothing was read? set failbit for in?
if (n.wouldblock(i n)) in.setstate(ios _base::failbit) ;
else in >> n.val;
return in;
}

...
int i;
fin >> Nonblocked<int> (i);
This option looks a little odd to me. Note I couldn't think of a good
name for the class template "Nonblocked ", perhaps a sign that this
design stands on shaky ground. A better implementation of
"wouldblock " than that given above is also needed.

You could even abuse the tying mechanism to tie an i(f)stream (call it
'in') to an "ostream" which sets/unsets failbit for itself depending
on the value of wouldblock(in), thus enabling/preventing input from
'in'. Due to the polymorphic behavior of ostream::flush, your
class(es) may not be able to take control until the sync() method of
the tied ostream's buffer is called (ostream::sync is non-virtual and
calls the non-virtual streambuf::pubs ync, which calls the virtual
streambuf::sync ), so you may need to extend streambuf for this to
work. If the i(f)stream was tied to some other ostream (call it
'out'), the "ostream" better flush 'out' somewhere along the way. To
switch between blocking and non-blocking, tie/untie the "ostream"
to/from 'in'. All in all, this is an ugly and effortful option, but
it's still an option.

If you decide to try extending any of C++'s I/O classes, consider the
sentry class and/or the ios_base storage/callback mechanism. Combine
this with tie() abuse and you can add a block/nonblock state to 'in'
so you can easily switch between blocking and non-blocking input with
manipulators.

If you haven't seen it yet, check out the C++ annotations for input
streams:
http://www.icce.rug.nl/documents/cpl...lus05.html#l78
C++ annotations, main page:
http://www.icce.rug.nl/documents/cplusplus/
Kanenas

Jul 23 '05 #2
Hy,

Kanenas wrote:
A non-C++ standard, non-portable alternative would be to get a C++
library which supports FIFOs or Unix domain sockets (e.g. GNU Common
C++'s UnixSocket). These tend to support non-blocking I/O. That this
is of limited portability shouldn't be any hindrance in your case. That would be a great solution, since I have to use cc++ anyways!
Could you point out a little hint where to look in the docs? cc++ doesn't
have a non-blocking file::open() call, (or I missed it,) but maybe there
is a way arround?
Another option is to use istream::readso me, which is probably
supported by your c++ library. It reads up to the number of That is actually a great idea, but won't work for me, as I need non-
blocking file-i/o for only one reason:
to read from, and write to a pipe at the same time, from different
programs.

With the small c-prog I posted, that works perfectly, you could
echo "hello world" >> fifopipe
from the shell and the prog would read it from there. But as soon as I
don't open the pipe non-blocking, it won't work anymore...
Maybe I'm doing something wrong?
If you haven't seen it yet, check out the C++ annotations for input
streams:
http://www.icce.rug.nl/documents/cpl...lus05.html#l78
C++ annotations, main page:
http://www.icce.rug.nl/documents/cplusplus/

I'll check these again, thx for the link, haven't seen them yet.

Mario
Jul 23 '05 #3
On Thu, 14 Apr 2005 11:52:55 +0200, Mario <su******@gmx.d e> wrote:
Hy,

Kanenas wrote:
> A non-C++ standard, non-portable alternative would be to get a C++
library which supports FIFOs or Unix domain sockets (e.g. GNU Common
C++'s UnixSocket). These tend to support non-blocking I/O. That this
is of limited portability shouldn't be any hindrance in your case.That would be a great solution, since I have to use cc++ anyways!
Could you point out a little hint where to look in the docs? cc++ doesn't
have a non-blocking file::open() call, (or I missed it,) but maybe there
is a way arround?

By "cc++", do you mean C/C++ (a mixture of C and C++) or is it a typo
for "C++"?

If you want to know more about GNU Common C++, go to:
http://www.gnu.org/software/commoncpp/
As for non-blocking stream I/O, the only support the standard C++
library provides which I'm aware of are the streambuf::in_a vail and
istream::readso me methods mentioned in my previous post.
Another option is to use istream::readso me, which is probably
supported by your c++ library. It reads up to the number of

That is actually a great idea, but won't work for me, as I need non-
blocking file-i/o for only one reason:
to read from, and write to a pipe at the same time, from different
programs.

istream::readso me should work with an ifstream, and an ifstream should
be able to open a FIFO, so istream::readso me should work for you.
You'll still be responsible for race conditions and file locking.

Is a FIFO required, or will other interprocess communication
structures (such as Unix domain sockets) work?
With the small c-prog I posted, that works perfectly, you could
echo "hello world" >> fifopipe
from the shell and the prog would read it from there. But as soon as I
don't open the pipe non-blocking, it won't work anymore...
Maybe I'm doing something wrong?
This suggests your true issue is whatever makes the reader not "work
anymore" rather than opening a non-blocking stream. Non-blocking I/O
may be a solution, but merely an indirect one. What, exactly, stopped
working? What happens if you open the pipe as an ifstream rather than
via open()? When I tested your basic approach (with a few corrections
and additions, keep reading for the important one), I didn't see any
issues; the reader read from the pipe whenever anything was put in.
The only difference was that for blocking I/O, the reader paused the
first time through the loop until something was written to the pipe.
--- c/c++ mixture ---
mkfifo( "testpipe", 777);
You probably want "0777", which is an octal number, rather than "777",
which is base 10, for the mode. Decimal "777" corresponds to octal
"01411", and will give the pipe the permissions "r----x--x" plus the
sticky bit, less the process's umask. This will make
echo "hello world" >> testpipe
fail as it won't have write permission.
int fdesc = open( "testpipe", O_RDONLY|O_NONB LOCK);
Don't forget to test that fdesc > 0 in your production code.
while( true)
{
bytes_read = read( fdesc, inbuffer, 255);
if( bytes_read > 0) In production code, the size of inbuffer better be a parameter rather
than a constant. read() won't terminate strings, so you need to do it
yourself. As you won't want to overwrite the last character if read
fills inbuffer, have read() read 1 less than the size of inbuffer
(call 'read(fdesc, inbuffer, bufsize-1)'). Then terminate the buffer
as below.
{
inbuffer[bytes_read] = 0; std::cerr << "Read " << inbuffer << std::endl; } else
{
std::cerr << "Nothing to read here!" << std::endl;
usleep( 250000);
select() is a much better alternative to polling via sleeping. Note
select() will mix more C into your program and is thus appropriate
only if a C/C++ I/O approach is better than pure C++ I/O. If you want
to know more about select(), check its man page, try a google search
or get a good book on Unix development such as "Advanced Unix
Programming" by Marc J. Rochkind.
}
}

Kanenas
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4422
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
3748
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
25
7627
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4519
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
12043
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class had a static function, and the one class had a non-static function. Both of these functions did the exact same thing. The test function: public void Test(){ decimal y = 2; decimal x = 3;
8
3506
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
14
8446
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
2
6113
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
399
12835
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
13
18948
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in the Post: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e81cd9d9c2200d74/48c0774eeb3bd998?hl=en&lnk=gst&q=initial+value+of+reference+to+non-const#48c0774eeb3bd998 ...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.