Sponsored Content
Full Discussion: shmat() with fixed address
Top Forums Programming shmat() with fixed address Post 302477560 by DoxieLvr on Sunday 5th of December 2010 08:49:23 PM
Old 12-05-2010
The problem is that a valid address running one process may not be valid/available in another. In fact, an address that may be valid on one system may not be valid on another system. That's why it is recommended to use an automatic address assignment.

If all of the communicating processes are the same and fork from one program, you can use shmat() before forking and each process will automatically have the memory segment at the same address.

Otherwise, the best course of action is to not use absolute pointers but instead use some for of relative pointer: for example, an integer offset from the base of the segment. Unfortunately, this requires a bit of code to dereference these "pointers".
 

10 More Discussions You Might Find Interesting

1. HP-UX

shmat() permission denied, what's this?

I have installed an application that runs correctly for root but not other users. The application generates an error that indicates users don't have permission to attach to shared memory. A daemon process creates the shared memory segment. I've checked every permission I can think of but nothing... (2 Replies)
Discussion started by: km4hr
2 Replies

2. Programming

shmat problem

The first shmat in my program cannot fetch the start address of the shared memory. #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> int main(int argc,char *argv) { int pid; key_t mykey; int shmid; long *shm; long *shm2; ... (3 Replies)
Discussion started by: yong
3 Replies

3. Linux

shmat() Failure While Using a Large Amount of Shared Memory

Hi, I'm developing a data processing pipeline with multiple stages, with data being moved between the stages using shared memory segments. The size of the data is typically of the order of hundreds of megabytes, and there are typically a few tens of main shared memory segments each of size... (2 Replies)
Discussion started by: theicarusagenda
2 Replies

4. AIX

sharemem-f-shmat

Hi, I got this error from AIX 4.3.3/db2 7: * %SHAREMEM-F-SHMAT Error getting PUTPARM shared memory address , shmid=23029 * * 3630, errno=13 Anybody who know how to fix this? Thanks in advance, itik (1 Reply)
Discussion started by: itik
1 Replies

5. UNIX for Advanced & Expert Users

Problem shmat in HP-UX. EINVAL Error

I have a process that need two active connections to the same zone of shared memory. But when i do the second call to shmat it give me error 22 (EINVAL). Only works ok the second call to shmat if i disconnect the first connection (shmdt) In Sun,AIX and Digital, i donīt have this problem. ... (7 Replies)
Discussion started by: dairby
7 Replies

6. Programming

Problem shmat in HP-UX Titanium ia64. EINVAL Error

I have a process that needs two active connections to the same zone of shared memory simultaneously. The firs conection works ok, but when i do the second call to shmat it give me error 22 (EINVAL). Only works ok the second call to shmat if i disconnect the first connection (shmdt) Steps:... (3 Replies)
Discussion started by: dairby
3 Replies

7. Debian

Question on the dhcp-server configuration for a fixed IP address.

Hi , when i configure my server with a fixed ip address in dhcpd.conf as below subnet 172.21.151.0 netmask 255.255.255.0 { range 172.21.151.66 172.21.151.66; } host switch { hardware ethernet 00:05:30:02:DB:31; fixed-address 172.21.151.66; } when i start dhcpd server , it... (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

8. UNIX for Advanced & Expert Users

Doubt on fixed ip address assinged by dhcp server?

we can assign a dynamic and fixed ip address for a dhcp_client through dhcp server , if it is dynamic ip address , it would be having lease time and renews after half of the leasetime gets over. would the same funda applies for fixed ip address to .. i dont think so ... please confirm on the... (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies

9. SuSE

shmat failed due to Cannot allocate memory

Hi, My program was running for a whole night. after 12 hours i got an error message "Cannot allocate memory" during the shmat commmand. So can you please let me know what could be the reason? is there any solution? thanks in advance. Regards, Mano (5 Replies)
Discussion started by: ManoharanMani
5 Replies

10. UNIX for Advanced & Expert Users

C program to detect duplicate ip address if any after assigning ip address to ethernet interface

Hi , Could someone let me know how to detect duplicate ip address after assigning ip address to ethernet interface using c program (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies
SHMOP(2)						     Linux Programmer's Manual							  SHMOP(2)

NAME
shmop - shared memory operations SYNOPSIS
#include <sys/types.h> #include <sys/shm.h> void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(const void *shmaddr); DESCRIPTION
The function shmat attaches the shared memory segment identified by shmid to the address space of the calling process. The attaching address is specified by shmaddr with one of the following criteria: If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment. If shmaddr isn't NULL and SHM_RND is asserted in shmflg, the attach occurs at the address equal to shmaddr rounded down to the nearest mul- tiple of SHMLBA. Otherwise shmaddr must be a page aligned address at which the attach occurs. If SHM_RDONLY is asserted in shmflg, the segment is attached for reading and the process must have read permission for the segment. Other- wise the segment is attached for read and write and the process must have read and write permission for the segment. There is no notion of a write-only shared memory segment. The brk value of the calling process is not altered by the attach. The segment will automatically be detached at process exit. The same segment may be attached as a read and as a read-write one, and more than once, in the process's address space. On a successful shmat call the system updates the members of the shmid_ds structure associated to the shared memory segment as follows: shm_atime is set to the current time. shm_lpid is set to the process-ID of the calling process. shm_nattch is incremented by one. Note that the attach succeeds also if the shared memory segment is marked to be deleted. The function shmdt detaches the shared memory segment located at the address specified by shmaddr from the address space of the calling process. The to-be-detached segment must be currently attached with shmaddr equal to the value returned by the its attaching shmat call. On a successful shmdt call the system updates the members of the shmid_ds structure associated with the shared memory segment as follows: shm_dtime is set to the current time. shm_lpid is set to the process-ID of the calling process. shm_nattch is decremented by one. If it becomes 0 and the segment is marked for deletion, the segment is deleted. The occupied region in the user space of the calling process is unmapped. SYSTEM CALLS
fork() After a fork() the child inherits the attached shared memory segments. exec() After an exec() all attached shared memory segments are detached from the process. exit() Upon exit() all attached shared memory segments are detached from the process. RETURN VALUE
On failure both functions return -1 with errno indicating the error. On success shmat returns the address of the attached shared memory segment, and shmdt returns 0. ERRORS
When shmat fails, errno is set to one of the following: EACCES The calling process has no access permissions for the requested attach type. EINVAL Invalid shmid value, unaligned (i.e., not page-aligned and SHM_RND was not specified) or invalid shmaddr value, or failing attach at brk. ENOMEM Could not allocate memory for the descriptor or for the page tables. The function shmdt can fail only if there is no shared memory segment attached at shmaddr, in such a case at return errno will be set to EINVAL. NOTES
Using shmat with shmaddr equal to NULL is the preferred, portable way of attaching a shared memory segment. Be aware that the shared mem- ory segment attached in this way may be attached at different addresses in different processes. Therefore, any pointers maintained within the shared memory must be made relative (typically to the starting address of the segment), rather than absolute. The following system parameter affects a shmat system call: SHMLBA Segment low boundary address multiple. Must be page aligned. For the current implementation the SHMBLA value is PAGE_SIZE. The implementation has no intrinsic limit to the per-process maximum number of shared memory segments (SHMSEG). CONFORMING TO
SVr4, SVID. SVr4 documents an additional error condition EMFILE. In SVID-v4 the type of the shmaddr argument was changed from char * into const void *, and the returned type of shmat() from char * into void *. (Linux libc4 and libc5 have the char * prototypes; glibc2 has void *.) SEE ALSO
ipc(5), shmctl(2), shmget(2) Linux 2.5 2002-01-05 SHMOP(2)
All times are GMT -4. The time now is 12:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy