Thursday, December 18, 2014

What is the difference between mutex and critical section on Windows?

There are main differences between mutex and critical section on Windows:
  1. Mutex is a kernel object. Critical section is implemented with using interlocked operations and in rare cases it uses event.
  2. In most cases critical section doesn't require switches from user mode to kernel mode, that is why it is much faster than mutex in general.
  3. Mutex can be used for synchronization of threads between different processes, but critical section can't be.
  4. Mutex can be named, but critical section can't be.
  5. Thread can wait for a mutex with using timeout, but when it is locked on critical section then timeout parameter doesn't supported.

Tuesday, December 9, 2014

Why do I get this error LNK2019: unresolved external symbol __malloc_dbg referenced in function?

I've faced with the same issue several minutes ago.
It has happened because I had set /MT by mistake instead of /MTd:

It was:


but it should be:

Monday, November 10, 2014

What is the purpose of universal hashing?


Suppose you are participating in programming competition. The task for you is to write a hash function that will process an input data as fast as possible. You can see and test other programmers' solutions as well as they can see your source code and generate the worst-case input. The winner is the programmer who will write the fastest hash function.

Problem is that any fixed hash function is vulnerable to such terrible worst-case behavior. Even if you write sophisticated hash function it will be possible to choose n keys that all hash to the same slot, so complexity of searching in average case will be O(n) instead of O(1).

Instead of writing sophisticated hash functions you might pay attention to randomness. Randomness not in the implementation of hash function, but in choosing hash function from set of multiple hash functions.

The approach when hash function is choosing randomly in a way that is independent of the keys that are actually going to be stored is called universal hashing.

So, purpose of universal hashing is to eliminate the vulnerability that can be used to slow down your program execution.

Wednesday, September 10, 2014

How to use shared memory for process intercommunication on Linux?

On Linux intercommunication with using shared memory API is more elegant than on Windows.

Shared memory server


Example sequence of calls for shared memory server is:
  1. Call ftok function and receive key for shared memory segment.
  2. Call shmget with IPC_CREAT to create new shared memory segment.
  3. Call shmat to attach to shared memory array. Do some operations with shared memory.
  4. Call shmdt to detach from shared memory array.
  5. Call shmctl with IPC_RMID to remove shared memory segment.
Here is an example of shared memory server which writes Hello World message to shared memory and waits for pressing of any key:

#include <stdio.h>

#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <cstring>
#include <fcntl.h>

int main(int argc, char ** argv)
{
        key_t key = ftok(argv[0], 1);
        if (key < 0)
        {
                perror("ftok"); /*Displays the error message*/
                return -1;
        }

        printf("Key for shared memory: '%s'\n", argv[0]);

        int shmid = shmget(key, 4096,  IPC_CREAT | O_EXCL | S_IRUSR | S_IWUSR);
        if(shmid == -1)
        {
                fprintf(stderr, "Failed to create memory segment. %s\n", 
                        strerror(errno));
                return -1;
        }

        void * pAddr = shmat(shmid, NULL, 0);
        if((long long )pAddr == -1)
        {
                if(shmctl(shmid, IPC_RMID, NULL) == -1)
                {
                        fprintf(stderr, "Failed to remove shared memory segment. %s\n", 
                                strerror(errno));
                        return -1;
                }

                fprintf(stderr, "Failed to attach to shared memory. %s\n", 
                        strerror(errno));
                return -1;
        }

        const char * szcServerMessage = "Hello world!";
        strcpy((char *) pAddr, szcServerMessage);

        printf("Server put message to shared memory.\n");
        printf("Press any key for exit ...\n");
        getchar();

        if(shmdt(pAddr) == -1)
        {
                fprintf(stderr, "Failed to detach from shared memory segment. %s\n", 
                        strerror(errno));
                return -1;
        }

        if(shmctl(shmid, IPC_RMID, NULL) == -1)
        {
                fprintf(stderr, "Failed to remove shared memory segment. %s\n", 
                        strerror(errno));
                return -1;
        }

        return 0;
}

Shared memory client

Example sequence of calls for shared memory client is:
  1. Call ftok function and receive key for shared memory segment.
  2. Call shmget to obtain pointer to existing shared memory segment descriptor.
    Don't set IPC_CREAT flag, besides instead memory size you can simply pass 0.
  3. Call shmat to attach to shared memory array. Do some operations with shared memory.
  4. Call shmdt to detach from shared memory array.
Please notice that on client side you shouldn't call shmctl with IPC_RMID, because shared segment was created on server side, but not on client.

Here is implementation of shared memory client that attaches to the shared memory segment and then reads message:

#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <cstring>
#include <fcntl.h>

