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;
}