OS2 World Community Forum

OS/2, eCS & ArcaOS - Technical => Programming => Topic started by: Martin Vieregg on March 21, 2020, 11:42:28 am

Title: Shared Memory correct usage
Post by: Martin Vieregg on March 21, 2020, 11:42:28 am
I want to use one Shared Memory in both directions. Samples in the internet are poor.

In the server, I wrote:

rc := DosAllocSharedMem(SharedMem, NULL, 4096/*smaller has no effect*/, PAG_WRITE | PAG_READ | PAG_COMMIT | OBJ_GETTABLE);


SharedMem is an untyped pointer.

I transfer the pointer value via commandline parameter to the client.

The pointer value is hold in ulong4.

In the client, I wrote:

      SharedMem = (VOID*) ulong4;
      SharedMem2 = (VOID*) (ulong4+8);
      rc = DosGetSharedMem(SharedMem, PAG_READ | PAG_WRITE);
      PSharedULONG = (PULONG) SharedMem;
      PSharedULONG2 = (PULONG) SharedMem2;

     
I can write the values in the server and read the values in the client, but not reverse (crash). I wrote in the client:

    *PSharedULONG2 = 2;
    //Now it crashes. PSharedULONG also crashes.


Where is my mistake?

Title: Re: Shared Memory correct usage
Post by: Martin Vieregg on March 21, 2020, 12:21:47 pm
I'm sorry. The code above is correct ! The reason why the program hangs was in the server. I use the shared memory together with event semaphores. I forgot to call DosResetEventSem after receiving the event semaphore with DosWaitEventSem. So the client always sends the same semaphore to the server and the whole computer falls onto its knees, it looked like the client hangs.
Title: Re: Shared Memory correct usage
Post by: Laurence Pithie on March 21, 2020, 01:51:46 pm
As a suggestion, for avoiding pitfalls such as you describe in future, you should abstract your shared memory usage and wrap it in an api.