This is a little urgent. I have to do use shared memory segments. I wrote this programme. I have tried everything, as you can see (void *, int * printing and so on), but havent been able to debug the error.
I will be grateful if some one can point out the bug.
Code:
#include<stdio.h>
#include<sys/ipc.h>
#define KEY 600
#define SIZE 20
int main()
{
int sid;
int i;
void *a;
int *shm;
if(fork()>0)
{
printf("%d\n",getpid());
sleep(1);
if(sid=shmget(KEY,SIZE,0)<0)
{
printf("Client Error\n");
exit(1);
}
a=shmat(sid,0,0);
shm=(int*)a;
shm++;
printf("Client Attach\n");
i=*shm;
printf("Read");
printf("%d",i);
printf("Client Release");
shmdt(a)
//shmctl(sid,IPC_RMID,(struct shmid_ds*)0);
}
else
{
if((sid=shmget(KEY,SIZE,IPC_CREAT|0666))<0)
{
printf("Error");
exit(1);
}
printf("Got\n");
a=shmat(sid,0,0);
shm=(int*)a;
printf("Attached\n");
shm++;
*shm=10;
printf("Value put, waiting for %d\n",getppid());
while(wait((int *)0)!=getppid());
printf("got back");
//shmdt(shm);
printf("Released\n");
shmctl(sid,IPC_RMID,(struct shmid_ds*)0);
}
return 0;
}
Please post any thing you think may be causing this error.
It prints till 'Value put, waiting for ...' in the child and then
Client Attach
this is followed by a Segmentation Fault message. It simply says 'Segmentation Fault'.