Reputation: 79
I am writing a client-server program which uses shared memory. I have created an shm.c and shm.h to make the client and server file a lot nicer. However,I would like to know how can I initialise the shared memory using accessSHM (missing as I have no clue how could I reach it if someone could explain that as well that would be quite helpful) in the code below.
#include <stdbool.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "shm.h"
char * getTimeStamp() {
time_t ltime = time(NULL);
return strtok(ctime(<ime), "\n");
}
int createSHM(char * shname)
{
if ((shmid = shmget(key, sizeof(msg), IPC_CREAT)) < 0) {
perror("shmget");
exit(1);
}
} int loadSHM(char * shname) {
if (shname< 0)
{
printf("shmget error\n");
exit(1);
}
}
SHMstruct * accessSHM(int fd) {
}
SHMstruct * initSHM(int fd, SHMstruct *data) {
}
void clearSHM(SHMstruct * shm)
{
int status = munmap(shm, sizeof(SHMstruct));
if (status ==-1){
fprintf(stderr, "Failure in clearSHM",strerror(errno));
exit(errno);
}
}
void closeSHM(int fd)
{
int status = close(fd);
if (status ==-1){
fprintf(stderr, "Failure in closeSHM",strerror(errno));
exit(errno);
}
}
void destroySHM(char * shname)
{
int status = shm_unlink (shname);
if (status ==-1){
fprintf(stderr, "Failure in destroySHM",strerror(errno));
exit(errno);
}
}
shm.h
#ifndef _shm_h_
#define _shm_h_
#include <stdbool.h>
#define SHNAME "/shmserver" // shared memory
#define MAX_TICKETS 10
#define MAX_SLEEP 1 // seconds
typedef struct SHM {
int ticket;
bool isTaken;
bool soldOut;
} SHMstruct;
extern char * getTimeStamp();
extern int createSHM(char *shname);
extern int loadSHM( char *shname);
extern SHMstruct* initSHM( int fd, SHMstruct *data);
extern SHMstruct * accessSHM(int fd);
extern void clearSHM(SHMstruct * shm);
extern void closeSHM(int fd);
extern void destroySHM(char * shname);
#endif
Upvotes: 0
Views: 1288
Reputation: 241
Shared memory is a fairly large topic; it really isn't enough to just say "this is how you open one." Before writing one yourself, you may want to make sure you need to do it.
There are several libraries, including Boost interprocess, that provide a wrapper around creating and accessing shared memory. You may want to look there before deciding to write your own.
If you can't use Boost (or some other library), and really have to write one yourself, keep in mind that you are also going to need to provide a way for synchronization (locking) so that reads and writes aren't occurring at the same time from multiple processes. I would suggest looking at some code from some of those libraries, or even code from right here on stackoverflow: A simple shared memory application written on Linux to get the basics, but just go into this knowing it's going to be quite a bit of code to get a working library.
Upvotes: 1
Reputation: 1244
you can attach and get the address using shmat call.
p_mem = (message *) shmat(shmid,0,0);
In this example, message is a structure. You can use any data type. p_mem is like any other pointer.
char *p_mem;
p_mem = (char *) shmat(shmid,0,0);
strcpy(p_mem, "test");
Upvotes: 0