Following is the one of code written by me for shared memory. Problem is that program donot come outside from marked loop and read numbers infinitely.
"common.h"
struct common
{
int a[10],b[10],c[10],flag;
};
p1.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/shm.h>
#include"common.h"
int main()
{
int running=1;
void *shm=(void *)0;
struct common *shared;
int shmid;
shmid=shmget((key_t)1234,sizeof(struct common),0666|IPC_CREAT);
if(shmid==-1)
{
printf("shmget failed");
exit(EXIT_FAILURE);
}
shared=shmat(shmid,(void *)0,0);
if(shared==(void *)-1)
{
printf("shmat failed");
exit(EXIT_FAILURE);
}
printf("Memory attached at %x\n",(int)shared);
shared->flag=0;
while(running)
{
printf("%d",shared->flag);
if(shared->flag==0)
{
int i;
printf("Enter the elements of first array on behalf of p1\n");
for(i=0;i<10;i++)
{
//printf("%d\n",i);
scanf("%d",&(shared->a[i]));
fflush(stdin);
}
printf("yes");
shared->flag=1;
printf("%d",shared->flag);
}
else if(shared->flag==3)
{
int i,j;
printf("On behalf of p1 sorted array is \n");
for(i=0;i<20;i++)
{
printf("%d\n",shared->c[i]);
}
//shared->flag1=4;
running=0;
}
else
{
printf("Waiting for p2 and p3 to complete");
sleep(1);
}
}
shmdt(shm);
}
Can anyone tell me the problem here?