Named pipe read/write of a struct

Hi,

Can someone please point me to a place where I can get examples of named pipes being used for sending structures ?
I am having some problems in my application and want to compare the code to a standard / sample code from somewhere
namityadavAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manav_mathurCommented:
namityadav,

would be better if you list your problem and code thats creating it here

Manav
0
stefan73Commented:
Hi namityadav,
You can use the mkfifo() function to create a FIFO special file (note: there is also a "mkfifo" shell command):

int mkfifo(const char *path, mode_t mode);
(This is a special case of the mknod() function, just in case you want to do something fancy)

You can then open the fifo file just as a regular file, passing data between a process writing and a process reading. Beware that the buffer of a fifo is small, so you can get deadlocks if you don't watch out.

Cheers!

Stefan
0
namityadavAuthor Commented:
Okay, I guess I need to explain the problem in detail.

Actually I am reading a structure from a pipe, but sometimes the data coming to my process isn't legit. I want my process to (the moment it finds out that the data is incorrect) clean up the pipe (maybe by reading out the pipe or something).

Something like -

             fn() {
               ....
               ....
               ....
                while(numread) {
                  numread = read(fd, bufMsg, MAX_BUF_SIZE);
                }
                continue;
               ....
               ....
             }

Is there something wrong with this?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

manav_mathurCommented:
namitayadav,

I have never practically implemented a named-pipe scheme, but If Im not wrong, your code will block in the first call to read.
This is because the writer may not have written complete MAX_BUF characters to the pipe. In this case, if O_NONBLOCK is not specified while creation of pipe, your code will block in the read(), waiting for more data to arrive.

As a flipcase, the data may be legit data which the writer writes in a subsequent write().

You should use fs.eof() in your while().

Manav
0
manav_mathurCommented:

And the mode is a simply equivalent to the file permissions you want to set on the named pipe.

Acually, what I believe is that instead of making an ad-hoc solution like reading the whole pipe when illegitimate data is found, you should investigate more as to why illegitimate data is coming(i.e. look more on the writer's side).
Thays bcoz these kinds of problems tend to mutate into somethign else, and then you'll end up coding a whole lot for trapping illegit data whereas the problem was a simple glitch.

Manav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
namityadavAuthor Commented:
Hi Manav,

Thanks for the help. Since there are more than one processes writing to the pipe, and since our QA believes in testing the read end of the pipe by sending crap through the pipe, I won't be able to assume that the data is always legit. So I need a way to clean up the pipe if something like that happens. We are okay with dropping some legit data as part of the cleaning up process too.

So, yeah, I open the pipe in non-blocking mode only. But I don't need to check the equality of numread with 0, 0 happens when the pipe's not open for write .. I needed to check for -1 here. I guess this code should work, what say?

fd = open(pipename, O_RDONLY|O_NONBLOCK);
...
...
while(1) {
numread = read(fd, bufMsg, SOMESIZE);
if (numread == 0 || (numread==-1 && errno==EAGAIN)) {
   sleep(SLEEPTIME); // Just to save lil bit of CPU - sleep for a sec or two
   continue;
}
...
ParseNamedPipeMsg(bufMsg,someStruct); /* the pipe is used to create a struct */
...
         if(someStruct->someVar!=5) { /* assume if someVar ain't 5, the data is corrupt */
            /* Flushin */
                while(numread!=-1) {
                  numread = read(fd, bufMsg, MAX_BUF_SIZE);
                }
                continue;
         }
}
0
stefan73Commented:
namityadav,

You can't write to the same pipe with several processes. Not good.

Each sender needs a separate pipe.

Check if SysV message queues better suit your needs. With those, each queued item is clearly distinguishable - you won't end up with a useless pipe when you get a single crappy message (i.e., truncated).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.