Wednesday, March 16, 2011

.NET 4 has added support for programming with Memory-Mapped Files (MMF)

First of all, what is a memory-mapped file or MMF? MMF is a kernel object that maps a disk file to a region of memory address space as the committed physical storage. In plain English, MMF allows you to reserve a range of addresses and use a disk file as the physical storage for the reserved addresses. When a MMF is created, you access the disk file as if you were accessing memory. MMF makes accessing files extremely easy. For example, Windows loads EXEs or DLLs using MMF. As a matter of fact, MMF is a corner stone for almost all modern forms of inter-process communication (or IPC) with Windows programming. I felt inconvenient when early .NET Framework releases did not support MMF. The good news is that starting version 4, .NET Framework has built-in support for MMF!

Imagine you have got some modules or applications written in native C/C++ and you want to use them. The old modules use MMF to exchange data. Without MMF support in earlier .NET, you can do two things among others:

  • Platform Invoke Win32 MMF support. You need to handle lots of marshalling between managed and unmanaged worlds.

  • You can always use Windows debug symbol support, which allows to access variables as symbols. However, this mechanism is rather involving and beyond beginners' comfort level. You have to fiddle with marshalling as well.

With .NET Framework 4, you just write an MMF-enabled application to exchange data with old C/C++ applications directly. For programming details, read my article "Programming Memory-Mapped Files with the .NET Framework" on "". This article will demonstrate how to write MMF-enabled .NET applications. I started from basic examples and then moved on to more advanced use of MMF in the shared memory design.


Sunday, February 03, 2008

Build an Interface using Fixed-Point Arithmetic: I. Introduction

We know and use floating-point implementation from the first day of computing. However, not all of us have ever implemented a fixed-point algorithsm, simply because we didn't need to. Recently, I have got such a chance to learn and use fixed-point stuff seriously.

Wikipedia defines fixed-point arithmetic as: "a fixed-point number representation is a real data type for a number that has a fixed number of digits after (and sometimes also before) the radix point (e.g., after the decimal point '.' in English decimal notation)."

Modern computing normally uses more complicated (and more computationally demanding) floating point number representations. However, fixed-point arithmatic is useful in building generic, platform-independent interfaces. So what is good about a fixed-point algorithm?

1) Platform (hardware and OS) independent
2) Network neutral

Note that because fixed point operations can produce results that are larger than the integer can represent, there is possibility for information loss. For example, the result of fixed point multiplication could double the number of bits in the two operands. In order to fit the result into the same number of bits as the operands, the answer must and will be truncated.

In computing, it's more convenient to use binary numbers than decimal numbers. The fixed-point integers are often represented as binary number. Thus, the "point" in the fixed-point arithmetic here means the binary point, not the decimal point.

In next post, I'll explain some fixed-point fundamentals by a real life, simple fixed-point algorithm.

Tuesday, September 19, 2006

New/Delete Is Just a C++ Way to Manage the Heap...

Recently, a blogger asked this question: What do operators new and delete do in C++?

New/delete is just a C++ way to manage the heap. You probably remember that C run-time calls malloc/free to allocate/release memory blocks on the heap. That's C run-time way to manage the heap. Furthermore, VC++ Debug Configuration uses DEBUG_NEW to manages the heap. DEBUG_NEW is the same as new, but it adds some debug information (such as line number) to it. Following the same theme, you could override C++ new/delete with your own approach to manage the heap.

Note that internally, new/delete still calls C run-time malloc/free to perform certain tasks. Regardless, it is important to ensure that your program manages the heap consistently. For instance, if you call new to allocate a block, call delete to release it. Otherwise, the memory may get messed up.

Wednesday, June 28, 2006

Multi-Thread Synchronization vs. Real-Time

When dealing with multi-threading, some of ue are confused by real-time and thread synchronization. Sometimes we are not sure about which we need in our applications.

