UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » Interprocess communication via shared memory and semaphores

#1 2006-11-30 10:29 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

I want to make three classes:
Sem - wraps work with semaphores
SharedMemory - works with segment of shared memory(which may include a Sem for synchronization - m_semProtect)
SharedMemQue - Put and Extract from deque which is in shared memory (func Put() posts m_semCount(member),and func Extract() waits m_semCount)).
I want to write a class which wraps work with semaphores,but i have a problem with generating unique keys.(Imagine one appliaction creates some processes which may use this class).Is there any more elegant way to do this generation except of using shared memory for that key?(Maybe using special m_semNextKey(getting it's value and posting each Sem object with new key created))

Offline

#2 2006-11-30 04:10 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

Hmm.. If I generate that key automatically i'll be able to find the number of semaphore i need from another process if i have generated it unknow times.So I must put semKey on my own. )

Offline

#3 2006-11-30 05:13 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Interprocess communication via shared memory and semaphores

class SemaphoreWrapper {
   private:
   int myTemporarySequence;

   ...
   public:
   ...
   key_t getTemporaryKey() {
      myTemporarySequence++;

      // Only the low order 8 bits are used, and must not be 0
      if (!(myTemporarySequence&0xff))
         myTemporarySequence++;

      // No error checking
      return ftok("/tmp", myTemporarySequence);
   }
   ...
};

Offline

#4 2006-12-01 08:07 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

Yeah, I did so frstly, but then I decided, that static member of the class couldn't be seen from another process memory.I was so sure of it, thati even didn't try it.

Offline

#5 2006-12-01 09:06 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

Can i allocate some shared memory,put container there and use it?
:
...shmget..
pShm = shmmat..
deque<T> d;
memcpy(pShm,&d,SHM_MAX);

P.S.I wrote some classes,try to use but got segmentation failed.I can post the code if needed.
Maybe the problem is that stl deque put new elements not in shared memory?
I think I should use mmap instead of shm*

Offline

#6 2006-12-01 06:43 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Interprocess communication via shared memory and semaphores

Offline

#7 2006-12-13 04:30 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

After using shared memory I must free the resource with smdt call and shmct with IPC_RMID flag.I read that it will not remove shm segment wihle another process(es) attached it (obviusly using shmget,shmat calls).But when i tried it on mu own this feature doesn't work .It's removed even when another process uses it.I think i'll use special sem (with #defined key) as counter of references if i wouldn't find a decision of the problem descibed above.Or just make a special function that would free resurce, when i really need it,but not when destructor call occurs.

Offline

#8 2006-12-13 04:48 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

Has anybody tried using stl in shred memory.It seems impossible because it allocates memory for copying objects somewhere I dod't know exactly where and I can't force it to allocate exactly in the place i want(in a shared memory segment).Mayby I just did smth wrong,so i couldn't use stl.But now I decided to make some container in shared memory on my own.Here is attached .vsd (Sorry, I have not studied UML yet, so I just draw smth can be read and understadable)

Offline

#9 2006-12-14 04:39 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

And one more question:Imagine,that  there is a process putting messages to shared memory and process extracting them.If you know,which start first,than all is ok,but what to do,when it's impossible to know,which starts first?To synchronize  read/write operations using sysv semaphores i must do one of them as created,and anothres oe as loaded.And if first is the process creating semaphore,than it sem.Wait until another process loads the semaphore with the same key and call sem.Post.But when first is loading semaphore the situation is : it run in cicle while another process started and created sem.But another process started, created sem and call sem.Wait.So it's blocked.And when i call sem.Post to loaded semaphore nothing happens.If anybody could understand me and give advice it'll be great.

It's not a problem for my task.I can make the third process ,which will create IPC objects,and another processes will just load them.

Offline

#10 2006-12-18 02:17 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

here is an example using classes with shared memory and samaphores.Thanks for any comments in advance.

Offline

#11 2007-02-05 08:43 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Interprocess communication via shared memory and semaphores

I want to synchronize access to shared memory with semaphore and put its key into shared memory.So different processes don't know anything except shared memory name and it's very convenient for me.The question is how to protect the memory from another process while process that creates the memory writes semaphore key to it.I don't want that process,that creates the memory wouldn't be switched while it creates a memory segment and writes semaphore's key to it.I think I can't use phtread_mutex for that.And I don't want to use some global semaphore for that operation.How can I do that?

Offline

  • Index
  • » Processes
  • » Interprocess communication via shared memory and semaphores

Board footer

Powered by FluxBB