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?
 
manav_mathurConnect With a Mentor Commented:

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
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
 
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
 
stefan73Connect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.