By real-time, I mean that the simulation time your program completes should go hand-in-hand with your wall clock, no faster nor slower. Let's say you're simulating the traffic on a highway. If you want to simulate traffic for a period of one second, then your program should complete that one-second simulation within one second time. Only this way you can acheive real-time. Multiple CPUs can help to acheive real-time through better parallelism. But if the computation is very simple, one CPU can do the job too. As you may notice now, parallelism is not a necessity in pursuing real-time, as lon as you can catch up the wall clock.

Synchroniztion is a different issue. It is about the collaboration between two threads. It has nothing to do with how fast or slow your CPU is. You don't need multiple or faster CPUs to acheive synchronization. One of the major purposes for synchronization is protecting resources shared by more than one thread.

Tuesday, June 20, 2006

On Windows, Why Should I Use _beginthreadex Instead of CreateThread/Ex to Create Threads?

In genral, using CreateThread is not thread safe. It is not because CreateThread/Ex is not thread safe itself, but because using it in applications linked with a CRT (C/C++ Run-Time) library is not. Two noticeable items:

1) The applications built on VC++ 6, regardless of type, have been linked to one of the CRT libraries shiped with VC++:

Single-Threaded (default)
Multithreaded DLL
Debug Single-Threaded
Debug Multithreaded
Debug Multithreaded DLL

You could avoid calling CRT functions in your module, but you have no control over who, where and when CRT is used in the application.

2) For multithreaded C/C++ to work properly, a data structure must be created and associated with each thread that uses CRT functions. CreateThread is an OS call, but _beginthreaded is a CRT call. If you call CreateThread, OS doesn't know:
- your application is written in C/C++,
- you are calling functions that aren't natively thread-safe
- it needs to allocate a CRT data block for the new thread
When you call _beginthreaded, CRT knows and handles all these in a proper manner. Then, CRT calls CreateThread inside _beginthreaded to actually create the thread you wanted.

Jeffery Richter's classic text "Programming Applications for Microsoft Windows" (4th Ed.) Chapter 6 has explained this in more detail.

.NET FAQ: How to Shutdown My Computer Programatically in C#?

Use .NET Process class. Make this call to its Start method:
System.Diagnostic.Process.Start("shutdown.exe", "-s -f -t 00");
For more info on how to use .NET Process class, check this out: click here.

Get This Blog Organised A Bit...

I found some time to get this blog organised into categories. Currently, I only opened two specific categories: .NET FAQ and ToolBox, since most my posts so far are in the twoe areas.

I'd like to hear what you guys think and how I can improve the blog.

Monday, June 19, 2006

.NET FAQ: Can A Button-Click Event on One Window Be Handled by Another?

Well, while this is entirely possible, you should avoid doing so. There are good reasons for this recommendation. Tow important ones are:
1) The button-click events are handled through .NET event model, in which when an event occurs, the associated delegate will be called. The delegate is a private member function of the form class. Letting another form to handle this event will break the protection of the delegate function.
2) When the delegate is called, the sender object is passed as an input parameter. In this case, it is the button that has been clicked. Again, the button object usually is a private data member of the form class. Letting another form to handle this event will inevitably expose the button object to tat foreign form.

Are there other ways that the button click on one window (Form A) can be handled by another (Form B)? Yes. Within the event handler, Form A can call into Form B in any proper way you want. For example, in the event handler, you may define a button-type (or click-type) parameter and pass it to the callback function of Form B. Then, Form B can take different actions based on the button parameter from Form A.

Monday, June 05, 2006

.NET FAQ: Is Observer Pattern a Better Alternative to Event Model for GUI?

Observer pattern probably is one of the most often used design patterns originally recommended by "The Gang of Four". In many cases, the Observer model can work as a GUI notification mechanism, but it is less than ideal. Just like any other patterns, Observer has numerous limitations, particularly when it is used on GUI. Some important limitations are:

1) As with any interface-based design, your oberver model is an very intrusive and very public solution. Suppose you wanted to subscribe to many notifications, not one. This is typically what you do with GUI. A typical GUI generates a large number of events to which you might need to subscribe.

2) Another related problem is that use of an interface means that your Update() method must be public. Any code then can call it at any time.

3) That said, Observer has to be interface-based. Because if you don't use interface-based design, SubjectBase class would be tightly coupled to GUIObservser class. In other word, only GUIObserver instances can attach to a Subject. If some other class has a method that you want Notify() to call, you'll have to create a whole new SubjectBase-like class. So the problem is not in the interface; it is Observer model's limitation.

4) Observer model handles one-to-many subscription fairly well. By one-to-many, I mean that many observers subscribe to one subject. But when an event fires, the notifications to all the subscribed observers are linear, meaning that observers will get callback one by one following exactly the same order for every loop. There are variants that instead of direct callbacks, Subject puts a message on each observer's message queue. This way, they create a "random" notification mechanism.

5) Thread-safety issue. There usually is very tight coupling between Subject and Observers. when notifying, Subject has to call into Observers' update() one by one. If one call has not returned promptly, or caught some exception, next call cannot be made. To avoid this, some usually implements a multithreaded refresh mechanism on the Subject side. So now the thread-safety becomes an issue, as the observer's data has to be proteced while it's being updated.

6) .NET Delegate/Event model solves some of these problems and creates a safer, more robust, more encapsulated style of notification. Specifically, I think it solves the problems 1-3 cleanly. I'm not quite sure whether the event model is thread safe by itself, though.

On Windows, I think the original messaging model and now the new event model is an better option than primitive Observer model. I still use Observer patterns a lot, but not for replacing Windows event model. On non-Windows platforms, I do see numerous examples of using Observer pattern as a GUI notification mechanism (I guess, Unix/Linux platform does not have a built-in messaging framework like Windows). I am pretty sure that many developers out there use Observer pattern on many other areas as well.

Tuesday, May 02, 2006

.NET FAQ: How .NET Applications Use Unmanaged C++ DLLs with No Source Code?

Currently, I have run into a question regarding how managed code can call unmanaged C++ classes stored in a DLL, without unmanaged source code. The managed code can be in any .NET compliant language, C++, C#, VB, or J#. Lets look at a simple example.
class __declspec(dllexport) Vehicle 
Vehicle(char* idx);
virtual ~Vehicle();
char* GetId() const;
virtual void Move();
char* id;
class __declspec(dllexport) Car : public Vehicle
void Move();

Aassume the two classes were built in a DLL and you don't have the source code. How can your .NET applications use the unmanaged C++ classes stored in this DLL?

In my recent article(here), I've demonstrated a wrapping approach to reusing unmanaged C++ libraries, particularly when direct importing from unmanaged DLLs becomes necessary. I have demonstrated three steps to wrap unmanaged C++ DLLs for use in .NET applications:
1) Retrieve class member data from the DLL.
2) Import required class methods.
3) Wrap up all the imports in a managed class.

The tutorial also showed that the implemention of the approach is not trivial, mainly because you must recover the original relationship between unmanaged classes, such as inheritance, virtual functions and polymorphism. Managed C++ can help, but when there are conflicts, you have to simulate some C++ compiler internals. In working with C++ internals, you will find virtual table and function pointer helpful.

Sunday, April 23, 2006

ToolBox: Some Thoughts about XLogoff Tool

While XLogoff tool (you can acquire here) does what it was designed to do, people are now looking for something beyond the current state. Here are some comments by a user:

"Thanks so much for what you've created. I just tried XLogoff out and while I can appreciate the work you put into it and what it does do - I'm looking for something that goes a little bit further. As a matter a fact I went looking for a 'solution' to this problem when earlier today I mistakenly logged off my Windows XP system when I had about 6 IE browser windows open on various pages that I was currently or had planned through the day to read. I also had a word document open to a file on my local drive. I had 'saved' the file before logging off so no real concern here.

