Hi, this code is compiling successfully, but giving "segmentation fault" when I am trying to copy data to shared memory. Please tell me where I am wrong.
Thanks a lot............

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
int main()
{
        int id=-1, status=-1;
    char *check=NULL;
    char *p="you";

    const size_t ps= sysconf(_SC_PAGE_SIZE);

    id=shmget(111,ps*3,IPC_CREAT|06666);

    if(id<0 && errno !=EEXIST)
    {
        perror("error creating shared memory");
        exit(0);
    }
    check= shmat(id,0,0);

    printf("the attached address is %u\n",check);
 // strncpy(check,p,4);
    memcpy(check,p,4);
    printf("data at check is %s \n",check);
    printf("2 rechecking address of check  %u\n",check);

    status=fork();

    if(status==-1)
    {
        perror("error creating process\n");
    }
    else if(status==0)
    {
        printf("in child context\n");
        printf ("child says data at check is %s",*check);
    }
    else
    {
        printf("in parent context \n");
    }

return 0;

}

Recommended Answers

All 5 Replies

id=shmget(111,ps*3,IPC_CREAT|06666);

I haven't used this flag 06666 before, but when i tried with 666 option it worked. Unless otherwise you have special purpose using this flag you can switch to this ubiquitous mode (0666).

if(id<0 && errno !=EEXIST)

This condition will only pass when the id is -1 (error status provided by shmget) and the errno is not EEXIST. The shmget will not always set the errno to EEXIST, there are couple of other values populated by it. For more info see man page of shmget. So, your

perror("error creating shared memory")

won't work as error checking always at all conditions.

check= shmat(id,0,0);

Please do error checking for the return value of shmat. It may return -1 incase of failure which should be checked before doing memcpy. Any invalid address such as -1 will definately produce a Segmentation fault.

You can use this link for additional reference.

Hope it helps :)

Thanks rustysynate, issue resolved. Here is the working code.

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>

int main()
{
    int id=-1, status=-1;
    char *check=NULL;
    char *p="you";

    const size_t ps= sysconf(_SC_PAGE_SIZE);

    id=shmget(111,ps*3,IPC_CREAT|0644);

    if(id<0)
    {
        perror("error creating shared memory");
        exit(0);
    }
    check= shmat(id,(void *)0,0);

   if(check==(char *) -1)
   {
    perror("shmat\n");
    return 0;
   }

    printf("the attached address is %u\n",check);
 // strncpy(check,p,4);
    memcpy(check,p,4);
//printf("Enter some data\n");
//gets(check);

    printf("data at check is %s \n",check);
    printf("2 rechecking address of check  %u\n",check);

    status=fork();

    if(status==-1)
    {
        perror("error creating process\n");
    }
    else if(status==0)
    {
        printf("in child context\n");
        printf ("child says data at check is %s\n",check);
    }
    else
    {
        printf("in parent context \n");
    }

return 0;

}

Another version, child getting attached to shared memory :)

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>

int main()
{
    int id=-1, status=-1;
    char *check=NULL,*ch=NULL;
    char *p="you";

    const size_t ps= sysconf(_SC_PAGE_SIZE);

    id=shmget(111,ps*3,IPC_CREAT|0644);

    if(id<0)
    {
        perror("error creating shared memory");
        exit(0);
    }
    check= shmat(id,(void *)0,0);

   if(check==(char *) -1)
   {
    perror("shmat\n");
    return 0;
   }

    printf("the attached address is %u\n",check);
 // strncpy(check,p,4);
    memcpy(check,p,4);
//printf("Enter some data\n");
//gets(check);

    printf("data at check is %s \n",check);
    printf("2 rechecking address of check  %u\n",check);

    status=fork();

    if(status==-1)
    {
        perror("error creating process\n");
    }
    else if(status==0)
    {
        printf("in child context\n");
      //  printf ("child says data at check is %s\n",check);
    ch=shmat(id,(void *)0,0);
    if(ch==(char *)-1)
    {
        perror("child can't attach");
    }

    printf("child says %s\n",ch);
    }
    else
    {
        printf("in parent context \n");
    }

return 0;

}

Don't forget to close before your program is exiting. Otherwise, when you try to re-run there might be File exist error :)

Great, will detach it.
Thanks again.........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.