Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:36:17+00:00 2026-06-15T23:36:17+00:00

I am trying to write a file with data into my shared memory segment.

  • 0

I am trying to write a file with data into my shared memory segment. However everything I have tried seems just to give the error Segmentation fault. I have been searching the internet for help for more then one day.

int main(int argc, char *argv[]).
{
    int sm;
    char *data;
    int pid=atoi(argv[1]);
    int key=atoi(argv[2]);
    char (*d)[1025];
    data=(char*) malloc(1025);


    //put the data in the shared memory segment
    FILE *file=fopen(argv[3], "r"); //r for read
    if (file==0)
    {printf("Could not open file");}
    else
    {
        while(fgets(data, 1025, file)!=NULL)
        {
            fputs(data, file);
        //  puts(d);
        }       
        fclose(file);
    }

    //access shared memory 
    //S_IWUSR gives owner the write permession
    sm = shmget(key, 1024, S_IWUSR); 
    //create a pointer to the shared memory segment
    d =  shmat(sm, (void *)0, 0); //shared memory id, shmaddr, shmflg
    //for (int j=0; j<100; j++)
        strcpy(d[0], data);

    shmdt(d); //detach the shared memory segment
    //remove the shared memory segment
    shmctl(sm, IPC_RMID, NULL);
}

Any help would be greatly appreciated
Thanks in advance

EDIT: added malloc

EDIT2: maybe I should rephrase my question, my problem is to get the data into my shared memory

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:36:18+00:00Added an answer on June 15, 2026 at 11:36 pm

    +1 about rjayavrp answer

    And I can add that data is not allocated… This is just a pointer and not a array of chars..

    Moreover what are you trying to do with

    char (*d)[1025];
    

    A quick and dirty example :

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    
    #define SIZE_SHARED_MEMORY  1024
    
    int main(int argc, char *argv[])
    {
        int sm = -1;
        char *d = NULL;
        FILE *file = NULL;
        key_t key = 0;
        int ret = 0;
    
        if (argc != 4)
        {
            return 1;
        }
    
        key = atoi(argv[2]);
    
        //access shared memory
        sm = shmget(key, SIZE_SHARED_MEMORY, IPC_CREAT | 0600);
        if (sm == -1)
        {
            perror("shmget : Failed");
            return 2;
        }
    
        //create a pointer to the shared memory segment
        d =  shmat(sm, (char *)0, 0);
        if (d == (void *)-1)
        {
            perror("shmat : Failed");
            return 3;
        }
    
        // Open the file
        file = fopen(argv[3], "r");
        if (file == NULL)
        {
            perror("fopen : Failed");
            ret  = 4;
        }
        else
        {
            if(fgets(d, SIZE_SHARED_MEMORY, file) == NULL)
            {
                perror("fgets : Failed");
                ret = 5;
            }
            fclose(file);
        }
    
        shmdt(d); //detach the shared memory segment
    
        // remove the shared memory segment ???
        // Don't understand why you are doing this
        shmctl(sm, IPC_RMID, NULL);
    
       return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write data into the file in binary form. I was trying
I am trying to write a lot of data into a binary file. Because
I'm trying to write an xml file with utf-8 encoded data using ElementTree like
I am trying to write my spatial data from a table to a file.
I am trying to write data stored in a binary file using turbo C++.
I'm trying to write a Java app that imports a data file. The process
I'm trying to write a rake task for loading data into my database. My
I am trying to write a function that will insert data into a MySQL
I have downloaded the Layar401.apk file from web and trying to install it into
I am trying to write into a file and if the file doesn't exist

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.