Reading and writing structure data to shared memory using POSIX

54 Views Asked by At

I am trying to read and write the structure to shared memory but the program is crashing with a permission issue and segmentation fault. write.CPP -> the movement I give the value it's getting stuck in loop and at last, it gives "struct failed: cannot allocate memory", segmentation fault

Write.cpp

#include<iostream>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<sys/file.h>
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<dirent.h>
#include<string.h>
#include"headerfile.h"
using namespace std;
int main()
{
        int fd;
        char *pg_addr;
        int size =10000;
        int mode = S_IRWXO|S_IRWXG|S_IRWXU;
        int i=0;
        while(1)
        {
                cout<<"\nenter 1 to read again and 2 to exit : ";
                cin>>i;
                if(i==2)
                        break;

                fd=shm_open("memory_exp1",O_RDONLY, 0666);
                if (fd == -1)
                perror("shm_open");
                struct shmbuf *shmp = (shmbuf*)mmap(NULL,sizeof(*shmp),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
                if(shmp==MAP_FAILED)
                        perror("MMap error");        
                cout <<"\n Read data is" << shmp->buf << "\n" << shmp->cnt;        
        }
        close(fd);
        return 0;
}

Read.CPP

#include<iostream>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<sys/file.h>
#include<unistd.h>
#include<sys/types.h>
#include<errno.h>
#include<dirent.h>
#include<string.h>
#include"headerfile.h"
using namespace std;
int main()
{
        cout<<"point1";
        int fd;
        char *pg_addr;
        int size =10000;
        int i=0;
        char strTemp[100]="";
         int mode = S_IRWXO|S_IRWXG|S_IRWXU;


    fd=shm_open("memory_exp1",O_CREAT,mode);
        while(1)
        {
                cout<<"\nEnter 2 to exit\nto continue press any other value :";
                cin >> i;
                if(i==2)
                        break;
                fd=shm_open("memory_exp1",O_RDWR, mode);
                if (fd == -1)
                        perror("shm_open");
                if (ftruncate(fd, size) == -1)
                        perror("ftruncate");

                struct shmbuf *shmp=(shmbuf*)mmap(NULL,sizeof(*shmp),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
                if(shmp == MAP_FAILED)
                        perror("Struct failed");       
                cout<<"\nEnter the value";
                cin>>strTemp;
                shmp->cnt=100;
                memcpy(&shmp->buf,strTemp,100);
        
                close(fd);
         }
        return 0;
}
           
1

There are 1 best solutions below

0
CoderRC On

When you run Write.cpp the size of the shared memory mapping is zero or the shared memory mapping is not created. So I recommend running first Read.cpp while running press any other value enter any value to be prompted "Enter the value" and write your value. Doing what I wrote in the previous sentence works because when you do what the previous sentence says it extends the size of the shared memory mapping to make the mmap call to succeed. If you want to try your code in windows you can use my repo from https://github.com/CoderRC/libmingw32_extended .