Click here to Skip to main content
6,596,602 members and growing! (20,805 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Inter-Process Communication     Intermediate

Using Named Pipes for Tracing

By Holger Kloos

Using Named Pipes for Traces out of Multiple Processes
VC6, Windows, Dev
Posted:14 Jul 2004
Views:42,664
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.99 Rating: 4.48 out of 5
1 vote, 7.7%
1

2
1 vote, 7.7%
3
2 votes, 15.4%
4
9 votes, 69.2%
5

Introduction

Traces are very useful tools for debugging purposes, especially if the program runs as a release version. It is easy to save the traces for a single application. But it needs a little work to receive the traces of multiple processes or even threads running at the same time in one single file or window. The example given here describes an elegant way to solve this problem. It uses "named pipes" for interprocess communication. Each process sends its trace data to one central special listener process. For the processes sending traces it doesn't matter if this listener process does or does not exist.

Background

A Named Pipe is a high level mechanism for the communication between processes even if they are running on different computers linked by a network. It hides all the communication details and offers two sides: One side to put data in and one side to read it out, but it can also be used bidirectional.

For building up a communication line, a process creates a pipe with a unique name. Using this name another process can connect to this pipe. Because a named pipe is a link between only two processes, it needs a set of named pipes to communicate with more then two processes at the same time. Each instance of these named pipe can use the same name.

The reading or writing of data to a pipe can be done with the normal set of Windows commands used to read or write data to a file. Like the normal file operations named pipes can be accessed in a blocking (= synchronous) or nonblocking (= asynchronous) mode. This example uses the synchronous communication mode because it is especially simple to use: The function calls to read or write data don't come back until the communication operation has finished. To allow communication with multiple processes it uses one thread for each pipe. Every time a new process connects to the listener, it creates a new pipe allowing the next process to connect with.

Using the code

For sending a trace, there is just a very easy interface needed: Building up the communication with Connect and sending traces with MakeTrace. The usage is demonstrated in the simple dialog based application TraceSender. You may start multiple instances of this program to generate more traffic.

class CTracer  
{
public:
  CTracer();
  virtual ~CTracer();

  bool Connect();
  bool MakeTrace(const char* pStr,...) const;    // use like printf(...)


private:
  HANDLE m_hPipe;

};

Because of its multithreading capability the mechanism for receiving traces is a little bit more complicate to implement. But its usage is as simple. This demonstrates the simple console application TraceListener:

#include "stdafx.h"

#include "conio.h"


#include "TraceCollector.h"


BOOL OnTrace(const char* pStr, DWORD nLength)
{
  _cputs(pStr);
  _cputs("\r\n");
  return TRUE;
}

int main(int argc, char* argv[])
{
  CTraceCollector tracer(OnTrace);
  tracer.Run();

  _getch();
  return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Holger Kloos


Member

Location: United Kingdom United Kingdom

Other popular Threads, Processes & IPC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalall pipe instance are busy GetLastError() = 231 Pinmemberrahulagarwal336:37 13 Oct '08  
GeneralExactly what I was looking for! Pinmembermi-chi20:44 23 Sep '07  
GeneralPossible handle leak PinmemberJim Xochellis23:58 10 Oct '06  
GeneralGlad you exist! PinmemberphilFox18:09 15 Mar '06  
Generalthanks PinmemberMoak1:41 1 Jul '05  
GeneralTwo Words PinmemberBrad Bruce7:47 15 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jul 2004
Editor: Nishant Sivakumar
Copyright 2004 by Holger Kloos
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project