views:

90

answers:

1

So Im making a Load.c file, that basically will load a bunch of "students" into shared memory. The students are stored in a struct that looks like this:

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

Anyways I need to load this in shared memory, we were given some sample code. and we are reading the code from a data file that will look like this:

John Blakeman

111223333

560 Southbrook Dr. Lexington,  KY 40522

8591112222

Paul S Blair

111223344

3197 Trinity Rd. Lexington,  KY 40533

etc....

Here's my idea for the code: (header.h just has struct info/ and semphamore count....which im unsure of what it needs to be, right now it's labeled as 5)

#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

Thats about as far as I got. now I know I can store data using strcpy(infoptr->fName,"Joe"); (for example) but I need to read an X number of names? How would I go about storing them? Using some sort of push/pop vector of structs? how would it look like?

And do I adjust sephamores based on how many "entires" there are I assume? Im a little bit confused how to adjust my Number of Sephamores.

Oh btw here's my header file just in case (SSN's are fake obviously)

/* header.h */
#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);
A: 

Uhm... I'm not sure here, but this is what I understood:

You're loading struct StudentInfo blocks into a shared memory space, and you want to be able to access it from other processes?

First, consider that your structure is fixed-size. If you want to read 10 names, you need to get sizeof(struct StudentInfo) * 10 bytes, if you want 400, make that * 400 -- so you don't need to push and pop your students from any kind of queue, since you can just use math to calculate from where and up to where you need to read. Getting students 10 to 20 is just reading the shared memory space from sizeof(struct StudentInfo) * 10 to sizeof(struct StudentInfo) * 10

As for mutual exclusion (if you're going to have multiple readers or writers, which I assume is what you wanted the semaphores for), I do not recommend semaphores. They are adequate for simpler kinds of exclusion, like "don't use this function if I'm using it", but not for locking large sets of data.

I'd use file locking. In Unix, you can use file locking primitives to create advisory locks over specific bytes in a file, even if the file is 0-bytes-long. What does this mean?

Advisory means you don't enforce them, other processes must respect them willingly. 0-byte-long means you can open a file that doesn't exist, and lock portions of it corresponding to your student structure positions in shared memory. You don't need the file to actually have data, you can use it to represent your shared memory database without writing anything to it.

What's the advantage of this over semaphores? You have fine-grained control of your locks with a single file descriptor!

Wheh, that got long. Hope I helped.

Santiago Lezica
I think for the assignment we have to use sephamores. and yes you have helped alot! Also how do i decide how many sephas to use.
Mercfh
Well, depends on how fine-grained you want your locking mechanism to be. You can use a single semaphore to lock the whole database, or as many semaphores as entries you have if you want to lock them separately. Keep in mind, though, that semaphores aren't infinite, and that if you implement multiple locks you'll have to synchronize acquiring and releasing them for operations that involve multiple entries.
Santiago Lezica
Hmm well i know I have to provide mutual exclusion between readers and writers. and I also know several students/advisors will access this database so I need to synchronize concurrent access since multiple readers and writers will be accessing it. However I dont know how many sephamores that means I will need since we have just started learning about this stuff.
Mercfh
Well, here's some ideas. A very simple implementation would use a single semaphore to prevent anyone from accessing the data while it's in use. A slightly better implementation would only lock the database when someone wants to write it, and allow readers to access it concurrently with no blocking. A much better implementation could use selective semaphores for records that are being written. You could have a semaphore pool, and "assign" them to records being written.
Santiago Lezica
I think thats maybe what the default professor had? he had it defaulted at 5 sephamores (seen in the header file) but I wasn't entirely sure why (im still new to the whole sephamore thing)
Mercfh
It's been 21 hours since the first time you said you were new to the whole semaphore thing :) I think the book Unix Systems Programming can be found online -- look it up, you'll learn in no time. There's nothing fancy about semaphores, they are nothing more than shared counters that can't go below 0 and can be modified atomically.
Santiago Lezica