int main(int argc, char ** argv)
{
        key_t key = ftok("./server", 1);
        if (key < 0)
        {
                perror("ftok"); /*Displays the error message*/
                return -1;
        }

        int shmid = shmget(key, 0,  S_IRUSR | S_IWUSR);
        if(shmid == -1)
        {
                fprintf(stderr, "Failed to create memory segment. %s\n", 
                        strerror(errno));
                return -1;
        }

        void * pAddr = shmat(shmid, NULL, 0);
        if((long long )pAddr == -1)
        {
                fprintf(stderr, "Failed to attach to shared memory. %s\n", 
                        strerror(errno));
                return -1;
        }

        char szcBufferMessage[4096];
        strcpy(szcBufferMessage, (char *) pAddr);

        printf("Client read message from shared memory: %s \n", szcBufferMessage);
        printf("Press any key for exit ...\n");
        getchar();

        if(shmdt(pAddr) == -1)
        {
                fprintf(stderr, "Failed to detach from shared memory segment. %s\n", 
                        strerror(errno));
                return -1;
        }

        /* In client we shouldn't delete any segment, because it is allocated by server */

        return 0;
}

Server's output example:

$ ./server
Key for shared memory: './server'
Server put message to shared memory.
Press any key for exit ...

Client's output example:

$ ./client
Client read message from shared memory: Hello world! 
Press any key for exit ...

As you can see client successfully reads the message from server.
Hope this post is useful. Good luck!

Friday, September 5, 2014

How to use shared memory for process intercommunication on Windows?

Introduction
Shared memory is the fastest way for process intercommunication. In the other hand it is the most danger. One mistake in code will crash your program at once.

Shared memory server
Here is an example of shared memory server that allocates shared memory and writes there a message.
#include <windows.h>
#include <conio.h>
#include <stdio.h>

const unsigned int c_uiSharedMemorySegmentSize = 512;

const char * szcSharedMemorySegmentName = "Global\\HelloMessage";

int main()
{
 printf("Shared memory server has been started successfully.\n");

 HANDLE hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,   // use paging file
    NULL,       // default security
    PAGE_READWRITE,     // read/write access
    0,        // maximum object size (high-order DWORD)
    c_uiSharedMemorySegmentSize, // maximum object size (low-order DWORD)
    szcSharedMemorySegmentName); // name of mapping object

 if (hMapFile == NULL)
 {
  fprintf(stderr, "Could not create file mapping object (%d).\n", GetLastError());
  return 1;
 }

 char * pcBuf = (char *) MapViewOfFile(hMapFile,   // handle to map object
  FILE_MAP_ALL_ACCESS, // read/write permission
  0,
  0,
  c_uiSharedMemorySegmentSize);

 if (pcBuf == NULL)
 {
  fprintf(stderr, "Could not map view of file (%d).\n", GetLastError());
  CloseHandle(hMapFile);
  return 2;
 }

 // Set memory for child process
 const char * szcMessageHelloWorld = "Hello world!";
 strcpy(pcBuf, szcMessageHelloWorld);

 printf("Message has been written to shared memory segment.\n");
 printf("You can start the client to read it.\n");
 printf("Press any key to remove shared memory segment and exit ...\n");

 getch();

 UnmapViewOfFile(pcBuf);

 CloseHandle(hMapFile);

 return 0;
}

Shared memory client
Here is an example of shared memory client that attaches to the shared memory and reads the message from the server.
#include <windows.h>
#include <stdio.h>
#include <conio.h>

const unsigned int c_uiSharedMemorySegmentSize = 512;
const char *  szcSharedMemorySegmentName = "Global\\HelloMessage";

int main()
{
 printf("Shared memory client has been started.\n");
 printf("Press any key to read the message from shared memory.\n");
 getch();

 HANDLE hMapFile = OpenFileMapping(
     FILE_MAP_ALL_ACCESS,   // read/write access
     FALSE,       // do not inherit the name
     szcSharedMemorySegmentName); // name of mapping object

 if (hMapFile == NULL)
 {
  fprintf(stderr, "Could not open file mapping object (%d).\n", GetLastError());
  return 1;
 }

 const char * pcBuf = static_cast<char *> (MapViewOfFile(hMapFile, // handle to map object
  FILE_MAP_ALL_ACCESS,           // read/write permission
  0,
  0,
  c_uiSharedMemorySegmentSize));

 if (pcBuf == NULL)
 {
  fprintf(stderr, "Could not map view of file (%d).\n", GetLastError());
  CloseHandle(hMapFile);
  return 2;
 }

 printf("Message from shared memory server: '%s'\n", pcBuf);
 printf("Press any key to exit ...");
 getch();

 UnmapViewOfFile(pcBuf);
 CloseHandle(hMapFile);

 return 0;
}
After you successfully built both examples you can start server at first:

Then start shared memory client and press any key:

As you can see client successfully reads the message from server.

Hope this post is useful. Good luck!


Thursday, September 4, 2014

How to do performance profiling of server process on Linux?

Suppose you have a server process on Linux and you want to investigate its performance.
To do performance profiling you need to run your server with using valgrind:

valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --collect-jumps=yes ./path_to_server arg1 arg2

After you started the server you needed to wait some time while your server processed data.
As soon as data has been processed you may stop server.
As the result valgrind should generate file(s) with names like that: callgrind.out.58575.

Now the biggest part of job has been done already.
Next step is to open generated callgrind.out.* file(s) by using kcachegrind.

If you are interested in performance profiling child processes I'll bring you good news.
You can achieve it just by adding --trace-children=yes argument to valgring command line:

valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --collect-jumps=yes --trace-children=yes ./path_to_server arg1 arg2

Hope this post is helpful. Good luck!

How connect to oracle database without using entry in tnsnames.ora?

Lets suppose you want to connect to database service with next description:

HOLE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.108.1.1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = hole.service)
    )
  )

Instead of adding it to tnsnames.ora you may write it instead of database name in your application :



If you don't want to write a long description you can use another format:
<host>:<port>/<service_name>

For example:
192.108.1.1:1521/hole.service

It will work as well as first case. Both examples represent the subset of valid oracle connection strings.

Moreover, you can use these connection strings in Oracle API methods instead of TNS names.
For example, one of these methods is OCISessionPoolCreate.

Good luck!





Wednesday, May 14, 2014

How to use beyond compare to show difference between commited files in local git repository and modified files?

For me it was enough to run next commands from git bash:

$git config --global diff.tool bc3

$git config --global difftool.bc3.path C\:/Program\ Files\ \(x86\)/Beyond\ Compare\ 3/BCompare.exe

$git config --global difftool.prompt false

Then you can use difftool command to see differences for each file which is modified:

$git difftool

Friday, April 4, 2014

What is more convenient Flask or Wt (witty)?

I experienced in c++ and almost don't know python.
My goal was to build simple web application with using c++.
I was looking for convenient c++ framework for this goal.
Wt was looked as the most documented and convenient.

After a week of installing and digging i got how wt working and i tried to build simple page, where text widget should be on the all area of page. It took me half a day experimenting with css and different ways of settings sizes and so on. It hadn't work as it should, so i decided that Wt a big black box with the a lot of surprises. You have no idea how widget is implemented and how it will behave. Since i started to suspect it is just beginning of the problems.

I decided not to give up at with Wt, but try to use Flask python framework, because I was bored by Wt very much.
It took half a day to read about Flask, install it and build simple Flaskr application which is an example application from manuals.
It was for me like a breath of fresh air.
The result html code was clean and it worked much faster then Apache+Fastcgi+Wt.

I am not saying that Wt is bad or it not usable i just express my subjective vision about this framework.
I am sorry that i spent, so much time with Wt instead of doing  worth things.

How to install Python Flask web framework on Debian Wheezy?

Install Flask is very easy and there shouldn't be any troubles with it if you will do the next steps:

1. Install virtual environment for python
This is additional package which is used to provide your future application personal python version.

$ sudo apt-get install python-virtualenv

2. Create your future project directory
Suppose first will be hello_world:
$ mkdir hello_world

3. Create virtual environment for your hello_world project
Suppose virtual environment name will be venv:
$cd ./hello_world
$ virtualenv venv

I had output like that:
New python executable in venv/bin/python
Installing setuptools, pip...done.

4. Activate your environment
$. venv/bin/activate

How your shell prompt should begin from (venv).

5. Create file hello_world.py in current directory with next content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

6. Start application listening on 5000 port

$ python hello_world.py
 * Running on http://127.0.0.1:5000/

Sunday, March 23, 2014

Google Logger glog. How to turn off buffering?

Buffering of log messages could be an obstacle when you want messages to appear immediately in log file.
To turn off buffering or in another words flush all messages you need set next in your code:

// Initialize Google's logging library. 
google::InitGoogleLogging(argv[0]); 
FLAGS_logbufsecs = 0; 
FLAGS_logbuflevel = google::GLOG_INFO;

Saturday, February 22, 2014

How to check if Wt framework is installed correctly on Debian wheezy?

Answer: it is needed to build Wt examples.
If Wt framework is installed correctly then examples will be built successfully.

Below are steps how to build Wt examples.
Here is considered that Wt is installed with examples and libwtfcgi-dev.
How to install it you can read here.

Steps to build Wt examples on Debian wheezy:

1. Create directory where cmake will create makefiles:
#> mkdir /home/%user%/build/wtexamples
#> cd /home/%user%/build/wtexamples

2.  Run cmake to create makefiles:
#> cmake -DWT_SOURCE_DIR=/usr/lib/Wt \
    -DCONNECTOR_FCGI=ON \
        -DEXAMPLES_CONNECTOR="wtfcgi" \
        -DCMAKE_BUILD_TYPE=Release \
        /usr/lib/Wt/examples

3. Build hello example:
#> cd /home/%user%/build/wtexamples/hello
#> make

In result you should get something like that:

4. If hello example is built successfully then you can try to build all examples:
#> cd /home/%user%/build/wtexamples
#> make

If all examples is built successfully most probably your Wt was installed properly.

How to install Wt framework on Debian wheezy?

Steps to install stable version of Wt framework on Debian wheezy:

0. Update your Debian wheezy:
#>sudo apt-get update

1. Install stable Debian boost version: 
#>sudo apt-get install libboost-all-dev

2. Install witty packages: 
#>sudo apt-get install witty witty-dbg witty-doc witty-dev witty-examples

3. [Optional] If you want to use Wt with fastcgi:
#>sudo apt-get install libwtfcgi-dev

That is all.
How to check if witty is installed correctly you can read here.