Professional Documents
Culture Documents
Aup Shared Memory
Aup Shared Memory
11/1/22, 8
Shared Memory
Process 1| Process 2
Shared Memory
We know that to communicate between two or more processes, we use shared memory but
before using the shared memory what needs to be done with the system calls, let us see this -
Create the shared memory segment or use an already created shared memory segment
(shmget())
Attach the process to the already created shared memory segment (shmat))
Detach the process from the already attached shared memory segment (shmdt())
Let us look at a few details of the system calls related to shared memory.
1/9
www.tutorialspoint.com/inter_process communication/inter_process_communication_shared_memory.ntnn*
22 84
#include sys/ 1p.hs
sys/shm.hs
sinclude
t . The arguments
The above system all creates or allocates a System V shared memory segment The
or
arbitrary value or one that can be derived from the
library function ftok(). The
can
key
he key
PRIVATE, means, running processes as server and client (parent and
child relatio
this key, then
PAGE_SIZE
REAT
The third argument, shmflg, specifes the required shared memory flag/s such as P
nt and the
(creating new segment) or C_EXCL (Used with IPC_CREAT to create new seg.
call fails, if the segment already exists). Need to pass the permissions as well.
Note - Refer earlier sections for details on permissions.
This call would return a valid shared memory identifier (used for further calls of shared memoiy
on success and -1 in case of failure. To know the cause of failure, check with erno variable o
perror() function.
#incLude <sys/types.h>
#include <sys/shm.h>
The above system call performs shared memory operation for System V shared memory
segment i.e., attaching a shared memory segment to the address space of the calling process.
The arguments that need to be passed are as follows-
The first argument, shmid, is the identifier of the shared memory segment. This id is the shared
memory identifier, which is the return value of shmget() system call.
The second argument, shmaddr, is to specify the attaching address. If shmadr is NULL, the
system by default chooses the suitable address to attach the segment. If shmaddr is not NULL
and SHM_RND is specified in shmfig, the attach is equal to the address of the nearest multiple of
SHMLBA (Lower Boundary Address). Otherwise, shmaddr must be a page aligned address at
which the shared memory attachment occurs/starts.
3:44 AM
Shared Memory
hasSHMRND
he third argument, shmflg, specifies the required shared
ared memory
memory flag/ss segmentto be
of is
executed) or SHM_RDONLY (attaches the segment for read-only purpose it
by
b y s h m a d d ra n d
d e f a u l t
This call would return the address of attached shared memory segment on Uccess
case of failure. To know the cause of failure, check With errno variable or pere 0 f u n c t i o n .
#include <sys/types.h>
#incLude <sys/shm.h>
V s h a r e d m e m o r y
The above system call performs shared memory operation for System V shared
the calling
segment of detaching the shared memory segment from the address space of t
process. The
to-be-
1ne
The argument, shmaddr, is the address of shared memory segment to be detached.
must be the address returned by the shmat() system call.
detached segment
eck
and -1 in case of failure. To know the cause of failure, u
This call would return 0 on success
#include <sys/ipc.h>
#include <sys/shm. h>
segment. The
control operation for a System V shared memory
The above system call performs
to be passed
-
structure buf.
8:44 AM Shared Memory
the
a f t e r
The
struct sshmid
The third argument, buf, is a pointer to the shared memory structure nameda Struct hmid_ds.
cmd.
values of this structure would be used for either set or get as per
This call returns the value depending upon the passed command. Upon success of IP"
tor 0
and SHM_INFO or SHM_STAT returns the index or identifier of the shared memory segmel
for other operations and -1 in case of failure. To know the cause of failure, check with
variable or perror() function.
In the shared memory, the writing process, creates a shared memory of size 1K (and flags)
and attaches the shared memory
Performs reading and writing process for a few times for simplication and also in order
er to
avoid infinite loops and complicating the program
Following is the code for write process (Writing into Shared Memory File: shm_write.cl
*Filename: shm_write. c *7
#incLude<stdio.h>
#incLude <sys/ipc.h>
#inc Lude <sys/shm . h>
Shared Memory
shared memory is
Inter Process Communication throughcommon a c o n . cept ere
Ss can b e
message queue.
data
The client reads the data from the IPC channel,again requiring the
buffer.
be copied from kernel's IPC buffer to the client's
buffer.
Finally the data is copied from the client's
read and 2 write). So, shared
A total of four copies of data are required (2
two or more processes share a memory
memory provides a way by letting twice from input 1
Shared Memory the data is only copied
-
segment. With
the output file.
and from shared memory to
into shared memory
USED ARE:
SYSTEM CALLS
a unique key.
ftok): is use to generate size_tsize, intshmflg); upon successful completion, shmget)
shmget(): int shmget(key_t,
the shared memory segment.
identifier for have to attach yourself
returns an shared memory segment, you
can use a
shmat(): Before you void "shmaddr,int shmfig);
shmat(). void "shmat(int shmid, address to use but we should
set
to it using id. shmaddr specities speciic
memory
shmid is shared choose the address.
will automatically should
and OS segment, your program
it to zero the shared memory
with
shmdt): When you're done int shmdt{void "shmaddr);
it using shmdtO). is not destroyed. So, to destroy
detach itself from shared memory, it
detach from
shmctl): when you shmid, IPC_RMID,
NULL);
is used. shmctl(int
shmctl()
PROCESS
MEMORY FOR WRITER
SHARED
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm. h>
#include <stdio.h>
int main ()
ftok to generate
un.ique key
/
ftok ( "shmfile ", 65) ;
key_t key
=
an
identifier in shmid
returns
shmget ;
int shmid =
shmget (key, 1024,0666|
IPC_CREAT)
return 0;
#include <iostream>
#include <sys/ipc. h>
#include <sys/shm. h>
#include <stdio.hn>
using namespace std;
int main ()
shmdt(str);
// destroy the shared memory
shmctl (shmid, IPC_RMID, NULL);
return 0;
Output: a n d r e s @ a n d r e s "jPrograms/os
andres@andres: -/programs/os
andresgandres:-/Prograns/0ss