[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Named pipe read/write of a struct

Posted on 2004-10-31
9
Medium Priority
?
771 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:namityadav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
9 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12463482
namityadav,

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

Manav
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12471203
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
 

Author Comment

by:namityadav
ID: 12526664
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:manav_mathur
ID: 12531642
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
 
LVL 16

Accepted Solution

by:
manav_mathur earned 200 total points
ID: 12531702

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
 

Author Comment

by:namityadav
ID: 12535653
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
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 200 total points
ID: 12535987
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question