From: magrasjc@texas.net
Subject: %!@# Help Posix semaphores & mmap
Date: 1997/06/19 [thread overview]
Message-ID: <33A971AB.3029@texas.net> (raw)
This is a multi-part message in MIME format.
--------------29CD3BA36A78
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Fellow unixheads,
Has anyone got this stuff to work?
I am trying to use POSIX sem_init(mappedfilePointer,1,1) to initialize
semaphores that are usable amongst multiple processes. SYR4 too ugly!
Thanks to the guy who sent me the code with the MAP_SEMAPHORE in the
code. It isnt in DEC UNIX's man pages so I didnt think to use it.
So What is it for? It seems to have no effect.
The Server end will mmap() with an address of 0. The clients will mmap
to an address passed to them and or'd MAP_FIXED. These are 2 different
processes and the children processes are complaining that the sem_init()
semaphores in mmap()d files space are bad. msync()? what is its role
in this? The server sem_init calls return 0.. no problem.
Bibliography Bill O Gallmeister,
"Posix.4:Programming for the Real World", O'Reilly & Associates
Thanks in advance for your help.
Practically clueless,
J.C. Magras
For the attached files ...
g++ -c -g -D_XOPEN_SOURCE -D_OSF_SOURCE -D_POSIX_4SOURCE -DDEBUG
-D_DECUNIX_3_2 // the build platform Solaris 2.5.1 has no POSIX sem's
--------------29CD3BA36A78
Content-Type: text/plain; charset=us-ascii; name="jMsg.C"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="jMsg.C"
//#include <iomanip.h>
#include "jMsg.h"
#include <strstream.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <ctime>
#include <sys/time.h>
#include <sys/stat.h>
#ifdef _POSIX_SEMAPHORES
#include <semaphore.h>
#endif
#include <fcntl.h>
#include <strstream.h>
#pragma implementation
extern "C" {
int initmap(int);
};
jMsg::jMsg(int key,
unsigned long int flags=_JWARE_ALLOC,
#ifndef _POSIX_SEMAPHORES
unsigned long size=512)
#else
unsigned long size=512)
#endif
{
char tmp_path[]="/tmp/",space[16];
char *testP,*SemDirectory=tmp_path;
int map_fd;
unsigned int fileperms;
bstring mapfil_bstr,lockfil_bstr;
// flock_t otherkey_lck={F_RDLCK,0,0,1};
this->flags = flags;
this->myType = key;
this->lockfileP = 0;
this->mapfileP = 0;
this->size = size;
if (testP= getenv("FLAGDIR")) SemDirectory=testP;
if ((this->flags & _JWARE_CREAT) == _JWARE_CREAT) {
fileperms=O_CREAT|O_RDWR ;
} else if ((this->flags & _JWARE_ALLOC) == _JWARE_ALLOC) {
fileperms=O_RDWR;
}
#ifndef _POSIX_SEMAPHORES
lockfileP = new strstream(space,ios::in|ios::out);
lockfil_bstr = SemDirectory;
*lockfileP << key ;
cout << lockfil_bstr.data() << endl;
lockfil_bstr += "LOCK," ;
cout << lockfil_bstr.data() << endl;
lockfil_bstr += lockfileP->str() ;
lockfil_bstr[13]=0;
cout << lockfil_bstr.data() << endl;
lock_fd = open (lockfil_bstr.data(),fileperms,0600);
if (lock_fd < 0) goto msgerrno_xit;
/// unlink(lockfil_bstr.data());
#define SEM_SPACE 0
#else
#define SEM_SPACE (3*sizeof(sem_t))
#endif
mapfileP = new strstream(space,ios::in|ios::out);
mapfil_bstr= SemDirectory;
*mapfileP << key ;
cout << mapfil_bstr.data() << endl;
mapfil_bstr += "MMAP," ;
cout << mapfil_bstr.data() << endl;
mapfil_bstr += mapfileP->str() ;
mapfil_bstr[13]=0;
cout << mapfil_bstr.data() << endl;
map_fd=shm_open (mapfil_bstr.data(),fileperms,0660);
if (map_fd<0) goto msgerrno_xit;
cout << map_fd <<endl;
if ((this->flags & _JWARE_CREAT) == _JWARE_CREAT) {
void *sharedMem;
initmap(map_fd);
if ((this->stationID=shmget(key,sizeof(this->mapP),_JWARE_CREAT|0660))<0)
cerr << "server shmget" << strerror(errno)<<endl;
if ((int)(this->mapCtlSharedMemP = shmat(this->stationID,0,0))==-1)
cerr << "server shmat" << strerror(errno)<<endl;
// if ((int)(this->mapCtlSharedMemP = shmat(this->stationID,sharedMem,0))==-1)
// cerr << "server shmat" << strerror(errno)<<endl;
mapP=mmap(0,sysconf(_SC_PAGESIZE),PROT_READ|PROT_WRITE,MAP_HASSEMAPHORE /*| MAP_FILE*/|MAP_VARIABLE|MAP_SHARED,map_fd,0);
memcpy(this->mapCtlSharedMemP,&(this->mapP),sizeof(void*));
msync(this->mapP/*+(3*sizeof(sem_t))*/,sysconf(_SC_PAGESIZE),MS_SYNC);
shmdt(this->mapCtlSharedMemP);
} else {
void *sharedMem;
if ((this->stationID=shmget(key,sizeof(this->mapP),IPC_ALLOC|SHM_RDONLY))<0)
cerr << "client shmget " << strerror(errno)<<endl;
if ((int)(this->mapCtlSharedMemP = shmat(this->stationID,0,0))==-1)
cerr << "client shmat" << strerror(errno)<<endl;
memcpy(&sharedMem,this->mapCtlSharedMemP,sizeof(void*));
this->mapP=mmap(sharedMem,sysconf(_SC_PAGESIZE),PROT_READ|PROT_WRITE,MAP_HASSEMAPHORE|MAP_FIXED/*|MAP_FILE*/|MAP_SHARED,map_fd,0);
strcpy (this->mapP+(3*sizeof(sem_t)),"kangaroot");
if ((long int)this->mapP == -1)
cerr << "mmap" << errno << " " << strerror(errno) << endl;
}
#ifdef _POSIX_SEMAPHORES
if ((this->flags & _JWARE_CREAT)==_JWARE_CREAT) {
cout << "creating semaphores in mapped file" <<endl;
if (sem_init(this->mapP,1,1)<0) /* init critical section semaphore */
cerr << "sem_init" << errno << " " << strerror(errno) << endl;
if (sem_init(this->mapP+sizeof(sem_t),1,0)<0) /*init server semaphore*/
cerr << "sem_init2" << errno << " " << strerror(errno) << endl;
if (sem_init(this->mapP+(2*sizeof(sem_t)),1,1)<0) /*init client semaphore*/
cerr << "sem_init3" << errno << " " << strerror(errno) << endl;
} else {
int semval;
cout << "semaphores not mapped " <<endl;
msync(this->mapP/*+(3*sizeof(sem_t))*/,sysconf(_SC_PAGESIZE),MS_SYNC);
if (sem_getvalue(this->mapP,&semval) <0)
cout << "semval1 " << semval << endl;
else
cout << "semval1 " << strerror(errno) << endl;
if (sem_getvalue(this->mapP+sizeof(sem_t),&semval) <0)
cout << "semval2 " << semval << endl;
else
cout << "semval2 " << strerror(errno) << endl;
if (sem_getvalue(this->mapP+(2*sizeof(sem_t)),&semval) <0)
cout << "semval3 " << semval << endl;
else
cout << "semval3 " << strerror(errno) << endl;
}
if ((this->flags & _JWARE_CREAT)==_JWARE_CREAT) {
msync(this->mapP,sysconf(_SC_PAGESIZE),MS_SYNC|MS_INVALIDATE);
}
#endif
//unlink (mapfil_bstr.data());
return;
msgerrno_xit:
cerr << "jMsg" << strerror(errno) << endl;
isValid=false;
return;
}
initmap(int map_fd) {
int index;
for (index=0;index < sysconf(_SC_PAGESIZE);index+=25)
write(map_fd,"er\048ahv856\011\09a\0f4fu\x1bk.}\00\017}~",25);
}
bool jMsg::jProto_iocntrl(unsigned int opcode) {
};
jMsg::~jMsg() {
struct shmid_ds removeBuf;
if (this->lockfileP) {
delete this->lockfileP;
}
if (this->mapfileP) {
delete this->mapfileP;
}
if ((this->flags & _JWARE_CREAT)==_JWARE_CREAT) {
shmctl(this->stationID,IPC_STAT,&removeBuf);
shmctl(this->stationID,IPC_RMID,&removeBuf);
}
shmdt(this->mapCtlSharedMemP);
#ifdef _POSIX_SEMAPHORES
if (this->mapCtlSharedMemP && (this->flags & _JWARE_CREAT)==_JWARE_CREAT ) {
sem_destroy(this->mapP);
sem_destroy(this->mapP+sizeof(sem_t));
sem_destroy(this->mapP+(2*sizeof(sem_t)));
}
#endif
if ((this->flags & _JWARE_CREAT)==_JWARE_CREAT)
munmap(this->mapP,this->size);
#ifndef _POSIX_SEMAPHORES
// unlink(lockfil_bstr.data());
#endif
}
void jMsg::read(void *dataP,int size,long msgT=0L ) {
int result;
struct flock msg_flck={F_SETLKW,0,0,0};
if (msgT==0) {
msgT=this->myType;
}
#ifdef _POSIX_SEMAPHORES
msync(this->mapP,sysconf(_SC_PAGESIZE),MS_SYNC);
if (sem_wait(this->mapP)<0) { /* critical section lock */
cout << "server read critical section failed with " << strerror(errno) << endl;
}
if ( (this->flags & _JWARE_CREAT)==_JWARE_CREAT) {
int semVal;
// after this client=1 server=1 Taunenbum UP
// server read after this block is like a socket accept()
if (sem_getvalue(this->mapP+sizeof(sem_t),&semVal)>=0)
cout << "server read pre-val" << semVal<< endl;
else cout << "server read pre-val failed with " << strerror(errno) << endl;
if (sem_wait(this->mapP+sizeof(sem_t)) < 0) {
cout << "read "<< strerror(errno) << endl;
}
} else {
// at start the client goes right through this
// to meet the "accept()" call goin on in the then clause.
// after this client=0 server=1 Taunenbum UP
sem_wait(this->mapP+(2*sizeof(sem_t)));
}
#else
fcntl(lock_fd,F_SETLKW,&msg_flck);
#endif
msync(this->mapP,sysconf(_SC_PAGESIZE),MS_SYNC/*|MS_INVALIDATE*/);
memcpy (dataP,this->mapP+(3*sizeof(sem_t)),size);
#ifdef _POSIX_SEMAPHORES
if ( (this->flags & _JWARE_CREAT)!=_JWARE_CREAT) {
// after this client=1 server =0 Taunenbaum UP
if (sem_post(this->mapP+sizeof(sem_t)) < 0)
cout << "read post sync" << strerror(errno)<<endl;
} else {
// after this server=0 client=1 Taunenbaum DOWN
if (sem_post(this->mapP+(2*sizeof(sem_t)))<0)
cout << "read post sync" << strerror(errno)<<endl;
}
sem_post(this->mapP); /* critical section unlock */
#else
fcntl(lock_fd,F_UNLCK,&msg_flck);
#endif
}
void jMsg::write(void *dataP,int size) {
int result;
long temp;
struct flock msg_flck={F_SETLKW,0,0,0};
#ifdef _POSIX_SEMAPHORES
if (sem_wait(this->mapP)<0) { /* critical section lock */
cerr << "critical section for write :" << strerror(errno) << endl;
msync(this->mapP,sysconf(_SC_PAGESIZE),MS_SYNC);
}
if ( (this->flags & _JWARE_CREAT)==_JWARE_CREAT) {
// after this client=1 server=1 Taunenbum UP
if (sem_wait(this->mapP+sizeof(sem_t))<0)
cout << "server write " << strerror(errno) << endl;
} else {
// after this client=1 server=1 Taunenbum UP
if (sem_wait(this->mapP+(2*sizeof(sem_t)))<0)
cout << "client write " << strerror(errno) << endl;
}
#else
fcntl(lock_fd,F_SETLKW,&msg_flck);
#endif
memcpy (this->mapP+(3*sizeof(sem_t)),dataP,size);
msync(this->mapP,sysconf(_SC_PAGESIZE),MS_SYNC|MS_INVALIDATE);
#ifdef _POSIX_SEMAPHORES
if ( (this->flags & _JWARE_CREAT)!=_JWARE_CREAT) {
int semValue;
// client=1 server=0 Taunenbum DOWN server read
sem_post(this->mapP+sizeof(sem_t));
// sem_post(this->mapP+sizeof(sem_t));
// sem_post(this->mapP+sizeof(sem_t));
sem_getvalue(this->mapP+sizeof(sem_t),&semValue);
cerr << "semval = " << (signed int)semValue << endl;
sem_getvalue(this->mapP+sizeof(sem_t),&semValue);
cerr << "semval = " << (signed int)semValue << endl;
} else {
// client=0 server=1 Taunenbum DONW client read
sem_post(this->mapP+(2*sizeof(sem_t)));
}
sem_post(this->mapP); /* critical section lock */
#else
fcntl(lock_fd,F_UNLCK,&msg_flck);
#endif
}
--------------29CD3BA36A78
Content-Type: text/plain; charset=us-ascii; name="jMsg.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="jMsg.h"
#ifndef _JMSG_H
typedef unsigned short ushort;
typedef unsigned long ulong;
#define _JWARE_CREAT 00001000
#define _JWARE_ALLOC 00100000
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <climits>
#include <strstream.h>
#ifdef _POSIX_SEMAPHORES
#include <semaphore.h>
#endif
#include <cerrno>
#include "jWare.h"
#include "security.h"
enum jAppOps { opRegister=1,
opShutdown,
opSuspendProcess,
opReduceProcess
};
typedef struct {
long mType;
long respChannel;
char textfield[64];
} jLoginMSG;
typedef struct {
long mType;
unsigned short confirm;
} jLoginRespMSG;
typedef struct {
long mType;
long keyLength;
long uid;
long fingerPrint[2];
char username[16];
unsigned char keys[128];
} jCreateUserMSG;
typedef struct {
long mType;
char checkString[32];
} jAuthMSG;
typedef struct {
long mType;
char path[128];
} jDirSearchMSG;
typedef struct {
long mType;
jAppOps opcode;
char appClass[16];
} jAppCtlMSG;
typedef struct {
long mType;
jAppOps opcode;
char savepath[96];
} jAppSuspendMSG;
enum jMssg_Types { jAuthUser_k = 0x1fb4c003L,
jUserAdd_k = 0x1e4587b3L,
jDirSearch_k = 0x5fef54bdL,
jRespondAuth_sub_k = 0x4f4897deL,
jAppliCtrl_k = 0x6de769deL
};
enum jReturnCodes { jNOROUTCONN= 3, jNOLGINRESP=4};
#pragma interface
class jMsg {
bool isValid;
int myType,lock_fd;
void *mapP;
unsigned long flags;
unsigned long size;
strstream *lockfileP,*mapfileP;
int stationID;
void * mapCtlSharedMemP;
public:
#ifdef _OSIX_SEMAPHORES
jMsg::jMsg(int,unsigned long flags=_JWARE_ALLOC,
unsigned long=512+sizeof(sem_t));
#else
jMsg::jMsg(int,unsigned long flags=_JWARE_ALLOC,unsigned long=512);
#endif
jMsg::~jMsg();
void jMsg::read(void*,int ,long flags=0);
void jMsg::write(void*,int);
bool jMsg::jProto_iocntrl(unsigned int);
};
#define _JMSG_H
#endif
--------------29CD3BA36A78--
next reply other threads:[~1997-06-19 4:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-06-19 4:00 magrasjc [this message]
1997-06-20 4:00 ` %!@# Help Posix semaphores & mmap Ben Chase
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox