views:

90

answers:

3

I want to write a C++ program that plays MP3. Among available MP3 decoding libraries, I chose mpg123.

I noticed that, besides being able to link against libmpg123 and make the necessary function calls in my code, the library includes a back-end/front-end interface that enables me to communicate with it's executable, and thus not having to include it's code in my program.

What are the advantages of writing a front-end rather than simply linking against the library?

A: 

As far as I can see, the only use of the executable would be for testing purposes. You would run this third-party lib as an executable to understand the behavior of the various APIs offered so you can understand better its usage from your code and to see how they work with various input. After that you would link it to your process so that the library calls are inside your process's address space. If you just run the 2 executable's concurrently you would also have the IPC overhead.

Can you elaborate on "IPC overhead"?
Lawand
If you have two processes communicating with each other then each process runs in its own address space. So to communicate with each other you must use Inter Process Communication facilities. Depending on what is your performance goal, this overhead (to communicate through IPC primitives) may be of interest, since the performance will not be the same as when calling an API in your own address space.
+4  A: 

Most of the advantages comes from process separation between your executable and the library executable:

  • Increased safety & security: if the library is crashing, this will not crash your application.
  • Implicit multi-processing: since both are running on separate processes, this is almost for free.
  • Predisposition to networking: if communication between processes is done with pipes or stdin/stdout, you can easily forward them to sockets and run your executable on a separate machine.
  • Language neutral: you can use whatever programming language you want.

Of course, there is a performance penalty by using an external communication channel. But the benefits of having such decoupling can be quite impressive.

Gabriel Cuvillier
+1 but "Increased security"? If the library is crashing and runs as a separate process it may be a very bad ending with a long thread on bugtraq.
doc
+1  A: 
  1. You can upgrade the backend without recompiling your program.
  2. If the backend crashes, it probably doesn't take your program with it.
Jonathan Leffler
Ad 1. This can be also done with shared library if only ABI doesn't change. If an interface needs change, then it has to be changed in beckend just as it would be in the library. So there's no benefit at this.
doc