segmentation problem writing to shared memory segment

plz help out what would be problem in the following program? I am trying to write structure to shm in child process and i want read from parent...

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SIZE 5*1024
main()
{
struct databuf{
int nread;
char buf[20];
int xyz;
};
struct databuf* ptr;
int shmid,pid;
// int SIZE = sizeof(*ptr);
shmid = shmget((key_t)1,SIZE,IPC_CREAT|0777);
ptr = (struct databuf *)shmat(shmid,(char*)0,0);
pid = fork();
if(pid==0)
{
//ptr->nread=read(0,ptr->buf,SIZE);
printf("hi\n");
strcpy(ptr->buf, "murali");
ptr->xyz=20;
ptr->nread=10;
}
else
{
wait(0);
//write(1,ptr->buf,ptr->nread);
printf("%s\n",ptr->buf);
printf("%d\n",ptr->nread);
printf("%d\n",ptr->xyz);

}
return 0;
}
Last edited on
did you put a break and checked which line the segmentation is occurring ?

I did not get any problems when I run it.
Topic archived. No new replies allowed.