Hello and welcome.
In this video session, we will demonstrate the usage of shared memory.
In the first program, we are going to write the data onto the shared memory.
And in the second program we are going to read the data that is written onto the shared memory.
First, let's see the program to write to the shared memory.
Here we include the required header files and we begin the program from main function.
And we define the required variables and underscore open is used to open the.
Shared memory that is specified in the first parameter and if the shared memory already exists, it.
It returns the file descriptor of the shared memory, or if the shared memory does not exist by specifying
o underscore create.
We are going to create the new shared memory in the mode 660.
This is nothing but read write mode for user read write mode for the group and no permission for others.
And if a system underscore open is failed, it returns a minus one.
And now the second step would be to define the length of the shared memory.
So in this program, the command line argument parameter is used.
As the data.
So erg v of one is used as the data that is being returned to the shared memory.
So we get the length of the data that is passed from command line argument.
And if truncate function is used to set the length of the shared memory and we have to pass the required
length and specify the file descriptor onto which the length is set for the shared memory.
And as a last last step, we have to map the created shared memory to the process.
Virtual memory.
This is done by calling the function map and the first parameter is null and the second parameter is
nothing but the length of the shared memory.
And the third parameter is the protection bits.
That is nothing.
But we are providing a read and write protection and following that the argument is map underscore shared.
This means we are using the shared memory for.
Map shared purpose that is nothing but the shared memory data on the shared memory is visible to.
Different all other processes and we have to specify the file descriptor and the offset is zero.
And if map failed, it returns.
A map underscore failed and upon success a map returns the.
Virtual memory address of the process.
And finally, we copy the data that is passed from the command line argument to the.
My memory.
So contains the mapped virtual map.
Memory location and any read or write onto this mapped memory.
The data is.
Return on to the shared memory.
So as we have discussed in this video slide, in this slide, that any read or write to the process,
virtual memory is written onto the shared memory.
So here what we have done is we have first created a created a shared memory.
Using this system open and specified the name of the shared memory.
Then define the length of the shared memory using.
If truncate.
We have defined the length of the shared memory, then mapped the shared memory onto the process using
map.
And using a map.
We have mapped the shared memory onto the process.
Virtual memory.
Now, any operation on the process, virtual memory, in fact, writes the data onto the shared memory.
Now let's compile this program.
So the name of the program and we need to specify minus LRT and.
The compilation is successful and the object file created is.
Underscore.
Right.
Now let's.
Go to the read function before executing the write program.
Now let's open the read.
Program.
So in this program we include the required header files.
So this program is used to open the shared memory that is, and read the data from the shared memory.
So in this case, we open the shared memory and specify the name of the shared memory.
And this name is same as the name that is passed here.
So we have written the data onto the shared memory by name underscore one.
So while reading also we need to open the same shared memory.
And we are.
Opening the shared memory in read only mode and if underscore open fails, it returns a minus one and
we print the same and we return.
So now we can get the size of the shared memory using if stat and and by passing the file descriptor
to if stat we get the length of the shared memory.
And finally we map the shared memory that is obtained from underscore open onto map.
So by mapping onto the process virtual memory, we get the.
Map to the shared memory.
So we have to pass Null as a first parameter, followed by the size of the shared memory and the protection
to the memory is read.
And this we have to specify as map underscore shared because we are using the shared memory.
And if D is nothing but the file descriptor obtained from open and the offset of zero.
And if a map failed, then we print the same and we return.
And if a map is successful, we get the virtual memory of the process.
And now any operation on this address.
We are, actually.
We are actually performing.
We are.
We are actually performing the read and write of the shared memory.
So now what we have done is we have opened the shared memory, we have opened the shared memory in process
two, and then we have mapped the shared memory onto the process, virtual virtual memory.
And now we have got the hand handle of this shared memory as a mapped onto the virtual memory.
And now any read write operation performed on this memory is in fact getting the data from the shared
memory.
So finally we print.
But by printing, we do a read operation on the addr.
That is, that is nothing but the virtual memory address of the process.
So now let's compile this program to understand.
So we have compiled the program.
So in order to compile the program of shared memory, we have to include minus LRT, we have to specify
minus LRT, and the underscore read is the object file of this program.
So now we have.
The system read and system underscore right as the object files of read and write.
So now let's first write some data.
Using a system, Right?
And here, let's specify the data.
So now.
We have written the data.
The data is nothing but hello underscore process to on to the shared memory.
Now let's run the underscore read program.
Once we execute the system, underscore read, we are reading the data from the shared memory.
So now we can see that the data that is written.
From the system underscore write program.
The same data is being read using.
Underscore read.
So using the concept of shared memory, the data is written from one process and read from the other
process.
So this this is an example of shared memory and shared memory is one of the shared memory is the fastest
method of IPC.