Return to BSD News archive
Newsgroups: comp.unix.bsd
Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!saimiri.primate.wisc.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!gatech!udel!princeton!phoenix.Princeton.EDU!sgovil
From: sgovil@phoenix.Princeton.EDU (Shivani Govil)
Subject: Shared Memory
Message-ID: <1993Apr5.212124.10951@Princeton.EDU>
Originator: news@nimaster
Sender: news@Princeton.EDU (USENET News System)
Nntp-Posting-Host: phoenix.princeton.edu
Organization: Princeton University
Date: Mon, 5 Apr 1993 21:21:24 GMT
Lines: 165
HI Netters,
I have some problem with SHARED MEMORY.
I have a header file, server program and a client program.
I had a funny experience with these programs.
I have a simple structure defined in the header file and
I am trying to get the shared memory segment in server
program by shmget command, then I attach it by shmat.
I am simply writing some stuff in the segment.
The client program also tries to get the segment, attach it and
print whatever server has written in it.
The progarm worked fine, I changed it for multiple shared
memory segments, it worked fine ...
but suddenly the same program failed to get the segment!
I cleared /tmp and /var/tmp. It worked for once or twice
and then again it failed...
So, looks like it is not getting enough memory and somewhere
I am not releasing the segment probably which makes it
unavailable next time... but I don't know how to clear
that!
I would really really appreciate if I get some guidance and
help from some who have worked on shared memory before..
I have the program here...
HEADER FILE
mesg.h
#define MAXMESGDATA (4096-16) /* we don't want sizeof (Mesg) >4096*/
#define MESGHDRSIZE (sizeof(Mesg) - MAXMESGDATA)
#define NBUFF 4 /* number of shared memory segments */
#define SHMKEY ((key_t) 7890) /* base value for shmem key */
#define PERMS 0666
typedef struct {
int mesg_len;
int mesg_type;
char mesg_data[MAXMESGDATA];
}Mesg;
--------------------------------------------------------------------
SERVER FILE
server.c
/* server.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include "mesg.h"
int shmid;
Mesg *mesgptr;
main()
{
register int i;
/* get the shared memory segment and attach them */
if((shmid = shmget(SHMKEY , sizeof(Mesg), PERMS|IPC_CREAT)) <0){
fprintf(stderr,"shmid =%d\n",shmid);/* IPC_CREAT =1000 */
err_sys("server: can't get shared memory ");
exit(-1);
}
if((mesgptr = (Mesg *)shmat (shmid, (char *) 0,0)) == (Mesg *)-1){
err_sys("server: can't attach shared memory");
exit(-1);
}
server();
if (shmdt(mesgptr) <0)
err_sys("server:can't detach shared memory ");
exit(0);
}
server()
{
mesgptr->mesg_len=6;
mesgptr->mesg_type=1;
strcpy(mesgptr->mesg_data,"testok");
}
--------------------------------------------------------------------
CLIENT PROGRAM
client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "mesg.h"
int shmid;
Mesg *mesgptr;
main()
{
register int i;
if((shmid=shmget(SHMKEY , sizeof(Mesg),0))<0){
err_sys("client: can't get shared memory ");
exit(-1);
}
if((mesgptr=(Mesg*)shmat(shmid,(char*)0,0))==(Mesg*)-1){
err_sys("client: can't attach shared memory ");
exit(-1);
}
client();
if(shmdt(mesgptr)<0){
fprintf(stderr,"shmdt err\n");
exit(-1);
}
if(shmctl(shmid,IPC_RMID,(struct shmid_ds *) 0)<0){
printf("shmctl err");
exit(-1);
}
exit(0);
}
client()
{
printf("mesg= %s",mesgptr->mesg_data);
}
err_sys is a routine whcih prints the error message.
I will really appreciate if I get some help form any of you.
Thank you very much in advance
Pallavi
Please mail me at
pallavi@psyche.sarnoff.com