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?

There is no problem with your code. You just need to wait for the output to come out. Or you could call fflush(stdout) after the printf("Waiting for p2 and p3 to complete"); or put newlines (\n) at the end of your print statements.
Other then that its working, it just looks like it is stuck.
With the fflush(stdout)

$ ./a.out
Memory attached at 35000
0Enter the elements of first array on behalf of p1
1
2
3
4
5
6
7
8
9
0
yes11Waiting for p2 and p3 to complete1Waiting for p2 and p3 to complete^C

k. Thanks Brohter. May the God bless you..

One more doubt. Why it is required to use fflush(stdout) there?

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.