Solved

After CreateFileMapping, MapViewOfFile , what next ?

Posted on 1998-11-10
13
957 Views
Last Modified: 2007-10-18
how to put messages into the shared memory i just created ?
0
Comment
Question by:SkyWolf
  • 6
  • 6
13 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1254169
You don't put "messages" into shared memory, you put bytes there.
Seriously, shared memory is treated [almost] like regular mory by the program.  You write to it and read it via the provided pointer (the "almost" bit is because you sould protect shared memory with a mutex to be on the safe side).

0
 

Expert Comment

by:zouying
ID: 1254170
After you call MapViewOfFile, the pointer you get is the pointer pointed to the shared memory, (the HANDLE can be used in different prcocess (program), if the pointer is called pView, use *(pView+100) to modfiy it, after that you should use unmapview to save it.
Hopefully, this is what you want?
Good Luck
0
 

Author Comment

by:SkyWolf
ID: 1254171
How to protect it using mutex ? can i have some sample codes or something so that i can understand better ?
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1254172
Easy.

Start with:
    HANDLE hMutex = CreateMutex(NULL, FALSE, "mutex name");

Immediately before accessing the shared memory do:
    WaitForSingleObject(hMutex, INFINITE);

Immediately after accessing the shared memory do:
    ReleaseMutex(hMutex);

That's all!

Make sure you use the same mutex name in all processes.
0
 

Author Comment

by:SkyWolf
ID: 1254173
what about semaphores ? where and when to include ? b4 mutex or after mutex ?
relasemutex right after i do a processing  ?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254174
>> what about semaphores?
No no no.  I probably wasn't clear enough.  Let's try again:

You should use *either* a mutex *or* a semaphore, *not* both.  A semaphore is more versatile than a mutex (a mutex can be treated as a semaphore with a maximum caount of 1) but for your needs, a mutex is suficient (and probably more efficient).

Now, let's go over the process in more details:

Assume you have several processes (programs) that want to access a shared resource (in your case, shared memory).  Here is the skeleton for the apps:

First, decide on the size of the shared memory you want to use:

    #define SIZE 100 /* 100 bytes in shared memory */

Then, create the shared memory (backed by the swap file) and a mutex (for synchronizing access to the shared memory):

    HANDLE  hMapping;
    HANDLE  hMutex;
    PBYTE   pMem;

    hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
                                 0, SIZE, "My File Mapping");

    pMem = (PBYTE)MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);

    hMutex = CreateMutex(NULL, FALSE, "My Mutex");

That takes care of the initialization.

Now, in some place in the program you want to access the shared memory.  First, you acquire the mutex:

    WaitForSingleObject(hMutex, INFINITE);

Note that the thread will block until the mutex is free.

Then, you access the memory (knowing that no other process (or thread) can reach this point because the mutex is locked):

    pMem[10] = 42; /* or anything similar... */

When you done accessing the memory, you release the mutex which enables another process (or thread) to aquire it:

    ReleaseMutex(hMutex);

Finally, at the end of the program you do some cleanup:

    CloseHandle(hMutex);
    UnmapViewOfFile(pMem);
    CloseHandle(hMapping);

That's all!

Just make sure that *every* access to the shared memory (or any other shared resource) is guarded by the mutex.  If you have several shared resources, you may wish to use several mutexes to improve cocurrency.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:SkyWolf
ID: 1254175
alexo, can u also go thru with the process of semaphore in details ? i tink i need to create multiple access thanks alot.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254176
SkyWolf, you DON'T NEED a semaphore if you use a mutex (just as you don't need a pick-up truck to get to work if you already have a family car).

You use the mutex for multiple access.  My previous comment was very detailed.  I told you step by step what needs to be done.  Do you feel that I omitted anything?  What exactly?
0
 

Author Comment

by:SkyWolf
ID: 1254177
semaphore is more versatile in what sense ? how versatile ? coz i hav a set of program code that uses BOTH which makes me quite confuse since i do not know much on memory programming stuf ..
0
 

Author Comment

by:SkyWolf
ID: 1254178
and i just want to check with u that coding semaphore is the same coding as Mutex ? now i juz want to compare my truck and my car ... *smile* ... if semaphore is more capable than mutex, i tink i will opt for semaphore...there must be some difference of capability between these two functions right ? else there wont b a need for 2 different function names ...but i may b wrong
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254179
>>  i just want to check with u that coding semaphore is the same coding as Mutex

Ah, now I understand.  Let's start with the mutex, it's simpler.

Have you read Goldring's "the lord of the flies"?  It's about a bunch of kids that get stranded on an island and develop a society of sorts.  Anyway, to keep order in their meetings they got a sea-shell and decided that only the person that holds the shell can speak, all the others have to wait until the shell is available.

A mutex is exactly like that sea shell.  A thread can request ownership of a mutex.  Only one thread can own the mutex so if two threads try to do that, one will succeed and the other one will be blocked until the mutex is released.

Incidently, the name "mutex" comes from the fact that it is useful in coordinating MUTually EXclusive access to a shared resource.

A semaphore, if we continue the analogy, is like a box of shells.
A semaphore has a counter.  As long as the counter is greater than zero, the semaphore is available("signalled").  When a thread waits on a semaphore the counter is decremented.  When the counter reaches zero any subsequent thread that tries to wait on the semaphore will get blocked until the counter is increased.

The semaphore object is useful in controlling a shared resource that can support a limited number of users.  It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.  For example, an application might place a limit on the number of windows that it creates.  It uses a semaphore with a maximum count equal to the window limit, decrementing the count whenever a window is created and incrementing it whenever a window is closed.  The application specifies the semaphore object in call to one of the wait functions before each window is created.  When the count is zero (indicating that the window limit has been reached) the wait function blocks execution of the window-creation code.

As you can see, mutexes and semaphores are used for different purposes.  There are several differences:  Threads do not acquire ownership of semaphore objects as they do with mutex objects.  A thread that owns a mutex object can wait repeatedly for the same mutex object to become signaled without its execution becoming blocked.  A thread that waits repeatedly for the same semaphore object, however, decrements the semaphore's count each time a wait operation is completed; the thread is blocked when the count gets to zero.  Similarly, only the thread that owns a mutex can successfully call the ReleaseMutex() function, though any thread can use ReleaseSemaphore() to increase the count of a semaphore object.

So, in your case, a mutex is clearly the correct object to use.
0
 

Author Comment

by:SkyWolf
ID: 1254180
Thanks a lot !
how can I find u or seek your expertise in future ? e-mails ?
Thansk again and if u do not wish to be consulted again ,well ,thanks again !
0
 
LVL 11

Expert Comment

by:alexo
ID: 1254181
>> how can I find u or seek your expertise in future ? e-mails ?
Either here or by email.  Click on the link to my info.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now