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
    Have you debuged it ? what line exactly fails ? – giorashc Dec 17 '12 at 11:24
  • I think it is the strcpy line that fails, at least every thing I try with adding my data to the shared memory fails – gudnylara7 Dec 17 '12 at 11:33
  • Have you noticed that char (*d)[1025] is a pointer to an array of 1025 chars (did you meant char d[1025] which is an array of 1025 chars which is already allocated). As @benjarobin mentioned your data is not allocated – giorashc Dec 17 '12 at 11:38
  • Well if I change it to just char d[1025] I can't use it to attach to my shared memory – gudnylara7 Dec 17 '12 at 11:46
  • I fail to comprehend, why you read a line from the file and then write it back to the same file?! – Hristo Iliev Dec 17 '12 at 16:41
  • was just a mistake, didn't realize i was doing it. But if you check the answer by benjarobin it shows what i was trying to accomplish – gudnylara7 Dec 17 '12 at 18:03

2 Answers2

0
  1. File is opened in read mode only.

change it to rw+ which will open a file in read/write mode. If the file is not available, it will be created.

fputs(data, file); here file is opened in read-only mode but write is happening.

But Im not sure, why you trying to write the read data to same file. You should consider your design first.

2. char (*d)[1025]; is a pointer to an char array of size 1025. You are using it in strcpy().

3. memory should be allocated for data either statically or dynamically.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • I am trying to add the file from argv[3] into my shared memory and thought it would be good to use fputs. what other way is better? – gudnylara7 Dec 17 '12 at 11:32
  • Problem in `while(fgets(data, 1025, file)!=NULL)`. Bcos memory for data is not allocated as @benjarobin stated. – Jeyaram Dec 17 '12 at 11:34
  • But is it possible to put this somehow in the shared memory? If I just use puts(data) then my data gets printed out so I thought it worked – gudnylara7 Dec 17 '12 at 11:38
  • For number 1. How do I change it to write mode as well? 2. In strcpy I'm trying to move my array to the shared memory. d is the pointer that points to my shared memory and data is supposed to be the data to be added to the shared memory – gudnylara7 Dec 17 '12 at 11:43
  • For number 1, I was searching for another way and saw this here http://stackoverflow.com/questions/8147389/how-to-read-multiple-lines-of-input-from-user-using-fgets-and-write-it-into-a-fi don't understand why my way is wrong as it is almost the same Ooops just realized the problem now :) but still get segmentation fault – gudnylara7 Dec 17 '12 at 12:03
0

+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;
}
benjarobin
  • 4,410
  • 27
  • 21
  • My shared memory should hold 1024 letters, so I thought my data could have 1024 letters as well. Like I said I have tried a lot of ways to get my data into the shared memory without luck – gudnylara7 Dec 17 '12 at 11:35
  • Add example, tested : It compiles and run – benjarobin Dec 17 '12 at 13:18
  • Thanks! But how can you check if the data is in the memory segment? – gudnylara7 Dec 17 '12 at 13:28
  • Well I already did and also figured it out, no reason for answering like this. But your example helped me get rid of the things I didn't need and figure everything out so thanks. – gudnylara7 Dec 17 '12 at 17:02