"Ideally I would like a solution such as yours or someone else's that would be able to remember that I had 6 IE windows open, what url's they were each looking at and that I had MS Word open to a particular document. An additional bonus would be if I had a notepad text document with text in it but not yet saved to the hard drive that the 'solution' be able to temporarily save the state of this work."

This user raised a very good question and I completely understand his requirements. I actually thought about this issue before I published the article. It appears that different applications store currently-opened files/URLs/etc in different ways. For instance, Internet Explore saves opened contents in a temporary folder. This information usually does not go with the process data. Currently, I don't have a general solution to the issue (other than treating them individually), but it certainly is worth further investigation. I'll keep you guys posted if any progress has been made toward this direction.

Thursday, February 23, 2006

ToolBox: Save Opened Windows before Logout Or Shutdown

If you have used Unix or Linux systems, you will find their shutdown option window is quite helpful. It allows the user to save all applications current running before logout, shutdown or restart. Sadly, Microsoft Windows does not have such a function. I have just developed a Windows shutdown tool called "XLogoff" and posted it on "Code Project". With this tool, Windows can restore all the saved applications automatically when you login next time.

This tool, along with the source code, is free. You can download it from here by registering with the website. If you don't want to register, contact me by e-mail.

Thursday, January 19, 2006

ToolBox: Monitoring Gasolin Prices through The Internet

I've posted a gas price web monitoring tool on "CodeProject". The tool monitors the gas price at any gas station of your choice around Great Toronto Area (GTA) of Canada. When the price you set has been reached, the tool will notify you by popping up a window and playing some music. You may configure the tool to monitor gas prices automatically when your computer is started. You also can use it as a simple web browser.

The tool is free software, written in C# for Visual Studio 2005 on Microsoft.Net Framework 2.0. The entire source code is delivered free of change. If you just want to use the tool, all you need is Framework 2.0 Re-distributable. I've also built a Windows Installer for the tool, with which you can install the tool by a click. The Installer is not posted on the "CodeProject" site, but you can contact me if you want the Installer.

Wednesday, January 18, 2006

How Python and C/C++ Work Together?

Recently, I have had a chance to work on integrating Python modules with C/C++ applications. Python is a powerful interpreted (and scripting) language, like Perl and PHP. Along with the available tools and libraries, Python makes a good choice for modeling and simulation developers. Best of all, it's free and the tools written for Python programmers are also free. For more details on the language, visit the "official website".

Now, you have got some modules written in Python by others and you want to use them in C/C++ applications. You are experienced in C/C++, but fairly new to Python. You might wonder if you can find a tool to convert them to C code, like the conversion from FORTRAN. The answer is no. The only choice you're having is to work on bringing Python and C/C++ together.

There are two general approaches to integrating Python with C/C++. One is embedding, in which you use Python/C API library to call Python modules directly, as you call any other C/C++ functions. I'm not going to say more about this approach. For more details, see my articles on "CodeProject".

In contrst, the second approach allows Python and C/C++ modules to run separately and talk to each other through an IPC mechanism. This really is a "black-box" approach, in which your C/C++ code does not call Python modules at all. Instead, they communicate with each other through message exchanges.

The IPC approach requires that you convert your Python modules into executables and libraries so that you can run them as independant applications. There are several such tools that can help, one of which is called "cx_Freeze", which is portable across platforms. I've tested it on Windows XP, RedHat Linux and SGI Irix 64. When implementing this approach, you need to do two things:

1) Write IPC interfaces for both Python and C/C++ modules. I found it fairly simple to implement a memory-mapped files (MMAP) IPC on both Python and C/C++ sides.

2) Convert each Python module to an executable with associated libraries. Then you are ready to run Python and C/C++ modules as separate processes and let them talk to each other through the IPC.

Personally, I prefer the first approach as it gives us a "white-box" control over Python modules. One drawback about the first approach is that it demands more intimate knowledge of the Python implementation.