0% found this document useful (0 votes)
53 views

#Include : SHM - RDONLY, Which Makes The Attached Memory Read-Only

Shared memory allows unrelated processes to access the same logical memory. It is created using shmget which returns an identifier. shmat attaches the shared memory to a process's address space by taking the identifier and an address. shmdt detaches shared memory from a process. shmctl controls shared memory, allowing querying, modifying, and deleting shared memory segments.

Uploaded by

subrata nandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

#Include : SHM - RDONLY, Which Makes The Attached Memory Read-Only

Shared memory allows unrelated processes to access the same logical memory. It is created using shmget which returns an identifier. shmat attaches the shared memory to a process's address space by taking the identifier and an address. shmdt detaches shared memory from a process. shmctl controls shared memory, allowing querying, modifying, and deleting shared memory segments.

Uploaded by

subrata nandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Shared Memory

It allows two unrelated processes to access the same logical memory.

#include <sys/shm.h>

1. Create shared memory using shmget function.

int shmget(key_t key, size_t size, int shmflg);

The program provides key which effectively names the shared memory segment. Special key
(IPC_PRIVATE) creates shared memory private to the process.

Size- Amount of memory required in bytes

Shmflg- Nine permission flags that are used in the same way as mode flag for creating files.

If the shared memory is successfully created, shmget returns a nonnegative integer, the shared
memory identifier. On failure, it returns –1.

2. When you first create a shared memory segment, it’s not accessible by any process. To
enable access to the shared memory, you must attach it to the address space of a process.

void *shmat(int shm_id, const void *shm_addr, int shmflg);

The first parameter, shm_id, is the shared memory identifier returned from shmget.

The second parameter, shm_addr, is the address at which the shared memory is to be attached to the
current process. This should almost always be a null pointer, which allows the system to choose the
address at which the memory appears.

The third parameter, shmflg, is a set of bitwise flags. The two possible values are SHM_RND, which,
in conjunction with shm_addr, controls the address at which the shared memory is attached, and
SHM_RDONLY, which makes the attached memory read-only.

If the shmat call is successful, it returns a pointer to the first byte of shared memory. On failure –1 is
returned.

3. The shmdt function detaches the shared memory from the current process. It takes a pointer
to the address returned by shmat. On success, it returns 0, on error –1. Note that detaching
the shared memory doesn’t delete it; it just makes that memory unavailable to the current
process.

int shmdt(const void *shm_addr);

4. The control functions for shared memory.

int shmctl(int shm_id, int cmd, struct shmid_ds *buf);

The first parameter, shm_id, is the identifier returned from shmget.

The second parameter, command, is the action to take. It can take three values: IPC_STAT, IPC_SET,
IPC_RMID.
IPC_STAT: Sets the data in the shmid_ds structure to reflect the values associated with the shared
memory.

IPC_SET: Sets the values associated with the shared memory to those provided in the shmid_ds data
structure, if the process has permission to do so.

IPC_RMID: Deletes the shared memory segment.

You might also like