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?

Dani AI

Generated

A few focused clarifications and fixes that make the posted program behave as expected.

As noted, the visible “stuck” behaviour is usually due to stdio buffering: many of the prints in the loop have no trailing newline, so the text sits in the stdout buffer instead of appearing on the terminal. On interactive ttys stdout is typically line‑buffered (flushes on newline); on pipes or files it is block‑buffered. Either include \n in the format strings, call fflush(stdout) after important prints, or disable buffering with setvbuf/stdbuf while debugging.

The fflush(stdin) call inside the input loop is undefined behaviour in ISO C and should be removed. Input leftovers should be consumed explicitly and scanf return values must be checked to avoid infinite loops when parsing fails. Example pattern:

if (scanf("%d", &shared->a[i]) != 1) { /* handle error or break */ }
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { }

A few other practical corrections and cautions: the pointer returned by shmat should be passed to shmdt (the posted code uses an unrelated shm variable). Print addresses with %p and cast to (void *) for portability. Check every IPC/system call return code. The single int flag is a fragile sync primitive; declaring it volatile only prevents some compiler caching but does not provide atomicity or ordering across processes. For reliable synchronization, use POSIX or SysV semaphores (or other proper IPC synchronization) rather than a bare integer flag.

Quick checklist for : add newlines or fflush(stdout) to see output, remove fflush(stdin) and validate scanf, pass the correct pointer to shmdt, use %p for addresses, and move to semaphores for real synchronization. Running with strace or stdbuf -oL can help diagnose remaining surprises.

Recommended Answers

All 3 Replies

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.