GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  SHAREDSTREAM (3)

NAME

FBB::SharedStream - I/O operations on shared memory

CONTENTS

SYNOPSIS

#include <bobcat/sharedstream>
Linking option: -lpthread -lbobcat

DESCRIPTION

This class combines the features of the std::istream and std::ostream classes, operating on shared memory. As with std::fstream objects, a seekp or seekg member call is required to switch from writing to reading or v.v.

As with std::fstream objects, FBB::SharedStream objects do not keep separate offsets for reading and writeing: the seek-members always refer to the (single) offset maintained by the FBB::SharedMemory object to which the SharedStream object interfaces.

So, although tellg and tellp return identical values, tellg should not be called after writing to a SharedStream object, and tellp should not be called after reading from a SharedStream object, as calling members related to reading (tellg) after writing and v.v. put the stream in its fail state.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

FBB::SharedStreambuf (private inheritance),
std::istream,
std::ostream,
FBB::SharedEnum__ (cf. sharedmemory(3bobcat) for a description of this base class).

SIZEUNIT ENUMERATION

The enum SizeUnit defines the following symbolic constants:
o kB, representing 1024 (2**10) bytes of memory;
o MB, representing 1048576 (2**20 bytes of memory;
o GB, representing 1073741824 (2**30) bytes of memory )

CONSTRUCTORS

