Link to home
Start Free TrialLog in
Avatar of namityadav
namityadav

asked on

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
Avatar of manav_mathur
manav_mathur

namityadav,

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

Manav
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
Avatar of namityadav

ASKER

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?
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
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
         }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial