Link to home
Start Free TrialLog in
Avatar of munchkin65
munchkin65

asked on

sys_shmget question

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;
                    break;
                }
          }
 
          if(!found)  
          {
               // 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;
               id++;
               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
          }
          else
          {
               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;      
}
ASKER CERTIFIED SOLUTION
Avatar of nonubik
nonubik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial