Name
shmat, shmdt
- shared memory operations
Library
libc.lib
Synopsis
|
void *
shmat (int shmid, const void *addr, int flag);
|
|
int
shmdt (const void *addr);
|
Return values
Upon success,
shmat
returns the address where the segment is attached; otherwise, -1
is returned and
errno
is set to indicate the error.
The
shmdt
function returns the value 0 if successful; otherwise the
value -1 is returned and
errno
is set to indicate the error.
Detailed description
The
shmat
system call
attaches the shared memory segment identified by
shmid
to the calling process’s address space.
The address where the segment
is attached is determined as follows:
- If
addr
is 0, the segment is attached at an address selected by the
kernel.
- If
addr
is nonzero and SHM_RND is not specified in
flag,
the segment is attached the specified address. (a nonzero
addr
is not supported)
- If
addr
is specified and SHM_RND is specified,
addr
is rounded down to the nearest multiple of SHMLBA.(a nonzero
addr
is not supported)
The
shmdt
system call
detaches the shared memory segment at the address specified by
addr
from the calling process’s address space.
Examples
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define SHM_SEG_SIZE 1024
int main(void)
{
int shm_id;
char *shm_addr;
/*
* Create a shared memory segment
*/
if ((shm_id = shmget(IPC_PRIVATE, SHM_SEG_SIZE,
IPC_CREAT | IPC_EXCL | 0666))
== -1) {
printf("Shared memory create failed with errno %d\n", errno);
return -1;
}
/*
* Attach the shared memory segment to the
* process address space
*/
if((shm_addr = (char *)shmat(shm_id, NULL, 0)) == (void *)-1) {
printf("Shared memory attach failed with errno %d\n", errno);
return -1;
}
/*
* Copy data to shared memory segment
*/
strcpy(shm_addr, "some_random_data");
/*
* Detach the shared memory segment
*/
if(shmdt(shm_addr) == -1) {
printf("Shared memory detach failed with errno %d\n", errno);
}
/*
* Remove the shared memory segment
*/
if(shmctl(shm_id, IPC_RMID, NULL) == -1) {
printf("Shared memory destroy failed with errno %d\n", errno);
}
return 0;
}
Errors
The
shmat
system call
will fail if:
[EINVAL]
|
|
No shared memory segment was found corresponding to
shmid.
|
[EINVAL]
|
|
The
addr
argument
was not an acceptable address.
|
[ENOTSUP]
|
|
The
addr
argument
was not zero.
|
The
shmdt
system call
will fail if:
[EINVAL]
|
|
The
addr
argument
does not point to a shared memory segment.
|
See also
shmctl,
shmget
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia
|
|