sys_shmget question

Posted on 2005-04-19
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

    >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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    This article will show, step by step, how to integrate R code into a R Sweave document
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now