Example #1
0
void *initialize_user_process(void *arg)
{
  //  printf("Enters initalize_user_process\n");
  int i;
  char **filename = (char **)arg;
  int argc = 0;
    
  while (filename[argc]!=NULL)
    {
       printf("This is filename[%i]: %s\n", argc, filename[argc]);
      argc++;
    }
  //    printf("This is argc in init: %i\n", argc);
    
  //Step 19: putting in argc
  //k = WordToMachine(argc);
  //memcpy(main_memory+MemorySize-40+12, &k, 4);
    
  //L3 Step 18: start first process, is this init? 
    init=(PCB *)malloc(sizeof(PCB));
    init->registers = (int *)malloc(NumTotalRegs*sizeof(int));
    for (i=0; i < NumTotalRegs; i++)
    {  
      init->registers[i] = 0;
    }
    init->pid = (int *)0;
    //    printf("This is init's pid: %i\n", (int)init->pid);
    //L3 Step 19: 
    init->waiter_sem = make_kt_sem(0);
    init->waiters = new_dllist();
    init->children = make_jrb();

    //Allocate a new PCB
  PCB *temp=(PCB *)malloc(sizeof(PCB));
  temp->registers = (int *)malloc(NumTotalRegs*sizeof(int));
  temp->user_base = 0;
  //printf("Initial user_base: %i\n", temp->user_base);
  //Changed Step 12: temp->user_limit = MemorySize-2048;
  temp->user_limit = MemorySize/8;
  //printf("Initial user_limt: %i\n", temp->user_limit);
  
  //L3 Step 18: 
  temp->parent = init;
  //L3 Step 19: 
  temp->waiter_sem = make_kt_sem(0);
  temp->waiters = new_dllist();
  //L3 Step 21: make rb tree for children 
  temp->children = make_jrb();
  
  //Changed at Step 12: User_Base = temp->user_base;
  User_Base = memory8();
  User_Limit = temp->user_limit;
  //printf("This is User_Base in initialize: %i\n", User_Base);
  //printf("This is User_Limit in initialize: %i\n", User_Limit);

  //set the regs of the
  //printf("Setting all the registers to 0 in initalize_user_process\n");
  for (i=0; i < NumTotalRegs; i++)
    temp->registers[i] = 0;
  
  //printf("Setting pid in init\n");
  temp->pid = (int *)get_new_pid();
  printf("First Pid: %i and its parent's should be 0 init: %i\n", temp->pid, temp->parent->pid );
  /* set up the program counters and the stack register */
  temp->registers[PCReg] = 0;
  temp->registers[NextPCReg] = 4;

  //insert the first process as init's child; WOW you can use this function!
  Jval tempN = new_jval_v((void*)temp); //can only insert Jvals 
  jrb_insert_int(init->children, (int)temp->pid, tempN); //JRB tree, int ikey, Jval val 
  //JRB ptr;
  //  jrb_traverse(ptr, init->children)
  //{
  //printf("This is child pid %i of init %i\n", ptr->key, (int)init->pid);
  //}
  //perform_execve(job, fn, argv)
  // where job is the new PCB, fn is the name of your initial executable (at this point, mine is a.out),
  //and argv is the argv of this initial job.
  //returns-> 0 success, errno if error
  //PrintStack(temp->registers[StackReg], temp->user_base);
  int errno = perform_execve(temp, filename[0],filename);
  // printf("This is perform_execve: %i\n", errno);
    //returns to initialize_user_process(), it either exits because there was an error, or it puts the new job onto the ready queue and calls kt_exit()
  PrintStack(temp->registers[StackReg], temp->user_base);
  if (errno!=0)
  {
      printf("Perform_execve returned unsucessful\n"); 
      kt_exit();
  }
  //printf("Placing jval into queue\n");
  Jval value = new_jval_v((void *)temp);
  //value.v = temp;
  //printf("This is the value being placed into the readyq in the initalize_user_process: %s\n",value.v );
  dll_append(readyq, value);
  // printf("Program %s loaded\n", kos_argv[0]);
  kt_exit();
}
Example #2
0
void *initialize_user_process(void *arg){

  PCB *user_process;
  Jval jval_pcb;
  Jval user_process_child_node;
  char **filename;
  int base;
  int arg_count;
  int errno;
  int i;
  
  arg_count = 0;
  errno = 0;
  i = 0;
  
  
  
  /* Unmarshalling */
  filename = (char **) arg;
  
  
  /* Create the global init process */
  init = (PCB *)malloc(sizeof(PCB));
  init->registers = (int *)malloc(sizeof(int) * NumTotalRegs);
  
  // Initialize init's registers */
  for (i = 0; i < NumTotalRegs; i++) {
    init->registers[i] = 0;
  }
  
  // Initialize init's other variables
  init->pid = (int *)0;
  init->waiter = make_kt_sem(0);
  init->waiters = new_dllist();
  init->children = make_jrb();
  
  
  
  /* Allocate new PCB and initialize registers */
  user_process = (PCB *)malloc(sizeof(PCB));
  user_process->registers = (int *)malloc(NumTotalRegs * sizeof(int));
  for (i = 0; i < NumTotalRegs; i++) {
    user_process->registers[i] = 0;
  }
  
  // Obtain pid and partiton memory for new process
  user_process->pid = (int *)get_new_pid();
  base = partition_memory(user_process->pid);
  
  
  // Initialize first process' other variables
  user_process->waiter = make_kt_sem(0);
  user_process->waiters = new_dllist();
  user_process->children = make_jrb(); 
  

  // Set base and limit for new process
  user_process->base = 0;
  user_process->limit = MemorySize/8;
  
  // Set User global vars
  User_Base = base;
  User_Limit = user_process->limit;
  
  // Set process' PCRegs
  user_process->registers[PCReg] = 0;
  user_process->registers[NextPCReg] = 4;
  
  
  // Set first process' parent as init and add to init's children
  user_process->parent = (struct PCB *)init;
  user_process_child_node.v = user_process;
  jrb_insert_int(init->children, (int)user_process->pid, user_process_child_node);
    
  
  // Get number of args and turn off exec flag.
  while (filename[arg_count] != NULL) {
    arg_count++;
  }
  totalArgs = arg_count;
  is_exec = 0;
  
  /* Call perform_execve */
  errno = perform_execve(user_process, filename[0], filename);
  
  // If errors, returns proper errno
  if (errno != 1) {
    syscall_return(user_process, errno);
  } else {
  // Puts new job onto readyQ and calls kt_exit()
    jval_pcb.v = user_process;
    dll_append(readyQ, jval_pcb);
    kt_exit();
  }
  
}