[Last Call] Learn how to a build a cloud-first strategyRegister Now


sys_shmget question

Posted on 2005-04-19
Medium Priority
Last Modified: 2008-01-16
I am writing a program for my operating system class.  It's due in two days so this is very urgent.  Can anyone explain in detail what this function is doing?  I need to use this to build off of so I need to really understand everything about it.  For example, what does sim_context_t *tctx mean?  What does struct shmget_args *args = (struct shmget_args*) ptr; do?  What does size_t sz = args->sz; mean?  I know some of these questions are basic but I'm really not very good at programming.  This code was written by another student so it may not be 100% correct.  Thanks

int sys_shmget(sim_context_t *tctx, void *ptr)
{   struct shmget_args *args = (struct shmget_args*) ptr;
    struct shm_area *shm_segments;
    int id = 0;
    int found = 0;
    size_t sz = args->sz;
    //First thing to do is align the requested size to a full page:
    if(sz % PAGE_SIZE) // if it's not perfectly page aligned
          sz = (((PAGE_SIZE/sz)+1)*PAGE_SIZE); // allocate a perfect page
          if(kern_data->shmem == NULL)
          {     SIM_printf("No existing shared memory!\n");
               kern_data->shmem = ((struct shm_area *)kmalloc(sizeof(struct shm_area))); // allocate kernel memory for a list of shared memory segments
               kern_data->shmem->key = 0;
               kern_data->shmem->next = NULL;
          shm_segments = kern_data->shmem;
          while(shm_segments->key != args->key && shm_segments->next != NULL)
          {    id++;
               shm_segments = shm_segments->next;
               if(shm_segments->key == args->key)
               {     found = 1;
               // by our reasoning above, we should be at the last position in the list and haven't found our keyed segment
               shm_segments->next = ((struct shm_area *)kmalloc(sizeof(struct shm_area))); // allocate a new shm_area
               shm_segments = shm_segments->next;
               shm_segments->next = NULL; // now we are the end of the list
               // then create a new vm_area and attach it to the existing process
               vm_area_t *new_area = make_brk_area(CURPROC, sz);
               new_area->flags = MAP_SHARED; // this is a shared page
               // TODO: SET FLAGS according to args.flags
               // add this to the current processes vm area (add_vm_area)
               add_vm_area(CURPROC, new_area);
               return(id); // return the offset to the last position in the list
               return(id); // id starts at 0, is incremented every time through the loop, so this should be the offset into the linked list
          return -1;      
Question by:munchkin65
1 Comment
LVL 16

Accepted Solution

nonubik earned 2000 total points
ID: 13822058
>sim_context_t *tctx
A variable, tctx, of type pointer to sim_context_t was declared.

>struct shmget_args *args = (struct shmget_args*) ptr;
A variable, args, of type pointer to the structure shmget_args was declared and assigned the value of the ptr variable, casted to the proper type.

>size_t sz = args->sz;
A varaible, sz, of type size_t was declard and assigned the value of the sz member of the args (see above) struct variable.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question