o SharedStream():
The default constructor defines a stub a SharedStream object that cannot immediately be used to access shared memory. To use it, its member open must first be called.
o SharedStream(size_t maxSize, SizeUnit sizeUnit, std::ios::openmode openMode = std::ios::in | std::ios::out, size_t access = 0600):
This constructor creates a stream inheriting the facilities of an std::istream and std::ostream that interfaces to a shared memory segment having a capacity of at least maxSize * sizeUnit bytes.
By default, the shared memory segment is opened for reading and writing. Different from the open modes used for file streams, creating a shared memory stream with open modes ios::in | ios::out is OK. In this case the shared memory segment is created and once information has been written to the shared memory it can also be read again.
The shared memory\(cqs access rights are defined by the access parameter, interpreted as an octal value, using the well-known (chmod(1)) way to define the access rights for owner, group and others.
If construction fails, an FBB::Exception is thrown.
o SharedStream(int id, std::ios::openmode openMode = std::ios::in | std::ios::out):
This constructor creates a stream inheriting the facilities of an std::istream and std::ostream that connects to a shared memory segment having ID id.
Specifying the ios::trunc flag immediately clears the contents of the shared memory.
An FBB::Exception is thrown if construction fails (e.g., no shared memory segment having ID id exists), Copy and move constructors are not available.

OVERLOADED OPERATORS

No overloaded move and copy assignment operators are available.

MEMBER FUNCTIONS

All members of std::istream and std::ostream and the enum values kB, MB, and GB, defined by FBB::SharedEnum__ are available.
o FBB::SharedCondition attachSharedCondition(std::ios::off_type offset, std::ios::seekdir origin):
Returns an FBB::SharedCondition(3) object, interfacing to a shared condition variable located at offset offset (relative to origin) in the SharedMemory object to which the SharedStream object interfaces. This member does not alter the value returned by the stream\(cqs tellg and tellp members.
An FBB::Exception is thrown if the FBB::SharedCondition object could not be constructed.
o FBB::SharedCondition createSharedCondition():
Returns an FBB::SharedCondition(3) object, interfacing to a newly created shared condition variable which is created at the current offset of the SharedMemory object to which the SharedStream object interfaces (or at the first offset of the next physical shared memory data block, cf. sharedcondition(3bobcat)). Creating a SharedCondition object does not alter the value returned by the stream\(cqs tellg and tellp members.
An FBB::Exception is thrown if the FBB::SharedCondition object could not be constructed.
o int id() const:
The ID of the shared memory segment is returned.
o void kill():
Without locking the shared memory first, all shared memory is returned to the operating system. The FBB::SharedStream object is unusable after returning from kill. Other processes that were using the shared memory can continue to do so.
o void memInfo(std::ostream &out, char const *end = \(dq\n\(dq):
Information about the SharedMemory object is inserted into the provide ostream object. The IDs of the shared segments, their sizes, the maximum number of shared memory segments, the number of bytes that can be read from the shared memory, and its actual storage capacity, etc., are displayed. Following the information about the shared memory, end is inserted into out.
o void open(size_t maxSize, SizeUnit sizeUnit, std::ios::openmode openMode = std::ios::in | std::ios::out, size_t access = 0600):
This member creates a shared memory segment having a capacity of at least maxSize * sizeUnit bytes, and connects the shared memory segment to the FBB::SharedStream. A matching close member does not exist and is not required.
By default, the shared memory segment is opened for reading and writing. Different from the open modes used for file streams, creating a shared memory stream with open modes ios::in | ios::out is OK. In this case the shared memory segment is created and once information has been written to the shared memory it can also be read again.
The shared memory\(cqs access rights are defined by the access parameter, interpreted as an octal value, using the well-known (chmod(1)) way to define the access rights for owner, group and others.
If opening fails, an FBB::Exception is thrown.
o void open(int id, std::ios::openmode openMode = std::ios::in | std::ios::out):
This member connects the FBB::SharedStream object to a shared memory segment having ID id. A matching close member does not exist and is not required. If opening succeeds the shared memory is ready for use.
Specifying the ios::trunc flag immediately clears the contents of the shared memory.
An FBB::Exception is thrown if opening fails (e.g., no shared memory segment having ID id exists).
o void remove():
The shared memory is first locked. Next, all shared memory is returned to the operating system. The FBB::SharedStream object is unusable after returning from remove. Other processes that were using the shared memory can continue to do so.
o bool truncate(std::streamsize offset):
If offset is not exceeding the value returned by seekg(0, std::ios::end), then this latter value is changed to offset and true is returned. Otherwise false is returned, and the value returned by seekg is not altered. If the value returned by tellg exceeded offset, tellg\(cqs return value it is reduced to offset as well. Subsequent read operations on the shared memory can only succeed as long as tellg\(cqs return value hasn\(cqt reached the value offset.

EXAMPLE

#include <iostream>
#include <string>
#include <ostream>
#include <istream>

#include <bobcat/exception> #include <bobcat/sharedstream> #include <bobcat/sharedcondition>

using namespace std; using namespace FBB;

int main() { SharedStream sharedStream;

int id = -1;

while (true) { cout << \(dq\n\(dq \(dq K kill (no lock) existing shared segment\n\(dq \(dq S show stats of current shared segment\n\(dq \(dq L <id> Load segment <id>\n\(dq \(dq C Install a SharedCondition at offset 0\n\(dq \(dq c create new shared memory (sets id)\n\(dq // \(dq l lock segment id until key pressed\n\(dq // \(dq p <x> c put char c at offset x\n\(dq \(dq q quit\n\(dq // \(dq r <x> <n> read n chars from offset x\n\(dq // \(dq w <x> args write all args at offset x\n\(dq \(dq i insert lines (until empty) at the current \(dq \(dqoffset\n\(dq

\(dq x extract lines (until EOF) from the current \(dq \(dqoffset\n\(dq \(dq X extract the next line from the current \(dq \(dqoffset\n\(dq \(dq when nodified via \(cqN\(cq\n\(dq \(dq N notify a waiting X\n\(dq \(dq s <x> seek (abs) offset x\n\(dq \(dq? \(dq;

char ch; cin >> ch;

ios::off_type offset;

cout << \(dqRequested: \(dq << ch << \(cq\n\(cq;

sharedStream.clear();

switch (ch) { case \(cqc\(cq: { sharedStream.open(1, SharedStream::kB);

id = sharedStream.id(); cout << \(dqid = \(dq << id << \(cq\n\(cq; sharedStream.memInfo(cout); cout << \(cq\n\(cq; } break; case \(cqC\(cq: { sharedStream.seekp(0); SharedCondition cond(sharedStream.createSharedCondition()); sharedStream.seekg(cond.width()); break; }

case \(cqK\(cq: // delete segment { if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; }

cout << \(dqRemoving segment id = \(dq << id << \(cq\n\(cq; if (ch == \(cqR\(cq) sharedStream.remove(); else sharedStream.kill();

id = -1; } break;

case \(cqL\(cq: cin >> id; cout << \(dqLoading segment \(dq << id << \(cq\n\(cq; sharedStream.open(id); sharedStream.memInfo(cout); cout << \(cq\n\(cq; break;

case \(cqS\(cq: if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; } sharedStream.memInfo(cout); cout << \(cq\n\(cq; break; case \(cqs\(cq: { size_t offset; if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; } cin >> offset; sharedStream.seekg(offset); sharedStream.seekp(offset); cout << \(dqtellg: \(dq << sharedStream.tellg() << \(dq, \(dq \(dqtellp: \(dq << sharedStream.tellp() << \(cq\n\(cq; } break; case \(cqi\(cq: { string line; getline(cin, line); sharedStream.seekp(0, ios::cur); while (true) { cout << \(dq? \(dq; if (not getline(cin, line) || line.empty()) break; sharedStream << line << endl; cout << \(dq tellp: \(dq << sharedStream.tellp() << \(cq\n\(cq; } cout << // \(dq tellg: \(dq << sharedStream.tellg() << \(dq, \(dq \(dq tellp: \(dq << sharedStream.tellp() << \(cq\n\(cq; } break; case \(cqx\(cq: { string line; sharedStream.seekg(0, ios::cur);

while (true) { cout << \(dq: \(dq; if (not getline(sharedStream, line)) break; cout << line << \(dq\n\(dq \(dq tellg: \(dq << sharedStream.tellg() << \(dq, \(dq \(dq tellp: \(dq << sharedStream.tellp() << \(cq\n\(cq; } } break; case \(cqX\(cq: { SharedCondition cond(sharedStream.attachSharedCondition(0));

string line; sharedStream.seekg(cond.width());

cond.lock();

while (true) { cond.wait();

cout << \(dq: \(dq; if (not getline(sharedStream, line)) break;

cout << line << \(dq\n\(dq \(dq tellg: \(dq << sharedStream.tellg() << \(dq, \(dq \(dq tellp: \(dq << sharedStream.tellp() << \(cq\n\(cq; } cout << \(dqAll done\n\(dq; cond.unlock(); } break;

case \(cqN\(cq: { string line; getline(cin, line);

SharedCondition cond(sharedStream.attachSharedCondition(0)); while (true) { cout << \(dq\(cqenter\(cq or \(cqq\(cq? \(dq; getline(cin, line);

if (line == \(dqq\(dq) break;

cond.lock(); cond.notify(); cond.unlock(); } } break; case \(cqp\(cq: // put a char behind the last written { if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; }

cin >> offset >> ch; if (!cin) throw Exception() << \(dqcmd specification error\(dq; sharedStream.seekp(offset); cout << \(dqSegment id = \(dq << id << \(dq at write offset \(dq << sharedStream.tellp() << \(cq\n\(cq; sharedStream.put(ch); } break; case \(cqr\(cq: // put a char behind the last written { if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; }

int n;

cin >> offset >> n; if (!cin) throw Exception() << \(dqcmd specification error\(dq;

char buf[n];

sharedStream.seekg(offset); cout << \(dqSegment id = \(dq << id << \(dq at offset \(dq << sharedStream.tellg() << \(dq, to read \(dq << n << \(dq bytes\n\(dq; n = sharedStream.read(buf, n).gcount(); if (n < 0) cout << \(dqNo data at \(dq << offset << \(cq\n\(cq; else { cout << \(dqRetrieved \(dq << n << \(dq bytes, containing ‘\(dq; cout.write(buf, n); cout << \(dq\(cq\n\(dq; for (auto ch: buf) cout << static_cast<int>(ch) << \(cq \(cq; cout << \(cq\n\(cq; } } break; case \(cqw\(cq: // write chars at offset { if (id == -1) { cout << \(dqNo segment loaded\n\(dq; continue; }

string line;

cin >> offset; getline(cin, line); if (!cin) throw Exception() << \(dqcmd specification error\(dq; streampos pos = sharedStream.seekp(offset).tellp();

cout << \(dqSegment id = \(dq << id << \(dq at offset \(dq << pos << \(dq, to write \(dq << line.length() << \(dq bytes\n\(dq; sharedStream.write(line.data(), line.length());

if (!sharedStream) cout << \(dqNo room left to write any bytes\n\(dq; else cout << \(dqWrote \(dq << (sharedStream.tellp() - pos) << \(dq bytes\n\(dq; } break; case \(cqq\(cq: return 0;

default: cout << \(dqrequest not implemented: \(dq << ch << \(cq\n\(cq; break; } } }

FILES

bobcat/sharedstream - defines the class interface

SEE ALSO

bobcat(7), chmod(1), isharedstream(3bobcat), osharedstream(3bobcat), sharedblock(3bobcat), sharedcondition(3bobcat), sharedmemory(3bobcat) sharedmutex(3bobcat), sharedpos(3bobcat), sharedsegment(3bobcat), sharedstreambuf(3bobcat)

BUGS

Note that by default exceptions thrown from within a std::stream object are caught by the stream object, setting its ios::failbit flag. To allow exceptions to leave a stream object, its exceptions member can be called, e.g., using:

myStream.exceptions(ios::failbit | ios::badbit | ios::eofbit);

DISTRIBUTION FILES

o bobcat_3.25.01-x.dsc: detached signature;
o bobcat_3.25.01-x.tar.gz: source archive;
o bobcat_3.25.01-x_i386.changes: change log;
o libbobcat1_3.25.01-x_*.deb: debian package holding the libraries;
o libbobcat1-dev_3.25.01-x_*.deb: debian package holding the libraries, headers and manual pages;
o http://sourceforge.net/projects/bobcat: public archive location;

BOBCAT

Bobcat is an acronym of ‘Brokken\(cqs Own Base Classes And Templates\(cq.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).

Search for    or go to Top of page |  Section 3 |  Main Index


libbobcat-dev_3&.25&.01-x&.tar&.gz FBB::SHAREDSTREAM (3bobcat) 2005-2015

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.