We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

shared memory program

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?

2
Contributors
3
Replies
19 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
niyasc
Light Poster
40 posts since Sep 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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
histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76
Solved Threads: 36
Skill Endorsements: 1

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

niyasc
Light Poster
40 posts since Sep 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

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

niyasc
Light Poster
40 posts since Sep 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by histrungalot

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0674 seconds using 2.71MB