The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shared memory in shared library DreamWarrior High Level Programming 12 05-30-2007 04:33 PM
memory sharing - not shared memory - elzalem High Level Programming 9 05-02-2007 07:45 AM
help with shared memory ddx08 High Level Programming 6 04-06-2007 09:53 AM
Shared memory shortage but lots of unused memory cjcamaro UNIX for Advanced & Expert Users 1 10-13-2004 05:10 PM
Shared memory joseph_shibu High Level Programming 1 10-24-2001 08:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-02-2003
Jayathirtha Jayathirtha is offline
Registered User
  
 

Join Date: Aug 2001
Location: Bangalore
Posts: 16
Shared Memory

Hi,

I was trying to use shared memory concept. I am very much new to this concept.

Once you create a pointer to a shared memory, what will be the scope of it ?

If i want to use it in a different process, then how should i use it ?

Please advice,

Thanks,
- Jay.
  #2 (permalink)  
Old 03-03-2003
Deepa Deepa is offline
Registered User
  
 

Join Date: Apr 2002
Posts: 53
The shared memory is created using the shared memory key. This key is the one that can be used by any process to access the shared memory.

You do a 'shmget()' to get the shared memory id for the given shared memory key....and a 'shmat()' to attach the shared memory to your process' address space....this , in turn, gives you back a pointer pointing to the attached shared memory. This pointer will have the scope within the process' life time.

If you want to access the shared memory in another process, use the shmkey to get the shmid (using shmget) and attach it to the process(using shmat).
  #3 (permalink)  
Old 03-03-2003
Jayathirtha Jayathirtha is offline
Registered User
  
 

Join Date: Aug 2001
Location: Bangalore
Posts: 16
Deepa,

I referred some books, and unix man pages.

The"key" that you mentioned about, to get the "shmid" should be "IPC_PRIVATE", but IPC_PRIVATE is used while creating a shared memory and not while accessing an existing one.

1. The book mentions about having a secret "key" that can be agreed upon by both the processes. How can the 'key" be decided upon ?

Please advice,

Thanks,
- Jay.
  #4 (permalink)  
Old 03-03-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
The key is not exactly secret. The ipcs command will display it. Think of it as unique.

One way of handling the key, is for a creating process to use IPC_PRIVATE as you suggest. The kernel will then pick a key. Now the creating process needs to tell the other processes about the key.

In many cases, the creating process will now fork and create those processes. The key is still in the data segment somewhere. Data base packages often work like this. Note that we discussing System V shared memory here (there are other kinds) and this was developed prior to threads. These days, anyone about to write something like this might want to consider separate threads rather than separate processes.

If the other processes are not direct desendents of the creating process, then it must use another form of IPC to transmit the key. Or maybe even write it in a file or something. This is why unrelated processes do not usually go with IPC_PRIVATE.

The other way is for the creating process to use a key (not IPC_PRIVATE) that is not currently in use. It is supposed to use ftok() to create this key. One input to ftok is a file. This can be any file. As long as all processes use the same file, and the same 2nd arg to ftok() they will get the same key. The file is not opened, ftok() gets the inode number of the file and device number of the filesystem that contains it. It conbines these two numbers together along with that second argument to get a unique key. Some people bypass ftok and pick a number out of thin air. I would go with ftok().
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0