Here's my problem, I am getting a bad access on a shared memory line on my producer/consumer program:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffff
0x00001db0 in main () at Project3.c:120
120     memset(buf, 0, BUFFER_SIZE);
int BUFFER_SIZE = 200;
int *buf;

[I]Some code[/I]......

int ShmID=shmget(IPC_PRIVATE, (sizeof(int)*200), 0666 | IPC_CREAT); 
buf = shmat(ShmID, 0,  0666);
memset(buf, 0, BUFFER_SIZE);
union semun valsem;
valsem.val = BUFFER_SIZE;
pid_t childpid;
int SemID;
int waiter;

With regular allocaiton, it works well, but with shared memory alocation, it fails.

What could be the problem? Please and thank you!

Recommended Answers

All 3 Replies

Always test the return values of the system calls. In this case, is ShmID valid? What is errno after shmget? What is errno after shmat?

Okay I found out that shared memory was too much for the computer and so I had to restart it everything went well. But now it seems like I'm having a hanging condition with my semaphores

struct sembuf WaitEmpty={SEM_EMPTY, -1, SEM_UNDO}; 
struct sembuf SignalEmpty={SEM_EMPTY, 1, IPC_NOWAIT}; 
struct sembuf WaitFull={SEM_FULL, -1, SEM_UNDO}; 
struct sembuf SignalFull={SEM_FULL, 1, IPC_NOWAIT};

[I]some code[/I].......
void putItemIntoBuffer(int SemID) { //Puts item into buffer
	int item;
	for(item = 0; item <199; item++) {
	semop(SemID, &WaitEmpty, 1); 
	buf[item] = 'i'; //1 means the item's in the buffer 2 means its being consumed, and 0 means that it's empty
		printf("%c \n", buf[item]);
	semop(SemID, &SignalFull, 1);
void removeItemFromBuffer(int SemID) { //Removes item from buffer
	int item;
	for (item = 0; item< 199; item++){
	semop(SemID, &WaitFull, 1);
	buf[item] = 'e';
	semop(SemID, &SignalEmpty, 1);

What would be the problem here. I'm supposed to consume every time I produce. I feel that it's the wait and signal in the loop that are hindering the process.

I've isolated the factor to the following line:

semop(SemID, &WaitEmpty, 1);

Removing it allows a smooth code but the items are not changed. What could be the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.