Hello and welcome.
In this video session we will discuss on the Posix shared memory.
In this diagram, we explain the shared memory.
So here.
On the left hand side, let's consider the process one virtual memory and let's say the virtual memory
size is four GB.
And on the right right hand side, we have process two virtual memory and the size of the virtual memory
is four GB.
Now.
We have the physical memory, which is nothing but the Ram.
So a shared memory is nothing but the portion of the ram.
And this portion of the Ram is mapped onto the different process.
So in this case, this portion of the.
Memory of the Ram is mapped on to process one and the same memory of the Ram is mapped to the process.
Two.
So now any read or write performed on process?
One virtual memory in this area is in fact written to the shared memory on the physical ram.
In the same way when process two performs any memory operations on this mapped.
Region, then the actual read and write is performed on the physical memory.
That is nothing but the shared memory.
So in this way, both the process.
That is process one and process two can exchange the data via the shared memory.
So shared memory is nothing but the common memory that can be shared among the different process.
In order to perform the inter-process communication.
So now, in order to create a shared memory, we have to follow the below steps.
So first we have to create the piece of memory on the ram that is nothing but the shared memory and
the shared memory.
Usually when we create it is of zero byte.
Then after that we need to define the size of the shared memory.
That is, we have to define the length of the shared memory and then map this shared memory onto the
required process.
So here in this case, we are mapping this shared memory onto the virtual memory of process one and
similarly mapping this shared memory on the virtual memory of process.
Two.
Once the shared memory is mapped on the different process, read and write onto the corresponding process,
Virtual memory will lead into IPC between the different process.
Now let's see the different programming system calls in order to achieve the shared memory.
So as defined, shared memory allows to share a mapped memory region between unrelated processes.
So in this case, shared memory is very much suitable even to use between two processes which are not
related to each other and the shared memory.
When a shared memory is created, there is an entry in slash dev slash slash sm.
We will see this when considering the example.
Also, the shared memory is considered as the fastest IPC mechanism when compared to other shared memory
such as message queues, pipes and Fifo.
This is because there is no extra kernel data that is involved also in other IPC.
If a data needs to be sent from process one to process two, then there is two set of copies that is
performed.
The first set is from the process.
Userspace to the kernel space then.
A similar copy is performed from the.
Kernel space to the target process user space.
So here.
In other ipcs two sets of copies are performed, whereas in the shared memory the data is written onto
the common shared memory.
And this shared memory can be read or written from the different process.
Thus the shade mercury is the fastest.
When compared to any other IPCC.
Now let's see the different functions involved in creating a shared memory.
As a first step, assume underscore open is used to create a new shared memory or to open a existing
shared memory.
This is similar to the open system call that we perform in order to open the regular file.
Now.
The system Open returns a file descriptor that is referring to the shared memory.
As we have.
Indicated before that a shared memory that is newly created is of zero bytes in length.
The since the length of the shared memory is zero bytes, we need to define the length of the shared
memory.
And then finally, as a step three, we need to map the shared memory to the different process using
the map function.
So as we can see that any read or write to the process, virtual memory will be actually read or written
from the shared memory.
So here, whenever any process performs, read or write operation on the mapped memory of the virtual
space of the corresponding process, it actually performs the read write directly on the shared memory.
This the other process can directly utilize the shared memory.
So the syntax to create a shared memories system underscore open and we need to pass the name of the
shared memory and oflag and mode.
This is similar to the open system call of a regular file and if open is.
Success.
It returns a file descriptor in case open fails, it returns minus one.
So here oflag can take different arguments.
Such as.
Read.
Only read.
Write.
This is similar to the parameters that we defined for a regular open system call.
And if we have to create a new shared memory, then option is provided so these parameters can be added
while creating.
Now let's see how to set the size of the shared memory.
As we have mentioned before, the shared memory, when it is newly, newly created, the size is zero.
So the user needs to assign the required size to the shared memory and this is done using if truncate.
Function.
So in order to assign the memory, we need to pass the file descriptor that is obtained by Open.
The system open returns the file descriptor.
So this file descriptor that is returned has to be passed.
As the first parameter.
And second parameter will be the required length of the shared memory.
The example of if truncate we will be seeing.
Finally, let's see how to map the shared memory on the individual process.
So here the syntax to map the shared memory onto the individual process is as follows.
We need to include the required header files, and map is the function that maps the shared memory onto
the process.
Virtual memory and the first parameter addr is nothing but the.
Required process, virtual memory on which it needs to be mapped.
Usually this address, if we do not have any specific virtual memory of the process, this is passed
as null so that the system can take a appropriate.
Memory of the virtual space and length.
Length is nothing but the length of the created shared memory, and prot is the protection of memory.
And usually we have prot read for read protection or prot write for write protection and.
In case of a shared memory.
The flags we need to use as map underscore shared so that the shared memory can be used by different
process or the data that is written onto the shared memory can be accessed by different process.
So in that case we have to mention flags.
The value of flags as map underscore shared and.
And if this is nothing but the file descriptor that is obtained from M underscore open.
So we will be seeing the example how to use all the three functions that is open f truncate and m map.
So.
The iMap on success.
It.
It returns the virtual memory of the process to which the shared memory is mapped.
So here, when iMap is success, it returns the base address of the virtual.
Memory of the process.
So we have created a virtual we have created a shared memory and using map, we have mapped the shared
memory onto the process, virtual memory.
And if a map is success, it returns the base address of the virtual map.
And if a map fails, then it returns.
Map failed.
That is nothing but minus one which is typecast to void star and the corresponding error number is set.
And we can also see more information of a map using man space to space map on the terminal.
Now, in the next session, we will see the example program, how to use the Posix shared memory.