Help about shared memory

Amit S. Joshi asjoshi at phoenix.Princeton.EDU
Mon Feb 15 18:00:20 AEST 1988


Hi,

I wanted to share some data between two processes. I looked up the
manual and found something called "shared memory". Could somebody tell
me waht that is and how do I use it ?

I naively tried something like this:

typdef struct { int flag; char buf[80];} mydat;

main() {
	mydat *dat;
	int sid,pid;

	if ((sid = shmget(IPC_PRIVATE,sizeof(mydat),IPC_CREAT)) < 0) {
		perror("no shared memory");
		exit(1);
	}
	dat = (mydat *)shmat(sid,0,0);
	sprintf(dat->buf,"this is a test");
	dat->flag = 1;
	
	if ((pid = fork()) < 0) {
		perror("no fork");
		exit(1);
	}
	
	if (pid == 0) {
		printf("%s\n",dat->buf);
	} else {
		wait(0);
		printf("%s\n",dat->buf);
	}
}

I simply got a core dump and with dbx I found that the data->buf "was
a nil pointer" or some such thing, and sprintf caused the crash due to a
invalid memory access.

Could somebody help me ??

Thanks,

-- 
Amit Joshi	BITNET	|	Q3696 at PUCC.BITNET
		USENET	| {seismo, rutgers}\!princeton\!phoenix\!asjoshi
"There's a pleasure in being mad... which none but madmen know!" - St.Dryden



More information about the Comp.unix.wizards mailing list