IPC with Shared Memory between two processes

Hi I would like to send bunch of data from process A to process B
through shared memory. Also I would like it so that when Process A finishes writting to the share memory, only then process B should read it.
How can I make this work. I have tried putting together a simple program where process A writes to share memory. and terminates. Then I exe process B to read from that share memory but it doesn’t work.

here is my code:

process A:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
int fd,i;
unsigned *addr;

fd=shm_open("/bolts",O_RDWR |O_CREAT,0777);
if(fd==-1)
{
	fprintf(stderr,"open failed:%s\n",strerror(errno));
	return EXIT_FAILURE;
}
//set the memory object's size
if(ftruncate(fd,100)==-1)
{
	printf("error in ftruncate");
	exit(1);
}
addr=mmap(0,100,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
if (addr==MAP_FAILED)
{
	printf("mmap failed\n");
	exit(1);
}
//write to shared memory
for (i=1;i<2;i++)
{
*addr=i;
printf("%d\n",i);
}
return EXIT_SUCCESS;

}

process B:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
int fd;
//int i;
unsigned *ptr;
if(fd=open("/dev/shmem/bolts",O_RDWR,0) == -1)
{
printf(“error opening bolts\n”);
exit(1);
}
ptr=mmap(0,100,PROT_READ,MAP_SHARED,fd,0);
if(ptr=MAP_FAILED)
{
printf(“failed to mmap\n”);
exit(1);
}
// trying to read just first data from the shared memory
printf("%x\n",ptr[0]);

close(fd);
munmap(ptr,100);
shm_unlink("/bolts");
printf("success\n");

return EXIT_SUCCESS;

}

The other program should use shm_open as well (i’m surprise you are not getting an error with mmap )

oh, that was a mistake an my part. I did change the process B code to use shm_open but I still fail on mmap.
how can I make this code work? I think something is with size parameter in mmap in both process, and reading the share memory in process B. Thanks

What is errno when mmap fails.

error number is 48. Which I think means “not supported”

here is my fixed code which still doesn’t work.

process A:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
int fd;
int i;
int *addr;

fd=shm_open("/bolts",O_RDWR |O_CREAT,0777);
if(fd==-1)
{
	fprintf(stderr,"open failed:%s\n",strerror(errno));
	return EXIT_FAILURE;
}
printf("%d\n",fd);
//set the memory object's size
if(ftruncate(fd,1024)==-1)
{
	printf("error in ftruncate");
	exit(1);
}
addr=mmap(0,sizeof(int),PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
if (addr==MAP_FAILED)
{
	printf("mmap failed\n");
	exit(1);
}
//write to shared memory
for (i=1;i<1024;i++)
{
addr[i]=i;
//printf("%d\n",addr[i]);
}
return EXIT_SUCCESS;

}

process B:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
int fd;
int *addr;
if(fd=shm_open("/bolts",O_RDONLY,0) == -1)
{
printf(“error opening bolts\n”);
exit(1);
}
addr=mmap(NULL,sizeof(int),PROT_READ,MAP_SHARED,fd,0);
if (addr==MAP_FAILED)
{
printf(“mmap failed: %d\n”,errno);
exit(1);
}

printf("%d\n",addr[0]);

close(fd);
munmap(addr,1024);
shm_unlink("/bolts");
printf("success\n");

return EXIT_SUCCESS;

}

Ok I figure it out, I think I will tease you on this one, hihi. It’s C issues (has nothing to do with QNX or share memory).

On process A check your pointer and array index.

On process B turn on compiler warning to max, and you should be able to figure it out.

I can see that I have created a 1024 byte of shared memory and used mmap of size int which is 4byte. So should have index it upto
256 instead of 1024. But even with turning on the warning level to 9 I didn’t see any compile problem with either code. and when I run the process B, it still fails at mmap with error number 48.

Ok I’ll help you with one cause it is kind of tricky

ff(fd=shm_open("/bolts",O_RDONLY,0) == -1)

This doesn’t work because == has precedence over = so fd will be equal to the result of shm_open() == -1 which is false so fd will be equal to 0. You need

ff( (fd=shm_open("/bolts",O_RDONLY,0)) == -1)

As for the other problem it’s so obvious that I will not do it for you…

ok I got it now~ thanks~~