Loading Data in shared memory (C++)

I'll try to keep this short, but basically I need to figure out a way to load data in shared memory (this file will be called load.c) I will later access the data with a print.c program.
The "data" is in the form of a student database that looks like this

John Blakeman

111223333

560 Southbrook Dr. Lexington,  KY 40522

8591112222

Paul S Blair

111223344

3197 Trinity Rd. Lexington,  KY 40533

etc....

I store it in a struct that i defined in a header file that looks like this(the ssn's are fake obviously, they are later used for a "change.c" portion)

#define KEY ((key_t)(10101)) /* Change to Last 5 digits of SSN */
#define SEGSIZE sizeof(struct StudentInfo)

#define NUM_SEMAPHS 5
#define SEMA_KEY ((key_t) (1010)) /* Last 4 digits of SSN */

struct StudentInfo{
char fName[20];
char lName[20];
char telNumber[15];
char whoModified[10];
};

void Wait(int semaph, int n);
void Signal(int semaph, int n);
int GetSemaphs(key_t k, int n);

Basically Im trying to figure out the best way to load an undefined number of "entires" of students into shared memory, so that I can access them later. I think I have the beginning part done...

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include "header.h"

main()
{   
    int i,id;
    struct StudentInfo *infoptr;
    int sema_set;

    id = shmget(KEY, SEGSIZE,IPC_CREAT|0666); /* get shared memory to store data */
    if (id < 0){
        perror("create: shmget failed");
        exit(1);
}

    infoptr=(struct StudentInfo *)shmat(id,0,0); /* attach the shared memory segment to the process's address space */

    if(infoptr <= (struct StudentInfo *) (0)) {
        perror("create: semget failed");
        exit(2);
    }

/* store data in shared memory segment */
//here's where I need help

However Im unsure of how to go about reading them in, I would assume I could store them in a vector/array.....but I wouldn't know how to do that when It came around to accessing them, especially when I dont know how many there are.

I do need to provide mutual exclusion (no more than one writer, but as many readers as I want)...altho Im a bit unsure of how many sephamores to use, I was using a default of 5.....but Im not sure if that is correct...Also I know i need to synchronize concurrent access since multiple writers/readers will be accessing change.c. The whole sephamores thing is a tad confusing tho...

Any hints/ideas/tips would be SUPER helpful. im not asking for an answer, but maybe a point in the right direction. Shared memory is a new thing for me.

Instead of using shmat, how about mmap? This solves the storage problem -- you no longer need to explicitly write to disk, just alter the memory and the OS will follow suit. It also makes it possible to use data larger than your system's memory without excessive swapping, since it only reads in the parts you're presently using. It also makes it easier for your readers to find the file -- it's just an ordinary file, not some weird special memory object thingamabob.

It does make the file fixed length though -- or at least, cumbersome to change the length of.

#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

...

mystruct str, *shared;
int fd, n, len;

fd=open("filename", O_RDWR);
memset(&str, 0, sizeof(str));

// Create a blank file 1024 records long
for(n=0; n<1024; n++)
{
        write(fd, &str, shared);
}

// Mapped area must be a multiple of the page size
len=sizeof(str) * 1024;
if(len % getpagesize())
{
        len -= (len % getpagesize());
        len += getpagesize();
}

// Map the file into memory.
mystruct *mem=mmap(NULL, len, PROT_READ|PROT_WRITE,
        MAP_SHARED, fd, 0);
// We can now close the fd.
close(fd);

if(mem == MAP_FAILED)
{
        fprintf(stderr, "Couldn't map memory\n");
}
else
{
        fprintf(stderr, "mapped into %p\n", mem);
        // As the contents of the memory change, so will the contents on disk.
        // This I/O may just be cached in memory for the OS to write when
        // convenient, but the data shared between processes will remain
        // current and consistent.
        strcpy(mem[0].fName, "Foo");
        strcpy(mem[0].lName, "Bar");
        munmap(mem, len);
}

unfortunately i have to use shmat <_<. sadly.
am I at least "close" to my implementation using shmat?

Is this homework? Sounds like it. We have a special forum for homework. Stipulations like yours generally arise from two sources: crazed management or classroom assignments. The "crazed" part applies to both.

It's homework, i wasn't aware there was a special forum (srry) but thats also why i wasn't asking for an answer, i was just asking of ways to do things :slight_smile:

We appreciate that sentiment, hence why we have a forum for homework at all. But this isn't it.

So do you want me to repost it in the HW section? or can it be like "moved"?

Here is an example. Use a mutex (mutual exclusion semaphore) which is a special subset of semaphores:

Closing the thread. If you need more help, post in homework.

Thanks.